为什么我在Symfony实体关系中得到未定义的索引“ company”

时间:2019-02-16 20:08:37

标签: symfony symfony4

我在这里有点困惑,我列出了几类,只简化为所需的部分:

class Instruction{

    /**
     * @ORM\Column(nullable=true)
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="instructions")
     */
    private $company;
}

class Company{
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Instruction", mappedBy="company")
     */
    private $instructions;

    public function getInstructions(){
        return $this->instructions;
    }
}

我是Symfony的新手,所以也许我犯错了,但是公司应该可以有很多说明。

当我调用$ company-> getInstructions()时,出现以下错误:

注意:未定义索引:公司

因此,在使用原则拉动所有公司之后,我在循环使用foreach,这是$ company的来源。

我可能在这里误解了一些东西,但是参数$ company显然位于指令类中。

控制器代码如下:

$em = $this->getDoctrine();
$repo = $em->getRepository(Company::class);
$companies = $repo->findAll();

foreach($companies as $company){
    $company->getInstructions(); // error happens here
}

完整的公司实体:

<?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\CompanyRepository")
 */
class Company
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $ReceiveDailySummaries = false;

    /**
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $EnableHitCounter = false;

    /**
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $AllowDisposalChoice = false;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Instruction", mappedBy="company")
     */
    private $instructions;

    /**
     * Company constructor.
     */
    public function __construct()
    {
        $this->instructions = new ArrayCollection();
    }

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return string|null
     */
    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return Company
     */
    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getEmail(): ?string
    {
        return $this->email;
    }

    /**
     * @param string|null $email
     * @return Company
     */
    public function setEmail(?string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * @return bool|null
     */
    public function getReceiveDailySummaries(): ?bool
    {
        return $this->ReceiveDailySummaries;
    }

    /**
     * @param bool $ReceiveDailySummaries
     * @return Company
     */
    public function setReceiveDailySummaries(bool $ReceiveDailySummaries): self
    {
        $this->ReceiveDailySummaries = $ReceiveDailySummaries;

        return $this;
    }

    /**
     * @param string $trueText
     * @param string $falseText
     * @return string
     */
    public function receivesDailySummariesString($trueText='Yes',$falseText='No') : string{

        return ($this->receivesDailySummaries() ? $trueText : $falseText);
    }

    /**
     * @return bool
     */
    public function receivesDailySummaries() : bool{

        return $this->getReceiveDailySummaries();
    }

    /**
     * @return bool|null
     */
    public function getEnableHitCounter(): ?bool
    {
        return $this->EnableHitCounter;
    }

    /**
     * @param bool $EnableHitCounter
     * @return Company
     */
    public function setEnableHitCounter(bool $EnableHitCounter): self
    {
        $this->EnableHitCounter = $EnableHitCounter;

        return $this;
    }

    /**
     * @return bool
     */
    public function isHitCounterEnabled() : bool{

        return $this->getEnableHitCounter();
    }

    /**
     * @param string $trueText
     * @param string $falseText
     * @return string
     */
    public function hitCounterEnabledString($trueText='Yes',$falseText='No') : string{


        return ($this->isHitCounterEnabled() ? $trueText : $falseText);
    }

    /**
     * @return bool|null
     */
    public function getAllowDisposalChoice(): ?bool
    {
        return $this->AllowDisposalChoice;
    }


    /**
     * @param bool $AllowDisposalChoice
     * @return Company
     */
    public function setAllowDisposalChoice(bool $AllowDisposalChoice): self
    {


        $this->AllowDisposalChoice = $AllowDisposalChoice;

        return $this;
    }

    /**
     * @return bool
     */
    public function isAllowedDisposalChoice() : bool{

        return $this->getAllowDisposalChoice();
    }

    /**
     * @param string $trueText
     * @param string $falseText
     * @return string
     */
    public function allowedDisposalChoiceString($trueText='Yes',$falseText='No') : string{

        return ($this->isAllowedDisposalChoice() ? $trueText : $falseText);
    }

    /**
     * @return Collection
     */
    public function getInstructions() : Collection{

        return $this->instructions;
    }

    /**
     * @return string
     */
    public function __toString() : string{

        return $this->getName();
    }

}

