学说做太多的查询

时间:2019-04-09 08:50:39

标签: php symfony doctrine-orm dql

我有多个实体,当我执行findAll时,一个实体上只有1个请求

/**
 * @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
 * @ORM\Table(name="property")
 */
class Property
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 * @Assert\Type("integer")
 * @var int
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Choice({"Appartement", "Maison", "Garage", "Bureau", "Château", "Commerce"})
 * @var string
 */
private $propertyCategory;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Type("string")
 * @var string
 */
private $uniqueName;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Type("string")
 * @var string
 */
private $address;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Type("string")
 * @var string
 */
private $city;

/**
 * @ORM\Column(type="integer")
 * @Assert\NotBlank
 * @Assert\Type("integer")
 * @Assert\Length(min = 5, minMessage = "Ce champ doit contenir 5 chiffres")
 * @Assert\Length(max = 5, maxMessage = "Ce champ doit contenir 5 chiffres")
 * @var int
 */
private $zipCode;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Country
 * @var string
 */
private $country;

/**
 * @ORM\Column(type="integer")
 * @Assert\NotBlank
 * @Assert\Type("integer")
 * @var string
 */
private $surfaceInSquareMeter;

/**
 * @ORM\Column(type="integer")
 * @Assert\NotBlank
 * @Assert\Type("integer")
 * @var int
 */
private $numberOfPiece;

/**
 * @ORM\Column(type="text", nullable=true)
 * @Assert\Type("string")
 * @var string
 */
private $description;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Choice({"Meublé", "Non meublé"})
 * @var string
 */
private $rentalCategory;

/**
 * @ORM\Column(type="float")
 * @Assert\NotBlank
 * @Assert\Type("float")
 * @var float
 */
private $rentExcludingCharges;

/**
 * @ORM\Column(type="float")
 * @Assert\NotBlank
 * @Assert\Type("float")
 * @var float
 */
private $charges;

/**
 * @ORM\Column(type="float")
 * @Assert\NotBlank
 * @Assert\Type("float")
 * @var float
 */
private $purchasePrice;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="properties")
 * @JoinColumn(name="user_property_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $userProperty;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Equipment", mappedBy="equipment")
 */
private $equipment;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Lessee", mappedBy="lessee")
 */
private $lessees;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 * @Assert\File(
 *     maxSize = "2000k",
 *     mimeTypes = {"application/pdf", "application/x-pdf"},
 *     mimeTypesMessage = "Choisisez un fichier PDF"
 * )
 */
private $pdfFile;

我不向您显示getter / setter,因为它超过500行,但是我生成了它们。

Doctrine对每个属性进行1个查询。为了解决我的问题,我尝试这样做:

/**
 * @param User $user
 * @return array
 */
public function findPropertyByUser(User $user): array
{
    $qb = $this->createQueryBuilder('p')
        ->andWhere('p.userProperty = :user')
        ->setParameter('user', $user)
        ->orderBy('p.id', 'ASC')
        ->getQuery();
    return $qb->execute();
}

但是它没有任何改变,我也尝试使用findBy,但是它没有任何改变。谁能告诉我我的问题来自哪里?

编辑: 根据评论,这可能是n + 1问题,

我尝试了不同的方法,但是没有找到解决方案,这是我的实际方法:

/**
 * @param User $user
 * @return array
 */
public function findPropertyByUser(User $user): array
{
    $qb = $this->createQueryBuilder('p')
        ->andWhere('p.userProperty = :user')
        ->setParameter('user', $user)
        ->orderBy('p.id', 'ASC')
        ->innerJoin('p.userProperty', 'u')
        ->addSelect('u')
        ->getQuery();
    return $qb->execute();
}
我认为是

最后个请求

{% if properties is defined %}
            {% for property in properties %}

                    {% set emails = [property.userProperty.email] %}
                    {% for lessee in property.lessees.values %}
                        {% set emails = emails|merge([lessee.email]) %}
                    {% endfor %}

                    {% if app.user.email in emails %}
                        <tr>
                            <td>{{ property.propertyCategory }}</td>
                            <td>{{ property.uniqueName }}</td>
                            <td>{{ property.address }}</td>
                            <td>{{ property.city }}</td>
                            <td>{{ property.zipCode }}</td>
                            <td>{{ property.rentExcludingCharges }} €</td>
                            <td>{{ property.charges }} €</td>
                            <td>{{ property.purchasePrice }} €</td>
                            <td>
                                <a href="{{ path('property_show', {'id': property.id}) }}">Voir</a>
                                {% if is_granted('ROLE_USER') %}
                                    <a href="{{ path('property_edit', {'id': property.id}) }}"><i class="fas fa-edit"></i> édition</a>
                                {% endif %}
                            </td>
                        </tr>
                    {% endif %}
            {% endfor %}
        {% else %}
            <tr>
                <td colspan="16">Vous n'avez pas enregistré de propriétés</td>
            </tr>
        {% endif %}

开始解决方案:我的请求量减少了

/**
 * @param User $user
 * @return array
 */
public function findPropertyByUser(User $user): array
{
    $qb = $this->createQueryBuilder('p')
        ->andWhere('p.userProperty = :user')
        ->setParameter('user', $user)
        ->orderBy('p.id', 'ASC')
        ->innerJoin('p.lessees', 'l')
        ->getQuery();
    return $qb->execute();
}

1 个答案:

答案 0 :(得分:2)

您需要一个leftJoin之类的

argv

,然后在视图中删除有关电子邮件的所有内容:

/**
 * @param User $user
 * @return array
 */
public function findPropertyByUser(User $user): array
{
    $qb = $this->createQueryBuilder('p')
        ->leftJoin('p.lessees', 'l')
        ->andWhere('p.userProperty = :user OR l.email = :userEmail')
        ->setParameter('user', $user)
        ->setParameter('userEmail', $user->getEmail())
        ->orderBy('p.id', 'ASC')
        ->getQuery();

    return $qb->execute();
}