问题描述
我很难找到正确的结果。
I am having a hard time finding the correct result for this.
我有一对一的映射。有两个表格:用户和users_settings
I have one to one mapping. There are two tables: users and users_settings
我有两个学说实体如下:
I have two doctrine entities as below:
/**
* @ORM\Table(name="users")
* @ORM\Entity
*/
class Users {
/**
* @ORM\OneToOne(targetEntity="UsersSettings", mappedBy="user",cascade={"persist"})
*/
private $userSetting;
//getters and setters
}
/**
* @ORM\Table(name="notifications_settings")
* @ORM\Entity
*/
Class UsersSettings {
/**
* @var Users
*
* @ORM\OneToOne(targetEntity="Users", inversedBy="userSetting")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
}
每当我提取实体一个如下:
Whenever I fetch entity one as below:
$q = $this
->createQueryBuilder('u')
->select('u, r')
->leftJoin('u.roles', 'r')
->where('u.username = :username OR u.email = :email')
->setParameter('username', $username)
->setParameter('email', $username)
->getQuery();
SELECT t0.id AS id1, t0.username AS username2, t0.email AS email3, t0.password AS password4, t29.id AS id30, t29.is_notify_by_email AS is_notify_by_email31, t29.user_id AS user_id32 FROM users t0 LEFT JOIN users_settings t29 ON t29.user_id = t0.id WHERE t0.id = ?
学说立即执行加入到我不想要的usersSettings实体。其他类型的映射像一对多和一对一执行延迟加载,但是在一对一映射的情况下,我无法配置为延迟加载。我如何懒惰加载这种关系?我正在使用教义2.3和symfony 2.1
Doctrine immediately performs join to usersSettings entity which I dont want. Other types of mapping like one to many and many to one performs lazy loading but in case of one to one mapping, I could not configure to lazy load. How can i lazy load this relation ? I am using doctrine 2.3 and symfony 2.1
推荐答案
使用提示HINT_FORCE_PARTIAL_LOAD避免延迟加载。
Use hint HINT_FORCE_PARTIAL_LOAD to avoid lazy-loading.
...
$qb->getQuery()->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
这篇关于懒惰加载原则一对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!