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, 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, CONSTRAINT datas_pkey PRIMARY KEY (idx), CONSTRAINT datas_idx_key UNIQUE (idx), 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; class Datas { private $idx; private $phrase; private $response; private $useridx; public function getIdx() { return $this->idx; } public function setPhrase($phrase) { $this->phrase = $phrase; } public function getPhrase() { return $this->phrase; } public function setResponse($response) { $this->response = $response; } public function getResponse() { return $this->response; } public function setUseridx(\Acme\DemoBundle\Entity\Users $useridx) { $this->useridx = $useridx; } public function getUseridx() { return $this->useridx; } } ?>
Users.php
<?php namespace Acme\DemoBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Users { private $idx; private $name; private $surname; private $isactive; public function getIdx() { return $this->idx; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setSurname($surname) { $this->surname = $surname; } public function getSurname() { return $this->surname; } public function setIsactive($isactive) { $this->isactive = $isactive; } 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 private $datas; public function __construct() { $this->datas = new \Doctrine\Common\Collections\ArrayCollection(); } public function addPhrases(\Acme\DemoBundle\Entity\Datas $datas) { $this->datas[] = $datas; } public function getDatas() { return $this->datas; } ?>
And these lines in Datas.php
<?php protected $users; public function setUsers(\Acme\DemoBundle\Entity\Users $users) { $this->users = $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.)