Symfony 2-findall()数据库查询太多

Symfony 2-findall()数据库查询太多,symfony,doctrine-orm,Symfony,Doctrine Orm,如果我使用以下代码,为什么Symfony2执行40 DB查询: $em = $this->getDoctrine()->getManager(); $records = $em->getRepository('MyWebBundle:Highlight')->findAll(); 我认为findAll()方法只返回高亮显示实体中的所有项,而与其他实体的关联将替换代理对象。但是现在findAll()方法获取所有关联实体 你知道问题出在哪里吗 指数化 public func

如果我使用以下代码,为什么Symfony2执行40 DB查询:

$em = $this->getDoctrine()->getManager();
$records = $em->getRepository('MyWebBundle:Highlight')->findAll();
我认为
findAll()
方法只返回高亮显示实体中的所有项,而与其他实体的关联将替换代理对象。但是现在
findAll()
方法获取所有关联实体

你知道问题出在哪里吗

指数化

public function indexAction() {
    $em = $this->getDoctrine()->getManager();
    $records = $em->getRepository('MyWebBundle:Highlight')->findAll();

    $csrf = $this->get('security.csrf.token_manager');
    $token = $csrf->refreshToken(self::FORM_TOKEN_ID);

    $params = array(
        "data" => array(
            "all" => $records,
        ),
        "token" => $token->getValue(),
        "static" => array(
            "add" => $this->generateUrl("admin_highlight_add"),
            "edit" => $this->generateUrl("admin_highlight_edit"),
            "del" => $this->generateUrl("admin_highlight_del"),
        ),
    );
    $ser = $this->get('jms_serializer');
    $jsonContent = $ser->serialize($params, 'json');

    return array('jsonContent' => $jsonContent);
}
namespace My\WebBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * Highlight
 *
 * @JMS\ExclusionPolicy("none")
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\WebBundle\Entity\HighlightRepository")
 */
class Highlight {

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

