Ima*_*adT 1 entity doctrine object symfony
使用Symfony2/doctrine2,我们使用find()函数根据所选实体获取特定对象(如有关系)(如OneToMany),Doctrine返回所有其他对象.
例如 :
$em = $this->get(
'doctrine.orm.entity_manager',
$request->getSession()->get('entity_manager')
);
$product = $em->getRepository('MyBundle:Product')->find($id);
Run Code Online (Sandbox Code Playgroud)
$ product上的结果将是Product对象+其他链接对象,如(Store,Category,...等).
我们如何控制学说来确定我们需要返回哪个对象.
我可以使用Querybuilder,但我正在寻找是否有任何功能都确定.
Doctrine返回所有其他对象
这不是它的工作方式,至少在默认情况下如此.
Doctrine使用所谓的延迟加载.
从官方文档中,您有以下示例:
<?php
/** @Entity */
class Article
{
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string") */
private $headline;
/** @ManyToOne(targetEntity="User") */
private $author;
/** @OneToMany(targetEntity="Comment", mappedBy="article") */
private $comments;
public function __construct {
$this->comments = new ArrayCollection();
}
public function getAuthor() { return $this->author; }
public function getComments() { return $this->comments; }
}
$article = $em->find('Article', 1);
Run Code Online (Sandbox Code Playgroud)
以下解释如下:
而不是向您传回真实的Author实例和注释集合Doctrine将为您创建代理实例.只有当您第一次访问这些代理时,它们才会通过EntityManager并从数据库加载其状态.
参考:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#entity-object-graph-traversal
有关该主题的更多信息:http://www.doctrine-project.org/blog/doctrine-lazy-loading.html