【问题标题】:QueryBuilder issue with JOINJOIN 的 QueryBuilder 问题
【发布时间】:2019-05-15 08:40:24
【问题描述】:

我对 Doctrine queryBuilder 有疑问。 我有两个实体:捐赠和捐赠者。我只想做一个左连接(例如,因为 Donations.donorId 指的是 Donateurs.id。)

DonationsRepository 上的查询:

public function showAll()
    {
        $qb = $this->createQueryBuilder('dr')
            ->select('dr, dn')
            ->leftJoin('dr.donateurs','dn')
            ->getQuery()
            ->execute();

        return $qb;
    }

调用我的 Controller.php :

public function don(DonationsRepository $repo, Request $request)
    {
        $qb = $repo->showAll();
        var_dump($qb);

        // Then return to Twig
    }

这是一个var_dump 示例:

array (size=14)
  0 => 
    object(App\Entity\Donations)[989]
      private 'id' => int 197
      private 'movement' => string 'D' (length=1)
      private 'departDate' => 
        object(DateTime)[885]
          public 'date' => string '2023-11-18 00:00:00.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Paris' (length=12)
      private 'certifDate' => 
        object(DateTime)[993]
          public 'date' => string '2023-11-18 00:00:00.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Paris' (length=12)
      private 'certifRef' => string 'EO62' (length=4)
      private 'productName' => string 'Clémentines' (length=12)
      private 'donorId' => int 1
      private 'family' => string 'Frais' (length=5)
      private 'recipient' => string 'EO62' (length=4)
      private 'carrierId' => int 3
      private 'nbPallet' => int 33
      private 'weight' => int 19720
      private 'billDate' => 
        object(DateTime)[891]
          public 'date' => string '2023-11-18 00:00:00.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Paris' (length=12)
      private 'billReceptionDate' => 
        object(DateTime)[992]
          public 'date' => string '2023-11-18 00:00:00.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Paris' (length=12)
      private 'billSentValidationDate' => 
        object(DateTime)[991]
          public 'date' => string '2023-11-18 00:00:00.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Paris' (length=12)
      private 'billReference' => string '23-11-18' (length=8)
      private 'price_HT' => int 11
      private 'donateurs' => null

所有数据仅来自我的捐赠实体.... 但是如果我从我的存储库中删除execute(),然后我从我的控制器中执行$qb->execute...var_dump 向我返回类似的东西(这是一个示例,转储很大)

var_dump($qb)

public 'declaringClasses' => 
        array (size=32)
          'id_0' => string 'App\Entity\Donations' (length=20)
          'movement_1' => string 'App\Entity\Donations' (length=20)
          'depart_date_2' => string 'App\Entity\Donations' (length=20)
          'certif_date_3' => string 'App\Entity\Donations' (length=20)
          'certif_ref_4' => string 'App\Entity\Donations' (length=20)
          'product_name_5' => string 'App\Entity\Donations' (length=20)
          'donor_id_6' => string 'App\Entity\Donations' (length=20)
          'family_7' => string 'App\Entity\Donations' (length=20)
          'recipient_8' => string 'App\Entity\Donations' (length=20)
          'carrier_id_9' => string 'App\Entity\Donations' (length=20)
          'nb_pallet_10' => string 'App\Entity\Donations' (length=20)
          'weight_11' => string 'App\Entity\Donations' (length=20)
          'bill_date_12' => string 'App\Entity\Donations' (length=20)
          'bill_reception_date_13' => string 'App\Entity\Donations' (length=20)
          'bill_sent_validation_date_14' => string 'App\Entity\Donations' (length=20)
          'bill_reference_15' => string 'App\Entity\Donations' (length=20)
          'price_ht_16' => string 'App\Entity\Donations' (length=20)
          'id_17' => string 'App\Entity\Donateurs' (length=20)
          'enterprise_18' => string 'App\Entity\Donateurs' (length=20)
          'social_address_19' => string 'App\Entity\Donateurs' (length=20)
          'siret_20' => string 'App\Entity\Donateurs' (length=20)
          'postal_adress_21' => string 'App\Entity\Donateurs' (length=20)
          'enterprise_type_22' => string 'App\Entity\Donateurs' (length=20)
          'dpt_23' => string 'App\Entity\Donateurs' (length=20)
          'product_24' => string 'App\Entity\Donateurs' (length=20)
          'product_type_25' => string 'App\Entity\Donateurs' (length=20)
          'contact_name_26' => string 'App\Entity\Donateurs' (length=20)
          'contact_function_27' => string 'App\Entity\Donateurs' (length=20)
          'phone_28' => string 'App\Entity\Donateurs' (length=20)
          'email_29' => string 'App\Entity\Donateurs' (length=20)
          'national_provider_30' => string 'App\Entity\Donateurs' (length=20)
          'prospector_adviser_31' => string 'App\Entity\Donateurs' (length=20)

这两个实体似乎映射在正确的字段上! 但是现在,我无法发送到树枝,在我看来一切都是空的(没有错误消息)。

这让我从 3 天开始就疯了。 需要帮助,对不起我的英语。

更新 实体。