    /**
     * @var string
     *
     * @ORM\Column(name="abbreviation", type="string", length=8, unique=true)
     */
    private $abbreviation;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=80, nullable=true)
     */
    private $description;

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

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Goods", mappedBy="highlight")
     */
    private $goods;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Calibration", mappedBy="highlight")
     */
    private $calibrations;

    /**
     * Constructor
     */
    public function __construct() {
        $this->goods = new \Doctrine\Common\Collections\ArrayCollection();
        $this->calibrations = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set abbreviation
     *
     * @param string $abbreviation
     * @return Highlight
     */
    public function setAbbreviation($abbreviation) {
        $this->abbreviation = $abbreviation;

        return $this;
    }

    /**
     * Get abbreviation
     *
     * @return string 
     */
    public function getAbbreviation() {
        return $this->abbreviation;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Highlight
     */
    public function setDescription($description) {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * Set color
     *
     * @param string $color
     * @return Highlight
     */
    public function setColor($color) {
        $this->color = $color;

        return $this;
    }

    /**
     * Get color
     *
     * @return string 
     */
    public function getColor() {
        return $this->color;
    }

    /**
     * Add goods
     *
     * @param \My\WebBundle\Entity\Goods $goods
     * @return Highlight
     */
    public function addGood(\My\WebBundle\Entity\Goods $goods) {
        $this->goods[] = $goods;

        return $this;
    }

    /**
     * Remove goods
     *
     * @param \My\WebBundle\Entity\Goods $goods
     */
    public function removeGood(\My\WebBundle\Entity\Goods $goods) {
        $this->goods->removeElement($goods);
    }

    /**
     * Get goods
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGoods() {
        return $this->goods;
    }

    /**
     * Add calibrations
     *
     * @param \My\WebBundle\Entity\Calibration $calibrations
     * @return Highlight
     */
    public function addCalibration(\My\WebBundle\Entity\Calibration $calibrations) {
        $this->calibrations[] = $calibrations;

        return $this;
    }

    /**
     * Remove calibrations
     *
     * @param \My\WebBundle\Entity\Calibration $calibrations
     */
    public function removeCalibration(\My\WebBundle\Entity\Calibration $calibrations) {
        $this->calibrations->removeElement($calibrations);
    }

    /**
     * Get calibrations
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCalibrations() {
        return $this->calibrations;
    }

}
突出显示实体
namespace My\WebBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * Highlight
 *
 * @JMS\ExclusionPolicy("none")
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\WebBundle\Entity\HighlightRepository")
 */
class Highlight {

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

    /**
     * @var string
     *
     * @ORM\Column(name="abbreviation", type="string", length=8, unique=true)
     */
    private $abbreviation;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=80, nullable=true)
     */
    private $description;

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

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Goods", mappedBy="highlight")
     */
    private $goods;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Calibration", mappedBy="highlight")
     */
    private $calibrations;

    /**
     * Constructor
     */
    public function __construct() {
        $this->goods = new \Doctrine\Common\Collections\ArrayCollection();
        $this->calibrations = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set abbreviation
     *
     * @param string $abbreviation
     * @return Highlight
     */
    public function setAbbreviation($abbreviation) {
        $this->abbreviation = $abbreviation;

        return $this;
    }

    /**
     * Get abbreviation
     *
     * @return string 
     */
    public function getAbbreviation() {
        return $this->abbreviation;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Highlight
     */
    public function setDescription($description) {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * Set color
     *
     * @param string $color
     * @return Highlight
     */
    public function setColor($color) {
        $this->color = $color;

        return $this;
    }

    /**
     * Get color
     *
     * @return string 
     */
    public function getColor() {
        return $this->color;
    }

    /**
     * Add goods
     *
     * @param \My\WebBundle\Entity\Goods $goods
     * @return Highlight
     */
    public function addGood(\My\WebBundle\Entity\Goods $goods) {
        $this->goods[] = $goods;

        return $this;
    }

    /**
     * Remove goods
     *
     * @param \My\WebBundle\Entity\Goods $goods
     */
    public function removeGood(\My\WebBundle\Entity\Goods $goods) {
        $this->goods->removeElement($goods);
    }

    /**
     * Get goods
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGoods() {
        return $this->goods;
    }

    /**
     * Add calibrations
     *
     * @param \My\WebBundle\Entity\Calibration $calibrations
     * @return Highlight
     */
    public function addCalibration(\My\WebBundle\Entity\Calibration $calibrations) {
        $this->calibrations[] = $calibrations;

        return $this;
    }

    /**
     * Remove calibrations
     *
     * @param \My\WebBundle\Entity\Calibration $calibrations
     */
    public function removeCalibration(\My\WebBundle\Entity\Calibration $calibrations) {
        $this->calibrations->removeElement($calibrations);
    }

    /**
     * Get calibrations
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCalibrations() {
        return $this->calibrations;
    }

}

突出显示存储库为空

我认为问题来自序列化程序。因为您序列化了Highlith,所以每个Highlith的属性都已序列化,这意味着将对重新发布的商品执行延迟查询,这些商品也将被序列化

namespace My\WebBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * Highlight
 *
 * @JMS\ExclusionPolicy("none")
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\WebBundle\Entity\HighlightRepository")
 */
class Highlight {

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

    /**
     * @var string
     *
     * @ORM\Column(name="abbreviation", type="string", length=8, unique=true)
     */
    private $abbreviation;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=80, nullable=true)
     */
    private $description;

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

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Goods", mappedBy="highlight")
     */
    private $goods;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Calibration", mappedBy="highlight")
     */
    private $calibrations;

    /**
     * Constructor
     */
    public function __construct() {
        $this->goods = new \Doctrine\Common\Collections\ArrayCollection();
        $this->calibrations = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set abbreviation
     *
     * @param string $abbreviation
     * @return Highlight
     */
    public function setAbbreviation($abbreviation) {
        $this->abbreviation = $abbreviation;

        return $this;
    }

    /**
     * Get abbreviation
     *
     * @return string 
     */
    public function getAbbreviation() {
        return $this->abbreviation;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Highlight
     */
    public function setDescription($description) {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * Set color
     *
     * @param string $color
     * @return Highlight
     */
    public function setColor($color) {
        $this->color = $color;

        return $this;
    }

    /**
     * Get color
     *
     * @return string 
     */
    public function getColor() {
        return $this->color;
    }

    /**
     * Add goods
     *
     * @param \My\WebBundle\Entity\Goods $goods
     * @return Highlight
     */
    public function addGood(\My\WebBundle\Entity\Goods $goods) {
        $this->goods[] = $goods;

        return $this;
    }

    /**
     * Remove goods
     *
     * @param \My\WebBundle\Entity\Goods $goods
     */
    public function removeGood(\My\WebBundle\Entity\Goods $goods) {
        $this->goods->removeElement($goods);
    }

    /**
     * Get goods
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGoods() {
        return $this->goods;
    }

    /**
     * Add calibrations
     *
     * @param \My\WebBundle\Entity\Calibration $calibrations
     * @return Highlight
     */
    public function addCalibration(\My\WebBundle\Entity\Calibration $calibrations) {
        $this->calibrations[] = $calibrations;

        return $this;
    }

    /**
     * Remove calibrations
     *
     * @param \My\WebBundle\Entity\Calibration $calibrations
     */
    public function removeCalibration(\My\WebBundle\Entity\Calibration $calibrations) {
        $this->calibrations->removeElement($calibrations);
    }

    /**
     * Get calibrations
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCalibrations() {
        return $this->calibrations;
    }

}
然后,您应该通过向高亮显示的商品属性添加注释来防止这种行为,如下所示

use ...
use JMS\SerializerBundle\Annotation\ExclusionPolicy;
use JMS\SerializerBundle\Annotation\Exclude;

/**
 * ...
 * @ExclusionPolicy("none")
 */
 class Highlight
{

   /**
    * ...
    * @Exclude
    */
   private $goods;

}

您可以从

中获得有关排除策略的更多详细信息。findAll本身不执行许多查询。当您通过getter访问相关实体时,将执行查询。由于关系不是急切地获取的,所以它们第一次是在您访问它们时获取的

我认为序列化程序可以访问所有子对象来发送对象

只要手头有托管实体实例,就可以遍历 并使用该实体的任何关联,这些关联被配置为 他们已经在记忆中了。条令将自动加载 通过延迟加载的概念按需关联对象

要防止这种情况,请禁用子序列化或使用fetch-EAGER或构建一个DQL查询,该查询将预取所有子项以及父项,如(仅示例,无效DQL)


您说过它可以执行40个查询。但这些疑问是什么?它们都一样吗?我们能有一个突出的实体吗?以及存储库?嗨,谢谢你的反应。Highlight与商品有很多关联。货物与校准有许多联系。这些查询用于获取这些关联实体。但我只想获取所有突出显示的项目。这就是为什么我希望您使用实体定义更新您的问题,并突出显示回购协议的代码;)现在是我的问题更新。我添加了突出显示实体indexAction,它执行40db查询。如果我用
$em->getRepository('MyWebBundle:Highlight')->findAll()注释行执行了一个查询。是的,序列化程序中存在问题。我添加了注释,现在可以了。非常感谢:)没问题,这是一个令人愉快的问题。问题确实出在序列化程序中,我解决了这个问题,为排除关系实体添加注释。谢谢你的回答。始终考虑延迟加载。当你思考的时候,这是一件好事。一旦忘记,您将收到数千个请求:)