Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2/Doctrine - Entity variable not returning an ArrayCollection object
    text
    copied!<p>I have used ArrayCollections in my last project, which worked fine. Now in my new project, I am attempting to implement one, and it does NOT work completely even though I have done the same thing in both projects.</p> <p><strong>What works:</strong> I can add a new element in the ArrayCollection, and when I print out an overview of the object entity, I do get to see the elements I have put in, before and after persisting and flushing, in the same method.</p> <p><strong>What does NOT work:</strong> When I try to get the ArrayCollection from the entity, it returns nothing. When I do a getype($collection), I get null as well.</p> <p><strong>Code:</strong></p> <p>This is the code for both entities. Users is the entity that contains the ArrayCollection (emails), and Email is the entity I want to add into the ArrayCollection. Note that the inconsistency of the name of both entities was done on purpose.</p> <p><strong>Users.cpp (Entity)</strong></p> <pre><code>&lt;?php namespace HRPortal\SystemBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * Users * * @ORM\Table() * @ORM\Entity(repositoryClass="HRPortal\SystemBundle\Entity\UsersRepository") */ class Users { // ... /** * @var emails[] * * $ORM\OneToMany(targetEntity="Email", mappedBy="user", indexBy="id") */ private $emails; /** * Constructor for the class. Sets createdAt to Now * */ public function __construct() { // ... $this-&gt;emails = new ArrayCollection(); } /** * Get value for data '$name' * @param string $name * @return Type of data '$name' */ public function __get($name) { return $this-&gt;$name; } /** * Set value for data '$name' * @param string $name * @param generic $value * @return Type of data '$name' */ public function __set($name, $value) { $this-&gt;$name = $value; return $this; } public function offsetGet($name) { return $this-&gt;$name; } } </code></pre> <p><strong>Email.php (Entity)</strong></p> <pre><code>&lt;?php namespace HRPortal\SystemBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Email * * @ORM\Table() * @ORM\Entity(repositoryClass="HRPortal\SystemBundle\Entity\EmailRepository") */ class Email { // ... /** * @var integer * * @ORM\ManyToOne(targetEntity="Users", inversedBy="emails") */ private $user; /** * @var string * * @ORM\Column(name="email", type="string", length=255) */ private $email; // ... } </code></pre> <p><strong>Part of the code from the controller</strong></p> <pre><code>$email = new Email(); $user-&gt;password = $bcrypt-&gt;hash($user-&gt;password); $user-&gt;emails-&gt;add($email); // print_r($user) will show data from $email (Emails Entity) $email-&gt;email = $form-&gt;get('email')-&gt;getData(); $email-&gt;is_default = true; $email-&gt;user = $user; $em-&gt;persist($email); $em-&gt;persist($user); $em-&gt;flush(); // print_r($user) will also show data from $email (of course) </code></pre> <p>However, when I try to access it later:</p> <pre><code>public function getDefaultEmail($user) { // print_r($user) will leave 'emails' empty but print out any other data // print_r($user-&gt;emails) will not show anything // Getting an error when accessing foreach, as $user-&gt;emails is null foreach ($user-&gt;emails as $email) { if($email-&gt;is_default === true){ return $email-&gt;email; } } return false; } </code></pre> <p>Any suggestions?</p> <p>Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload