Symfony2 Doctrine Doesn't Create One-to-Many Structures For Me

I have two tables. The first table is users , and the second is datas . There is no user idx value in useridx columns. (primary unique key).

These are the table structures:

users table

 CREATE TABLE public.users ( idx bigint NOT NULL, "name" varchar(250) DEFAULT NULL::character varying, surname varchar(250) DEFAULT NULL::character varying, isactive boolean NOT NULL DEFAULT false, /* Keys */ CONSTRAINT users_pkey PRIMARY KEY (idx), CONSTRAINT users_idx_key UNIQUE (idx) ) WITH ( OIDS = FALSE ); 

datas table:

 CREATE TABLE public.datas ( idx bigint NOT NULL, useridx bigint, phrase varchar(100) DEFAULT NULL::character varying, response varchar(100) DEFAULT NULL::character varying, /* Keys */ CONSTRAINT datas_pkey PRIMARY KEY (idx), CONSTRAINT datas_idx_key UNIQUE (idx), /* Foreign keys */ CONSTRAINT fk_cf180c1a262768b5 FOREIGN KEY (useridx) REFERENCES public.users(idx) ON DELETE NO ACTION ON UPDATE NO ACTION ) WITH ( OIDS = FALSE ); 

Now when I run these commands:

 app/console doctrine:mapping:convert yml ./src/Acme/DemoBundle/Resources/config/doctrine/metadata/orm --from-database --force 

and

 app/console doctrine:mapping:import AcmeDemoBundle annotation app/console doctrine:generate:entities AcmeDemoBundle 

I got this result:

Datas.php

 namespace Acme\DemoBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Acme\DemoBundle\Entity\Datas * * @ORM\Table(name="datas") * @ORM\Entity */ class Datas { /** * @var bigint $idx * * @ORM\Column(name="idx", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="datas_idx_seq", allocationSize="1", initialValue="1") */ private $idx; /** * @var string $phrase * * @ORM\Column(name="phrase", type="string", length=100, nullable=true) */ private $phrase; /** * @var string $response * * @ORM\Column(name="response", type="string", length=100, nullable=true) */ private $response; /** * @var Users * * @ORM\ManyToOne(targetEntity="Users") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="useridx", referencedColumnName="idx") * }) */ private $useridx; /** * Get idx * * @return bigint */ public function getIdx() { return $this->idx; } /** * Set phrase * * @param string $phrase */ public function setPhrase($phrase) { $this->phrase = $phrase; } /** * Get phrase * * @return string */ public function getPhrase() { return $this->phrase; } /** * Set response * * @param string $response */ public function setResponse($response) { $this->response = $response; } /** * Get response * * @return string */ public function getResponse() { return $this->response; } /** * Set useridx * * @param Acme\DemoBundle\Entity\Users $useridx */ public function setUseridx(\Acme\DemoBundle\Entity\Users $useridx) { $this->useridx = $useridx; } /** * Get useridx * * @return Acme\DemoBundle\Entity\Users */ public function getUseridx() { return $this->useridx; } } ?> 

Users.php

 <?php namespace Acme\DemoBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Acme\DemoBundle\Entity\Users * * @ORM\Table(name="users") * @ORM\Entity */ class Users { /** * @var bigint $idx * * @ORM\Column(name="idx", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="users_idx_seq", allocationSize="1", initialValue="1") */ private $idx; /** * @var string $name * * @ORM\Column(name="name", type="string", length=250, nullable=true) */ private $name; /** * @var string $surname * * @ORM\Column(name="surname", type="string", length=250, nullable=true) */ private $surname; /** * @var boolean $isactive * * @ORM\Column(name="isactive", type="boolean", nullable=false) */ private $isactive; /** * Get idx * * @return bigint */ public function getIdx() { return $this->idx; } /** * Set name * * @param string $name */ public function setName($name) { $this->name = $name; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set surname * * @param string $surname */ public function setSurname($surname) { $this->surname = $surname; } /** * Get surname * * @return string */ public function getSurname() { return $this->surname; } /** * Set isactive * * @param boolean $isactive */ public function setIsactive($isactive) { $this->isactive = $isactive; } /** * Get isactive * * @return boolean */ public function getIsactive() { return $this->isactive; } } ?> 

I also have yml files, but I don’t think they are needed here only for the PHP files that I posted here.

Now when I run this command inside my controller:

 <?php $user = $this->getDoctrine() ->getRepository('AcmeDemoBundle:Users') ->find(24); $phrase = $user->getDatas()->getPhrase(); ?> 

I received the error message Call to a member function getDatas() on a non-object... I know this is understandable. In User.php, I don't have getDatas ().

But what I read from the Symfony2 and Doctrine documentation should be there because they are related. All I want to do is get Datas inside Users.

What's my mistake? What do they miss?

Update : I added these lines to Users.php

 <?php /** * @var \Acme\DemoBundle\Entity\Datas * * @ORM\OneToMany(targetEntity="Datas", mappedBy="datas", cascade={"all"}) */ private $datas; public function __construct() { $this->datas = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add phrases * * @param Acme\DemoBundle\Entity\Datas $datas */ public function addPhrases(\Acme\DemoBundle\Entity\Datas $datas) { $this->datas[] = $datas; } /** * Get datas * * @return Doctrine\Common\Collections\Collection */ public function getDatas() { return $this->datas; } ?> 

And these lines in Datas.php

 <?php /** * @ORM\ManyToOne(targetEntity="Users", inversedBy="users", cascade={"all"}) */ protected $users; /** * Set users * * @param Acme\DemoBundle\Entity\Users $users */ public function setUsers(\Acme\DemoBundle\Entity\Users $users) { $this->users = $users; } /** * Get users * * @return Acme\DemoBundle\Entity\Users */ public function getUsers() { return $this->users; } ?> 

getDatas() now works, but not inside. ( $user->getDatas()->getPhrase(); ) I get this error:

 Call to undefined method Doctrine\ORM\PersistentCollection::getPhrase() 

Conclusion I got a collection error because it returns a collection - course. Iterations (e.g. foreach) and you will get access to the data. (If you encounter such a problem.)

+4
source share
1 answer

If you look at the @JoinColumn documentation

This annotation is used in the context of relationships in the fields @ManyToOne, @OneToOne and in the context of @JoinTable nested inside @ManyToMany . This annotation is not required. If not specified, the attribute name and referColumnName are inferred from the table names and primary keys.

So, since you are using the ManyToOne relationship, your relationship definition will be,

 /** * @var Users * * @ORM\ManyToOne(targetEntity="Users") * @ORM\JoinColumn(name="useridx", referencedColumnName="idx") */ private $useridx; 

Edit:

If you want to get data from the user part, you need to create OneToMany . eg

In Datas.php

 /** * @var Users * * @ORM\ManyToOne(targetEntity="Users", inversedBy = "datas") * @ORM\JoinColumn(name="useridx", referencedColumnName="idx") */ private $useridx; 

And in Users.php add the following line,

 /** * @ORM\OneToMany(targetEntity="Datas", mappedBy="useridx", cascade={"persist"}) */ protected $datas; 

And then run the doctrine:generate:entities command. To perform a relationship check this doc entry.

+3
source

Source: https://habr.com/ru/post/1410844/


All Articles