我有以下内容:
用户有一个组,组可以有多个用户
用户
<?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;
}
}
组
<?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;
}
}
当我尝试从组中检索关系时,它工作得很好,但是当我选择用户并尝试获取他们的组时,Doctrine会创建一些代理对象,结果是一个只填充了关系键的空对象。
有人能指出我的好方向吗?
这是我的代码:
$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
答案 0 :(得分:0)
您不需要用户实体中的$ GRP_ID - 它已经与$ group关系映射。 Doctrine自动处理ID。 你的命名约定也看起来有点奇怪。通常你应该使用lowe-camel-case(以避免出现奇怪的错误)
实施例: $ USR_CODE应为$ usrCode - &gt;匹配你的getter / setter:setUsrCode(),getUsrCode()。
刚才注意到:你没有任何二传手。您必须使用命名约定(请参阅上面的示例)
为您的属性定义setter编辑: 您可以使用doctrine orm表示法映射列:
/**
*
* @ORM\Column(name="USR_CODE", type="string")
*/
private $usrCode;
是的,你需要设置者,否则教义将无法设置值。
您还需要addUser()和removeUser()函数(因为user是一个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);
}
}
}