Symfony2/Doctrine - Entity variable not returning an ArrayCollection object

1.7k views Asked by At

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.

What works: 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.

What does NOT work: When I try to get the ArrayCollection from the entity, it returns nothing. When I do a getype($collection), I get null as well.

Code:

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.

Users.cpp (Entity)

<?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->emails = new ArrayCollection();
    }

    /**
     *  Get value for data '$name'
     *  @param string $name
     *  @return Type of data '$name'
     */
    public function __get($name)
    {
        return $this->$name;
    }

    /**
     *  Set value for data '$name'
     *  @param string $name
     *  @param generic $value
     *  @return Type of data '$name'
     */
    public function __set($name, $value)
    {
        $this->$name = $value;
        return $this;
    }

    public function offsetGet($name)
    {
        return $this->$name;
    }
}

Email.php (Entity)

<?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;

    // ...
}

Part of the code from the controller

$email = new Email();

$user->password = $bcrypt->hash($user->password);
$user->emails->add($email);
// print_r($user) will show data from $email (Emails Entity)
$email->email = $form->get('email')->getData();
$email->is_default = true;
$email->user = $user;

$em->persist($email);
$em->persist($user);

$em->flush();
// print_r($user) will also show data from $email (of course)

However, when I try to access it later:

public function getDefaultEmail($user)
{
    // print_r($user) will leave 'emails' empty but print out any other data
    // print_r($user->emails) will not show anything
    // Getting an error when accessing foreach, as $user->emails is null
    foreach ($user->emails as $email) {
        if($email->is_default === true){
            return $email->email;
        }
    }
    return false;
}

Any suggestions?

Thank you

1

There are 1 answers

4
SuperBob On

edit:

Your emails are properly registered in DB?

You probably forgot the ORM/JoinColumn.


Email.php

<?php
// ...

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Users", inversedBy="emails")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

// ...