0

我有两个实体用户和个人资料具有一对一的关系。

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
$qb->add('select', 'u')
   ->add('from', '\Acme\TestBundle\Entity\User u')
   ->add('orderBy', 'u.id DESC');
$query = $qb->getQuery();
$customer = $query->execute();

当我检查 Symfony 分析器中的查询数量时,我可以看到在 Profile 表上为User 表中的n 个用户触发的查询的n个数。有什么方法可以停止对 Profile 表的查询。

如果有更好的实施方法,请告诉我。

提前致谢

添加的实体类

class User
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;

    /**
     * @var Acme\TestBundle\Entity\Profile
     * 
     * @ORM\OneToOne(targetEntity="Acme\TestBundle\Entity\Profile", mappedBy="user")
     */
    private $profile;

}

class Profile
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer $user_id
     *
     * @ORM\Column(name="user_id", type="integer")
     */
    private $user_id;

    /**
     * @var string $user_name
     *
     * @ORM\Column(name="user_name", type="string", length=100)
     */
    private $user_name;

    /**
     * @var Acme\TestBundle\Entity\User
     * 
     * @ORM\OneToOne(targetEntity="Acme\TestBundle\Entity\User", inversedBy="profile")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;    

}

来自 mysql 日志的响应

120110 15:14:29    89 Connect   root@localhost on test
           89 Query SET NAMES UTF8
           89 Query SELECT c0_.id AS id0, c0_.email AS email1, c0_.password AS password2, c0_.is_demo_user AS is_demo_user3, c0_.status AS status4, c0_.current_service AS current_service5, c0_.registration_mode AS registration_mode6, c0_.verification_code AS verification_code7, c0_.account_type AS account_type8, c0_.activated_date AS activated_date9, c0_.status_updated_at AS status_updated_at10, c0_.created_at AS created_at11, c0_.updated_at AS updated_at12 FROM user c0_ WHERE c0_.id = 1 ORDER BY c0_.email ASC
           89 Query SELECT t0.id AS id1, t0.user_id AS user_id2, t0.user_name AS user_name3, t0.age AS age4, t0.created_at AS created_at5, t0.updated_at AS updated_at6, t0.user_id AS user_id7 FROM profile t0 WHERE t0.user_id = '1'
           89 Quit  
4

1 回答 1

0

Your answer is here!!

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html

"In many cases associations between entities can get pretty large. Even in a simple scenario like a blog. where posts can be commented, you always have to assume that a post draws hundrets of comments. In Doctrine 2.0 if you accessed an association it would always get loaded completly into memory. This can lead to pretty serious performance problems, if your associations contain several hundrets or thousands of entities.

With Doctrine 2.1 a feature called Extra Lazy is introduced for associations. Associations are marked as Lazy by default, which means the whole collection object for an association is populated the first time its accessed. If you mark an association as extra lazy the following methods on collections can be called without triggering a full load of the collection:"

<?php
namespace Doctrine\Tests\Models\CMS;

/**
 * @Entity
 */
class CmsGroup
{
    /**
     * @ManyToMany(targetEntity="CmsUser", mappedBy="groups", fetch="EXTRA_LAZY")
    */
    public $users;
}

It's a bit late, but it might help others out there!

于 2012-09-14T09:29:31.037 回答