实体类映射无效

时间:2019-07-11 11:45:23

标签: php symfony4

我在调试Entity的映射错误时遇到麻烦。 当我运行php bin/console doctrine:schema:validate时,它显示错误,如下所示

  

[FAIL]实体类App \ Entity \ Company映射无效:   关联App \ Entity \ Company#policies是指不存在的拥有方字段App \ Entity \ Policy#company_id。

我尝试了这篇帖子(Entity class mapping is invalid),该帖子与我几乎相同,但似乎不起作用。

这是我的公司实体

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity(repositoryClass="App\Repository\CompanyRepository")
 */
class Company
{

    use TimestampableEntity;

    const STATUS_ACTIVE = "ACTIVE";
    const STATUS_STAGING = "STAGING";
    const STATUS_INACTIVE = "INACTIVE";


    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

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

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Policy", mappedBy="company_id")
     */
    private $policies;

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="companies")
     */
    private $user;


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

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getAddress(): ?string
    {
        return $this->address;
    }

    public function setAddress(string $address): self
    {
        $this->address = $address;

        return $this;
    }

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

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

        return $this;
    }

    public function getLogo(): ?string
    {
        return $this->logo;
    }

    public function setLogo(string $logo): self
    {
        $this->logo = $logo;

        return $this;
    }

    /**
     * @return Collection|Policy[]
     */
    public function getPolicies(): Collection
    {
        return $this->policies;
    }

    public function addPolicy(Policy $policy): self
    {
        if (!$this->policies->contains($policy)) {
            $this->policies[] = $policy;
            $policy->setCompany($this);
        }

        return $this;
    }

    public function removePolicy(Policy $policy): self
    {
        if ($this->policies->contains($policy)) {
            $this->policies->removeElement($policy);
            // set the owning side to null (unless already changed)
            if ( $policy->getCompany() === $this) {
                $policy->setCompany(null);
            }
        }

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(string $url): self
    {
        $this->url = $url;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(string $status): self
    {
        $this->status = $status;

        return $this;
    }
}

这是我的政策实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity(repositoryClass="App\Repository\PolicyRepository")
 */
class Policy
{
    use TimestampableEntity;

    const VEH_TYPE_MINIBUS = "Minibus";
    const VEH_TYPE_FOUR_WHEEL = "4x4";
    const VEH_TYPE_CAR     = "Car";

    const VEH_PURPOSE_PERSONAL = "Personal";

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Company")
     * @ORM\JoinColumn(name="company_id", nullable=false, referencedColumnName="id")
     */
    private $company;

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

    /**
     * @ORM\Column(type="datetime")
     *  @Assert\NotBlank
     */
    private $fromDate;

    /**
     * @ORM\Column(type="datetime")
     * @Assert\NotBlank
     */
    private $toDate;

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

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

    /**
     * @ORM\Column(type="float",scale=2)
     */
    private $total_amt;

    /**
     * @ORM\Column(type="float", scale=2)
     */
    private $cover_amt;

    /**
     * @ORM\Column(type="float", scale=2)
     */
    private $discount;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="policies")
     */
    private $customer;


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

    public function getRefId(): ?string
    {
        return $this->ref_id;
    }

    public function setRefId(string $ref_id): self
    {
        $this->ref_id = $ref_id;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(string $status): self
    {
        $this->status = $status;

        return $this;
    }

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

    public function setFromDate(\DateTimeInterface $fromDate): self
    {
        $this->fromDate = $fromDate;

        return $this;
    }

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

    public function setToDate(\DateTimeInterface $toDate): self
    {
        $this->toDate = $toDate;

        return $this;
    }

    public function getTotalAmt(): ?float
    {
        return $this->total_amt;
    }

    public function setTotalAmt(float $total_amt): self
    {
        $this->total_amt = $total_amt;

        return $this;
    }

    public function getCoverAmt(): ?float
    {
        return $this->cover_amt;
    }

    public function setCoverAmt(float $cover_amt): self
    {
        $this->cover_amt = $cover_amt;

        return $this;
    }

    public function getCompany(): ?Company
    {
        return $this->company;
    }

    public function setCompany(?Company $company): self
    {
        $this->company = $company;

        return $this;
    }

    public function getCustomer(): ?Customer
    {
        return $this->customer;
    }

    public function setCustomer(?Customer $customer): self
    {
        $this->customer = $customer;

        return $this;
    }

    public function getVehicleType(): ?string
    {
        return $this->vehicle_type;
    }

    public function setVehicleType(string $vehicle_type): self
    {
        $this->vehicle_type = $vehicle_type;

        return $this;
    }

    public function getPurpose(): ?string
    {
        return $this->purpose;
    }

    public function setPurpose(string $purpose): self
    {
        $this->purpose = $purpose;

        return $this;
    }

    public function getDiscount(): ?float
    {
        return $this->discount;
    }

    public function setDiscount(float $discount): self
    {
        $this->discount = $discount;

        return $this;
    }

    public function getPolicyCode(): ?string
    {
        return $this->policy_code;
    }

    public function setPolicyCode(string $policy_code): self
    {
        $this->policy_code = $policy_code;

        return $this;
    }
}

1 个答案:

答案 0 :(得分:1)

company_id仅在表中不存在于实体中。因此,尝试使用mappedBy="company吗?希望对您有帮助!