完整指令实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Entity\Company;

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

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


    /**
     * @ORM\Column(nullable=true)
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="instructions")
     */
    private $company;

    /**
     * @var Vehicle
     * @ORM\Column(nullable=true)
     * @ORM\OneToOne(targetEntity="App\Entity\Vehicle", inversedBy="instruction")
     */
    private $vehicle;

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

    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $holdProcessing = false;

    /**
     * @var
     * @ORM\Column(type="text", nullable=true)
     */
    private $holdReason;


    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $overrideDates = false;

    /**
     * @var string
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $overrideDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $startDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $endDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $contactGarageStartDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $contactGarageEndDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $collectionAuthStartDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $collectionAuthEndDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $collectionReportStartDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $collectionReportEndDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $disposalAuthStartDate;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
     */
    private $disposalAuthEndDate;

    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $instructionComplete = false;

    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $garageComplete = false;


    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $collectionAuthComplete = false;

    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $collectionReportComplete = false;

    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default":false})
     */
    private $disposalAuthComplete = false;


    /**
     * Instruction constructor.
     * @throws \Exception
     */
    public function __construct()
    {
        $this->overrideDate = new \DateTime();

        $this->startDate = new \DateTime(); // when the instruction was created
        $this->endDate = new \DateTime(); // when the instruction ended


        $this->contactGarageStartDate = new \DateTime();
        $this->contactGarageEndDate = new \DateTime();

        $this->collectionAuthStartDate = new \DateTime();
        $this->collectionAuthEndDate = new \DateTime();

        $this->collectionReportStartDate = new \DateTime();
        $this->collectionReportEndDate = new \DateTime();

        $this->disposalAuthStartDate = new \DateTime();
        $this->disposalAuthEndDate = new \DateTime();

    }


    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return string|null
     */
    public function getClientRef(): ?string
    {
        return $this->clientRef;
    }

    /**
     * @param string $clientRef
     * @return Instruction
     */
    public function setClientRef(string $clientRef): self
    {
        $this->clientRef = $clientRef;

        return $this;
    }

    /**
     * @param \App\Entity\Company $company
     * @return Instruction
     */
    public function setCompany(Company $company) : self{

        $this->company = $company;

        return $this;
    }

    /**
     * @return \App\Entity\Company|null
     */
    public function getCompany() : ?Company{

        return $this->company;
    }

    /**
     * @param string $code
     * @return Instruction
     */
    public function setCustomerCostCode(string $code) : self{

        $this->customerCostCode = $code;

        return $this;
    }

    /**
     * @return string
     */
    public function getCustomerCostCode() : string{

        return $this->customerCostCode;
    }

    /**
     * @param bool $processing
     * @return Instruction
     */
    public function setHoldProcessing(bool $processing) : self{

        $this->holdProcessing = $processing;

        return $this;
    }

    /**
     * @return bool
     */
    public function getHoldProcessing() : bool{

        return $this->holdProcessing;
    }

    /**
     * @param string $reason
     * @return Instruction
     */
    public function setHoldReason(string $reason) : self{

        $this->holdReason = $reason;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getHoldReason() : ?string{

        return $this->holdReason;
    }

    /**
     * @param bool $override
     * @return Instruction
     */
    public function setOverrideDates(bool $override) : self{

        $this->overrideDates = $override;

        return $this;
    }

    /**
     * @return bool
     */
    public function getOverrideDates() : bool{

        return $this->overrideDates;
    }

    /**
     * @param \DateTime $dateObj
     * @return Instruction
     */
    public function setOverrideDate(\DateTime $dateObj) : self{

        $this->overrideDate = $dateObj;

        return $this;
    }

    /**
     * @param string $datetime
     * @return Instruction
     * @throws \Exception
     */
    public function setOverrideDateString(string $datetime) : self{

        $this->setOverrideDate(new \DateTime($datetime));
    }

    /**
     * @return \DateTime
     */
    public function getOverrideDate() : \DateTime{

        return $this->overrideDate;
    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setStartDate(\DateTime $date) : self{

        $this->startDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getStartDate() : \DateTime{

        return $this->startDate;
    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setEndDate(\DateTime $date) : self{

        $this->endDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getEndDate() : \DateTime{

        return $this->endDate;
    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setContactGarageStartDate(\DateTime $date) : self{

        $this->contactGarageStartDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getContactGarageStartDate() : \DateTime{

        return $this->contactGarageStartDate;
    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setContactGarageEndDate(\DateTime $date) : self{

        $this->contactGarageEndDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getContactGarageEndDate() : \DateTime{

        return $this->contactGarageEndDate;
    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setCollectionAuthStartDate(\DateTime $date) : self{

        $this->collectionAuthStartDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getCollectionAuthStartDate() : \DateTime{

        return $this->collectionAuthStartDate;

    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setCollectionAuthEndDate(\DateTime $date) : self{

        $this->collectionAuthEndDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getCollectionAuthEndDate() : \DateTime{

        return $this->collectionAuthEndDate;

    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setCollectionReportStartDate(\DateTime $date) : self{

        $this->collectionReportStartDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getCollectionReportStartDate() : \DateTime{

        return $this->collectionReportStartDate;

    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setCollectionReportEndDate(\DateTime $date) : self{

        $this->collectionReportEndDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getCollectionReportEndDate() : \DateTime{

        return $this->collectionReportEndDate;

    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setDisposalAuthStartDate(\DateTime $date) : self{

        $this->disposalAuthStartDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getDisposalAuthStartDate() : \DateTime{

        return $this->disposalAuthStartDate;

    }

    /**
     * @param \DateTime $date
     * @return Instruction
     */
    public function setDisposalAuthEndDate(\DateTime $date) : self{

        $this->disposalAuthEndDate = $date;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getDisposalAuthEndDate() : \DateTime{

        return $this->disposalAuthEndDate;

    }



    /**
     * @param bool $complete
     * @return Instruction
     */
    public function setInstructionComplete(bool $complete) : self{

        $this->instructionComplete = $complete;

        return $this;
    }

    /**
     * @return bool
     */
    public function getInstructionComplete() : bool{

        return $this->instructionComplete;
    }

    /**
     * @return bool
     */
    public function isInstructionComplete() : bool{

        return $this->getInstructionComplete();
    }

    /**
     * @param bool $complete
     * @return Instruction
     */
    public function setGarageComplete(bool $complete) : self{

        $this->garageComplete = $complete;

        return $this;
    }

    /**
     * @return bool
     */
    public function getGarageComplete() : bool{

        return $this->garageComplete;
    }

    /**
     * @return bool
     */
    public function isGarageComplete() : bool{

        return $this->getGarageComplete();
    }

    /**
     * @param bool $complete
     * @return Instruction
     */
    public function setCollectionAuthComplete(bool $complete) : self{

        $this->collectionAuthComplete = $complete;

        return $this;
    }

    /**
     * @return bool
     */
    public function getCollectionAuthComplete() : bool{

        return $this->collectionAuthComplete;
    }

    /**
     * @return bool
     */
    public function isCollectionAuthComplete() : bool{

        return $this->getCollectionAuthComplete();
    }

    /**
     * @param bool $complete
     * @return Instruction
     */
    public function setCollectionReportComplete(bool $complete) : self{

        $this->collectionReportComplete = $complete;

        return $this;
    }

    /**
     * @return bool
     */
    public function getCollectionReportComplete() : bool{

        return $this->collectionReportComplete;
    }

    /**
     * @return bool
     */
    public function isCollectionReportComplete() : bool{

        return $this->getCollectionReportComplete();
    }

    /**
     * @param bool $complete
     * @return Instruction
     */
    public function setDisposalAuthComplete(bool $complete) : self{

        $this->disposalAuthComplete = $complete;

        return $this;
    }

    /**
     * @return bool
     */
    public function getDisposalAuthComplete() : bool{

        return $this->disposalAuthComplete;
    }

    /**
     * @return bool
     */
    public function isDisposalAuthComplete() : bool{

        return $this->getDisposalAuthComplete();
    }


    /**
     * @return Vehicle|null
     */
    public function getVehicle() : ?Vehicle{

        return $this->vehicle;
    }



}

2 个答案:

答案 0 :(得分:1)

这是您的实体应该是什么样的示例:

指令类

class Instruction {
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="instructions")
     * @ORM\JoinColumn(nullable=true)
     */
    private $company;

    /**
     * @param \AppBundle\Entity\Company $company
     * @return Instruction
     */
    public function setCompany(\AppBundle\Entity\Company $company) {
        $this->company=$company;

        return $this;
    }

    /**
     * @return \AppBundle\Entity\Company
     */
    public function getCompany() {
        return $this->company;
    }
}

公司类别

class Company{
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Instruction", mappedBy="company")
     */
    private $instructions;

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

    /**
     * @param \AppBundle\Entity\Insctruction $insctruction
     * @return Company
     */
    public function addInstruction(\AppBundle\Entity\Insctruction $insctruction) {
        $this->insctructions[]=$insctruction;

        return $this;
    }

    /**
     * @param \AppBundle\Entity\Insctruction $insctruction
     * @return bool
     */
    public function removeInstruction(\AppBundle\Entity\Insctruction $insctruction) {
        return $this->insctructions->removeElement($insctruction);
    }

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

答案 1 :(得分:0)

我发现了问题!

/**
     * @ORM\Column(nullable=true)
     * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="instructions")
     */

字面上只是将Column更改为JoinColumn并创建新的迁移