I'm using the zfcuser doctrine module for my zf2 project with bjyauthorize, which is working quite fine. Now I'd like to get the connected Country entity out of my User entity
in User.php:
/**
* An example of how to implement a role aware user entity.
*
* @ORM\Entity
* @ORM\Table(name="users", indexes={
* @ORM\Index(name="fk_User_Country1_idx", columns={"Country_id"}),
* }, uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})})
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface, ProviderInterface
{
...
/**
* @var ersEntity\Country
* @ORM\ManyToOne(targetEntity="Country", inversedBy="users")
* @ORM\JoinColumn(name="Country_id", referencedColumnName="id")
*/
protected $country;
in Country.php
/**
* Entity\Country
*
* @ORM\Entity()
* @ORM\Table(name="Country")
* @ORM\HasLifecycleCallbacks()
*/
class Country implements InputFilterAwareInterface
{
...
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="country")
* @ORM\JoinColumn(name="id", referencedColumnName="Country_id")
*/
protected $users;
A simple testAction in one of my Controllers fails:
$user = $em->getRepository("ersEntity\Entity\User")
->findOneBy(array('id' => 1));
error_log($user->getFirstname().' '.$user->getSurname());
error_log('country: '.$user->getCountry()->getName());
which results in:
[Tue May 19 00:02:44 2015] [error] [client 185.17.207.16] Andi N.
[Tue May 19 00:02:44 2015] [error] [client 185.17.207.16] PHP Fatal error: Call to a member function getName() on a non-object in /home/ers/www/ers/module/Admin/src/Admin/Controller/TestController.php on line 172
I'm wondering why it's not possible to get the Country entity from the User entity. With other entities in this same project this is working fine.
Can somebody tell me what needs to be done to be able to get the Country entity out of this zfcuser-bjyauthorize-doctrine User entity?
For more code info, this whole project is available at https://github.com/inbaz/ers in the develop branch.
EDIT:
With users who have no country set there needs to be an error. It's right that there is the need to check if a country exists. But this user has a country set. I checked that via phpmyadmin. It's not possible to get this country entity via the getCountry() method.
Maybe this is cause the deserializing and serializing of the doctrine entity into the session. I checked the doctrine documentation on how to save entities into the session. But I'd like to keep all subentities in the session, so in my case I have a order entity in the session which holds multiple package entities. Each package entity has one user and multiple item entities. When getting the order entity back from the session I'd like to be able to access all these elements.
I even tried to do a merge on each user in the session like:
foreach($participants as $participant) {
$participant = $em->merge($participant);
}
but that doesn't change anything. Even a merge on the whole order was not successful.
Do you have an idea on how to get the doctrine entities back from the session with the full doctrine features?