我有这个实体:
/**
* User state.
*
* @ORM\Entity(repositoryClass="User\Repository\StateRepository")
* @ORM\Table(name="user_state")
*/
class State implements StateInterface
{
/** ONE-TO-ONE BIDIRECTIONAL, OWNING SIDE
* @var User
* @ORM\Id
* @ORM\OneToOne(targetEntity="User\Entity\User", inversedBy="state")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/** ONE-TO-ONE UNIDIRECTIONAL
* @var \Application\Entity\State
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Application\Entity\State", fetch="EAGER")
* @ORM\JoinColumn(name="state_id", referencedColumnName="id")
*/
protected $state;
public function __construct(User $user, \Application\Entity\State $state)
{
$this->user = $user;
$this->state = $state;
}
/********/
/* USER */
/********/
/**
* Get user
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set user
*
* @param User $user
*
* @return Email
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**********/
/* STATUS */
/**********/
/**
* Get state
*
* @return \Application\Entity\State
*/
public function getState()
{
return $this->state;
}
/**
* Set state
*
* @param \Application\Entity\State $state
*
* @return State
*/
public function setState(\Application\Entity\State $state)
{
$this->state = $state;
return $this;
}
/******/
/* ID */
/******/
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->state->getId();
}
/**
* Set id.
*
* @param int $id
*
* @return State
*/
public function setId($id)
{
$this->state->setId((int) $id);
return $this;
}
/********/
/* NAME */
/********/
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->state->getName();
}
/**
* Set name
*
* @param string $name
*
* @return State
*/
public function setName($name)
{
$this->state->setName((string) $name);
return $this;
}
}
要获取实体,我在自定义存储库中执行此操作
return self::findOneBy($params);
这导致此查询:
SELECT t0.user_id AS user_id1, t0.state_id AS state_id2, t3.id AS id4, t3.name AS name5 FROM user_state t0 LEFT JOIN state t3 ON t0.state_id = t3.id WHERE t0.user_id = ? LIMIT 1
完美!正是因为fetch="EAGER"
关系,所以我急切地想要了解相关实体。
现在我想用DQL实现相同的功能,所以我这样做:
$query = $this->_em->createQuery("SELECT us, s FROM 'User\Entity\State' us LEFT JOIN us.state s WHERE us.user = :user_id");
$query->setParameters($params);
$query->setFetchMode('User\Entity\State', 'state', ClassMetadata::FETCH_EAGER);
return $query->getOneOrNullResult();
我收到一条错误消息:Call to a member function getId() on a non-object
显然相关实体没有正确加载。当我手动检查时,$state
原来是NULL
如果我在查询中省略s,就像这样:
$query = $this->_em->createQuery("SELECT us FROM 'User\Entity\State' us LEFT JOIN us.state s WHERE us.user = :user_id");
然后它按照我想要的方式返回结果,但它会产生两个查询(另一个查询来获取相关的状态实体),这显然是有意义的,因为查询不被视为fetch join
任何更长,所以懒惰加载相关实体。
如何使用findOneBy
方法获得相同的结果(一个查询,一个结果),而不是使用DQL?
答案 0 :(得分:0)
我刚刚将Doctrine2 ORM更新为2.5版,现在它正在运行:)
我当前的composer.json文件中的Doctrine元素如下所示:
"doctrine/doctrine-module":"dev-master",
"doctrine/doctrine-orm-module": "dev-master",
"doctrine/dbal": "2.5.*@dev",
"doctrine/orm": "2.5.*@dev",