捐赠者:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\DonateursRepository")
 */
class Donateurs
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $socialAddress;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $siret;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $postalAdress;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $enterprise_type;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $dpt;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $product;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $productType;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $contactName;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $contactFunction;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $phone;

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

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $nationalProvider;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $prospectorAdviser;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Donations", mappedBy="donateurs")
     */
    private $donations;

    public function __construct()
    {
        $this->donations = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEnterprise(): ?string
    {
        return $this->enterprise;
    }

    public function setEnterprise(string $enterprise): self
    {
        $this->enterprise = $enterprise;

        return $this;
    }

    public function getSocialAddress(): ?string
    {
        return $this->socialAddress;
    }

    public function setSocialAddress(?string $socialAddress): self
    {
        $this->socialAddress = $socialAddress;

        return $this;
    }

    public function getSiret(): ?int
    {
        return $this->siret;
    }

    public function setSiret(?int $siret): self
    {
        $this->siret = $siret;

        return $this;
    }

    public function getPostalAdress(): ?string
    {
        return $this->postalAdress;
    }

    public function setPostalAdress(?string $postalAdress): self
    {
        $this->postalAdress = $postalAdress;

        return $this;
    }

    public function getEnterpriseType(): ?string
    {
        return $this->enterprise_type;
    }

    public function setEnterpriseType(?string $enterprise_type): self
    {
        $this->enterprise_type = $enterprise_type;

        return $this;
    }

    public function getDpt(): ?int
    {
        return $this->dpt;
    }

    public function setDpt(?int $dpt): self
    {
        $this->dpt = $dpt;

        return $this;
    }

    public function getProduct(): ?string
    {
        return $this->product;
    }

    public function setProduct(?string $product): self
    {
        $this->product = $product;

        return $this;
    }

    public function getProductType(): ?string
    {
        return $this->productType;
    }

    public function setProductType(?string $productType): self
    {
        $this->productType = $productType;

        return $this;
    }

    public function getContactName(): ?string
    {
        return $this->contactName;
    }

    public function setContactName(?string $contactName): self
    {
        $this->contactName = $contactName;

        return $this;
    }

    public function getContactFunction(): ?string
    {
        return $this->contactFunction;
    }

    public function setContactFunction(?string $contactFunction): self
    {
        $this->contactFunction = $contactFunction;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(?string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(?string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getNationalProvider(): ?string
    {
        return $this->nationalProvider;
    }

    public function setNationalProvider(?string $nationalProvider): self
    {
        $this->nationalProvider = $nationalProvider;

        return $this;
    }

    public function getProspectorAdviser(): ?string
    {
        return $this->prospectorAdviser;
    }

    public function setProspectorAdviser(?string $prospectorAdviser): self
    {
        $this->prospectorAdviser = $prospectorAdviser;

        return $this;
    }
}

捐款:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Entity(repositoryClass="App\Repository\DonationsRepository")
 */
class Donations
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $departDate;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $certifDate;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    private $certifRef;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $productName;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $donorId;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $family;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    private $recipient;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $carrierId;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $nbPallet;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $weight;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $billDate;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $billReceptionDate;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $billSentValidationDate;

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

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $price_HT;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Donateurs", inversedBy="donations")
     */
    private $donateurs;

    public function __construct()
    {
        $this->donateurs = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getProductName(): ?string
    {
        return $this->productName;
    }

    public function setProductName(string $productName): self
    {
        $this->productName = $productName;

        return $this;
    }

    public function getNbPallet(): ?int
    {
        return $this->nbPallet;
    }

    public function setNbPallet(int $nbPallet): self
    {
        $this->nbPallet = $nbPallet;

        return $this;
    }

    public function getMovement(): ?string
    {
        return $this->movement;
    }

    public function setMovement(string $movement): self
    {
        $this->movement = $movement;

        return $this;
    }

    public function getDepartDate(): ?\DateTimeInterface
    {
        return $this->departDate;
    }

    public function setDepartDate(\DateTimeInterface $departDate): self
    {
        $this->departDate = $departDate;

        return $this;
    }

    public function getCertifDate(): ?\DateTimeInterface
    {
        return $this->certifDate;
    }

    public function setCertifDate(\DateTimeInterface $certifDate): self
    {
        $this->certifDate = $certifDate;

        return $this;
    }

    public function getCertifRef(): ?string
    {
        return $this->certifRef;
    }

    public function setCertifRef(?string $certifRef): self
    {
        $this->certifRef = $certifRef;

        return $this;
    }

    public function getFamily(): ?string
    {
        return $this->family;
    }

    public function setFamily(string $family): self
    {
        $this->family = $family;

        return $this;
    }

    public function setContactId(int $contactId): self
    {
        $this->contactId = $contactId;

        return $this;
    }

    public function getRecipient(): ?string
    {
        return $this->recipient;
    }

    public function setRecipient(string $recipient): self
    {
        $this->recipient = $recipient;

        return $this;
    }

    public function getCarrierId(): ?int
    {
        return $this->carrierId;
    }

    public function setCarrierId(int $carrierId): self
    {
        $this->carrierId = $carrierId;

        return $this;
    }

    public function getWeight(): ?int
    {
        return $this->weight;
    }

    public function setWeight(int $weight): self
    {
        $this->weight = $weight;

        return $this;
    }

    public function getBillDate(): ?\DateTimeInterface
    {
        return $this->billDate;
    }

    public function setBillDate(\DateTimeInterface $billDate): self
    {
        $this->billDate = $billDate;

        return $this;
    }

    public function getBillReceptionDate(): ?\DateTimeInterface
    {
        return $this->billReceptionDate;
    }

    public function setBillReceptionDate(\DateTimeInterface $billReceptionDate): self
    {
        $this->billReceptionDate = $billReceptionDate;

        return $this;
    }

    public function getBillSentValidationDate(): ?\DateTimeInterface
    {
        return $this->billSentValidationDate;
    }

    public function setBillSentValidationDate(?\DateTimeInterface $billSentValidationDate): self
    {
        $this->billSentValidationDate = $billSentValidationDate;

        return $this;
    }

    public function getBillReference(): ?string
    {
        return $this->billReference;
    }

    public function setBillReference(?string $billReference): self
    {
        $this->billReference = $billReference;

        return $this;
    }

    public function getPriceHT(): ?int
    {
        return $this->price_HT;
    }

    public function setPriceHT(?int $price_HT): self
    {
        $this->price_HT = $price_HT;

        return $this;
    }

    public function getDonorId(): ?int
    {
        return $this->donorId;
    }

    public function setDonorId(int $donorId): self
    {
        $this->donorId = $donorId;

        return $this;
    }

    public function getDonateurs(): ?Collection
    {
        return $this->donateurs;
    }

    public function setDonateurs(?Donateurs $donateurs): self
    {
        $this->donateurs = $donateurs;
        return $this;
    }
}

【问题讨论】:

  • 我认为你不需要去取捐赠者。它们将在需要时自动加载。您是否尝试使用 $repo->findAll();在控制器中和 {% for donateur indonate.donateurs %} {{ donateur.name }} {% endfor %} with twig?
  • 聪明,谢谢:)。但它不起作用......当我尝试它时出现错误(在 {% for 捐赠中的捐赠 %} 内,对吗?)这是错误:“既不是属性“捐赠者”也不是其中一种方法“ donateurs()"、"getdonateurs()"/"isdonateurs()"/"hasdonateurs()" 或 "__call()" 存在并且在“App\Entity\Donations”类中具有公共访问权限
  • 那么你有一个功能可以让捐赠者进入捐赠类吗?你是如何建立这种关系的?发布捐赠课程真的很有帮助。
  • 您能向我们展示您的捐赠实体吗?里面可能有东西坏了
  • @Vyctorya,谢谢,它已经更新,现在你可以看到实体了。

标签: php symfony doctrine symfony4 query-builder


【解决方案1】:

您只需将访问器添加到属性donateurs

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Donateurs", inversedBy="donations")
 */
private $donateurs;

public function getDonateurs()
{
    return $this->donateurs;
}

public function setDonateurs($donateurs)
{
    $donateurs = $this->donateurs;

    return $this;
}

应该能解决你的问题

编辑:

您也应该在Donateur 实体中为donations 属性添加getter 和setter。

解决“showAll”问题的另一种方法是使用学说的存储库方法:

// You should suffix any action of your controller with "Action"
public function donAction(DonationsRepository $repo, Request $request)
{
    $results = $repo->findAll(); //
    var_dump($results);

    // Then return to Twig
}

您可以通过以下方式访问您的树枝中的donateurs

{{ donation.donateurs }}

通过保留您的方法,如果您尝试通过在存储库方法中执行操作来强制关联水合会发生什么:

public function showAll()
{
    $results = $this->createQueryBuilder('dr')
        ->select('dr, dn')
        ->leftJoin('dr.donateurs','dn')
        ->getQuery()
        ->execute([], Query::HYDRATE_OBJECT);

    return $results;
}

如果这仍然不起作用,您确定在您的数据库中有一个 donateur_id 在您的 donation 行和您的 donateur 表中的相应行吗?

【讨论】:

  • 感谢您的回答。是的,这是一个错误,没有吸气剂,不能工作,我的错。但这并不能解决我的问题。当我var_dump我的查询时,属性“捐赠者”仍然为空,就像我的第一篇文章一样。
  • 你是否在Donateur实体中添加了getter和setter?他们也失踪了
  • 我……很困惑。就像你和 Vyctorya 说的那样,问题在于 setter 和 getter。但是,经过 3 天的测试,我不小心删除了捐助者 ID 的数据。所以,没有 id,这是行不通的。现在一切都很好。我不能 +1 你的回答(我的声望
  • 很高兴知道:) 我之前编辑过我的答案,而不是看到你的评论。我在其中添加了一些提示来帮助您(注释和变量名称)。看来你还是误解了一些概念;)
  • 是的,谢谢,我刚刚看到 :)。因为这个问题(当然也因为你),我学到了很多关于实体关系和 Doctrine ORM 哲学的知识。 “Merci l'ami!A un de ces jours sur Lyon peut-être x)”。
猜你喜欢
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多