0

I have the following:

User have one group, group can have many users

User

       <?php namespace Application\Model;

use Doctrine\Common\Collections;
use Doctrine\ORM\Mapping as ORM;

/**
 * User model
 * Read-only entity
 * @ORM\Table(name="VLOGGER_WEBCALENDAR_USR")
 * @ORM\Entity
 * @package Application\Model
 */
class User
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(name="USR_ID", type="integer")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="USR_LOGIN", type="string")
     */
    protected $login;

    /**
     * @var string
     * @ORM\Column(name="USR_CODE", type="string")
     */
    protected $code;

    /**
     * @var int
     * @ORM\Column(name="GRP_ID", type="integer")
     */
    protected $groupId;

    /**
     * @var string
     * @ORM\Column(name="GRP_CODE", type="string")
     */
    protected $groupCode;

    /**
     * @var User\Group
     * @ORM\ManyToOne(targetEntity="Application\Model\User\Group",fetch="EAGER")
     * @ORM\JoinColumn(name="GRP_ID", referencedColumnName="GRP_ID")
     */
    protected $group;

    /**
     * @var Event
     * @ORM\OneToMany(targetEntity="Application\Model\Event", mappedBy="user")
     * @ORM\JoinColumn(name="USR_ID", referencedColumnName="USR_ID")
     */
    protected $events;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->events = new Collections\ArrayCollection();
    }

    /**
     * @return string
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param string $code
     * @return User
     */
    public function setCode($code)
    {
        $this->code = $code;
        return $this;
    }

    /**
     * @return Event
     */
    public function getEvents()
    {
        return $this->events;
    }

    /**
     * @param Event $events
     * @return User
     */
    public function setEvents($events)
    {
        $this->events = $events;
        return $this;
    }

    /**
     * @return User\Group
     */
    public function getGroup()
    {
        return $this->group;
    }

    /**
     * @param User\Group $group
     * @return User
     */
    public function setGroup($group)
    {
        $this->group = $group;
        return $this;
    }

    /**
     * @return string
     */
    public function getGroupCode()
    {
        return $this->groupCode;
    }

    /**
     * @param string $groupCode
     * @return User
     */
    public function setGroupCode($groupCode)
    {
        $this->groupCode = $groupCode;
        return $this;
    }

    /**
     * @return int
     */
    public function getGroupId()
    {
        return $this->groupId;
    }

    /**
     * @param int $groupId
     * @return User
     */
    public function setGroupId($groupId)
    {
        $this->groupId = $groupId;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     * @return User
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string
     */
    public function getLogin()
    {
        return $this->login;
    }

    /**
     * @param string $login
     * @return User
     */
    public function setLogin($login)
    {
        $this->login = $login;
        return $this;
    }
}

Group

    <?php namespace Application\Model\User;

use Application\Model;
use Doctrine\Common\Collections;
use Doctrine\ORM\Mapping as ORM;

/**
 * User group model
 * Read-only entity
 * @ORM\Table(name="VLOGGER_WEBCALENDAR_GRP")
 * @ORM\Entity
 * @package Application\Model
 */
class Group
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(name="GRP_ID", type="integer")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="GRP_CODE", type="string")
     */
    protected $code;

    /**
     * @var Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="Application\Model\User", mappedBy="group")
     * @ORM\JoinColumn(name="GRP_ID", referencedColumnName="GRP_ID")
     */
    protected $users;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->users = new Collections\ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param mixed $code
     * @return Group
     */
    public function setCode($code)
    {
        $this->code = $code;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     * @return Group
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * @param mixed $users
     * @return Group
     */
    public function setUsers($users)
    {
        $this->users = $users;
        return $this;
    }
}

When I try to retrieve the relation from the groups, it works great, but when I slect the users and try to get their group, Doctrine create some proxy objects and the result is an empty object with only the relationship key filled.

Can someone point me to the good direction ?

Here my code:

$query = $em->createQueryBuilder()
    ->select('u')
    ->from('Application\Model\User', 'u');

$data = $query->getQuery()->getResult();
$data = reset($data);
var_dump($data->getGroup()); // proxy

###########################

$query = $em->createQueryBuilder()
    ->select('g')
    ->from('Application\Model\User\Group', 'g');

$data = $query->getQuery()->getResult();
$data = reset($data);
var_dump($data->getUsers()); // ok
4
  • Why do you have an additional $GRP_ID in the User class?
    – andy
    Oct 7, 2014 at 12:54
  • Additional ? I only have one $GRP_ID to matche the user group
    – kitensei
    Oct 7, 2014 at 13:04
  • You already have specified GRP_ID in $group by @ORM\JoinColumn(name="GRP_ID", referencedColumnName="GRP_ID"). As for the proxy, it will automatically load the corresponding entity as required. Try $data->getGroup()->getCode(), for example.
    – andy
    Oct 7, 2014 at 13:43
  • I get that, but what I'd like to understant is why, in certain cases, I get some auto-generated proxies, and sometimes I get a real model object
    – kitensei
    Oct 7, 2014 at 13:50

1 Answer 1

0

you don't need the $GRP_ID in the User Entity - it's already mapped with the $group relation. Doctrine handles the IDs automatically. Also your naming-convention looks a bit weird. Normally you should use lowe-camel-case (to save you from strange errors)

Example: $USR_CODE should be $usrCode -> to match your getter/setter: setUsrCode(), getUsrCode().

just noticed: you don't have any setters. You have to define setters for your attributes, with the naming-convention (look at my example above)

Edit: you can map the column with the doctrine orm notation:

/**
*
* @ORM\Column(name="USR_CODE", type="string")
*/
private $usrCode;

And yes, you need setters otherwise doctrine won't be able to set the values.

You also need addUser() and removeUser() functions (since user is a Collection):

public function addUsers(Collection $users)
{
    foreach($users as $user)
    {
        if( ! $this->users->contains($user))
        {
            $this->users->add($user);
            $user->setGroup($this);
        }
    }
}

public function removeUsers(Collection $users)
{
    foreach($users as $user)
    {
        if($this->users->contains($user)){
            $this->users->remove($user);
            $user->setGroup(null);
        }
    }
}
6
  • I know that the naming convention is completely weird, but I'm mapping doctrine to an old oracle database that cannot be edited, so I'm just trying to adapt my code to the existing db. Setters are needed for Hydrator ? because ideally I'd rather not implement them as I'm using a views, which cannot be pushed
    – kitensei
    Oct 7, 2014 at 13:20
  • ok edited (you can see my code in edits), but I still get proxies instead of objects, even if I set the load to EAGER instead of LAZY, so I guess the problem comes from the relationship query retrieving some columns that aren't declared in my code, I'm a bit lost :/
    – kitensei
    Oct 7, 2014 at 13:38
  • thats normal behaviour, doctrine puts proxies where normally the associated object would be (I think thats for increasing performance), you should be able to use the proxy-object like the "real"-object
    – Sepultura
    Oct 7, 2014 at 14:05
  • note: you accidentally posted the Group-Entity in the User-entity
    – Sepultura
    Oct 7, 2014 at 14:08
  • also look at my second edit (add and remove functions)
    – Sepultura
    Oct 7, 2014 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.