Проблема QueryBuilder с JOIN - PullRequest
1 голос
/ 15 мая 2019

У меня проблема с 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;
    }
}

1 Ответ

0 голосов
/ 15 мая 2019

Вам просто нужно добавить методы доступа к свойству 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;
}

Это должно решить вашу проблему

Редактировать:

Вы должны добавить геттер и сеттер дляАтрибут donations в сущности Donateur.

Еще один способ решения вашей проблемы для "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?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...