简体   繁体   English

视图实体上的“没有为实体指定标识符/主键”错误

[英]"No identifier/primary key specified for Entity" Error on a view entity

Symfony 6 Symfony 6

No identifier/primary key specified for Entity "App\Entity\ContactVw".没有为实体“App\Entity\ContactVw”指定标识符/主键。 Every Entity must have an identifier/primary key.每个实体都必须有一个标识符/主键。

I know how to fix that error when using a simple table.我知道如何在使用简单表格时修复该错误。

But how can I fix that error when I am using a view.但是当我使用视图时如何修复该错误。 Views don't have primary keys.视图没有主键。 Where in the class definition do I have to tell doctrine what column to use as primary?在 class 定义的哪个位置,我必须告诉 doctrine 哪个列用作主列?

This is my class这是我的 class

<?php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
 * ContactVw
 *
 * @ORM\Table(name="contact_vw", indexes={@ORM\Index(name="city", columns={"city"}), @ORM\Index(name="customer", columns={"customer"}), @ORM\Index(name="name", columns={"name"}), @ORM\Index(name="country_sold_to", columns={"country_sold_to"})})
 * @ORM\Entity
 */
class ContactVw
{

    public function __construct(
        #[Id, Column]
        private string $contactId,
    ) {
    }


    /**
     * @var string|null
     *
     * @ORM\Column(name="first", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $first = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="middle", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $middle = 'NULL';

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

    /**
     * @var string
     *
     * @ORM\Column(name="customer", type="decimal", precision=38, scale=0, nullable=false)
     */
    private $customer;

    /**
     * @var string|null
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $email = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="phone", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $phone = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="mobile", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $mobile = 'NULL';

    /**
     * @var bool
     *
     * @ORM\Column(name="skill_id", type="boolean", nullable=false)
     */
    private $skillId;

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

    /**
     * @var string
     *
     * @ORM\Column(name="country", type="string", length=2, nullable=false)
     */
    private $country;

    /**
     * @var string|null
     *
     * @ORM\Column(name="country_sold_to", type="string", length=41, nullable=true, options={"default"="NULL"})
     */
    private $countrySoldTo = 'NULL';

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=35, nullable=false)
     */
    private $name;

    /**
     * @var string|null
     *
     * @ORM\Column(name="name_2", type="string", length=35, nullable=true, options={"default"="NULL"})
     */
    private $name2 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="postal_code", type="decimal", precision=38, scale=0, nullable=true, options={"default"="NULL"})
     */
    private $postalCode = 'NULL';

    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=35, nullable=false)
     */
    private $city;

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

        return $this;
    }

    public function getFirst(): ?string
    {
        return $this->first;
    }

    public function setFirst(?string $first): self
    {
        $this->first = $first;

        return $this;
    }

    public function getMiddle(): ?string
    {
        return $this->middle;
    }

    public function setMiddle(?string $middle): self
    {
        $this->middle = $middle;

        return $this;
    }

    public function getLast(): ?string
    {
        return $this->last;
    }

    public function setLast(string $last): self
    {
        $this->last = $last;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function getMobile(): ?string
    {
        return $this->mobile;
    }

    public function setMobile(?string $mobile): self
    {
        $this->mobile = $mobile;

        return $this;
    }

    public function isSkillId(): ?bool
    {
        return $this->skillId;
    }

    public function setSkillId(bool $skillId): self
    {
        $this->skillId = $skillId;

        return $this;
    }

    public function getSkill(): ?string
    {
        return $this->skill;
    }

    public function setSkill(string $skill): self
    {
        $this->skill = $skill;

        return $this;
    }

    public function getCountry(): ?string
    {
        return $this->country;
    }

    public function setCountry(string $country): self
    {
        $this->country = $country;

        return $this;
    }

    public function getCountrySoldTo(): ?string
    {
        return $this->countrySoldTo;
    }

    public function setCountrySoldTo(?string $countrySoldTo): self
    {
        $this->countrySoldTo = $countrySoldTo;

        return $this;
    }

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

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

        return $this;
    }

    public function getName2(): ?string
    {
        return $this->name2;
    }

    public function setName2(?string $name2): self
    {
        $this->name2 = $name2;

        return $this;
    }

    public function getPostalCode(): ?string
    {
        return $this->postalCode;
    }

    public function setPostalCode(?string $postalCode): self
    {
        $this->postalCode = $postalCode;

        return $this;
    }

    public function getCity(): ?string
    {
        return $this->city;
    }

    public function setCity(string $city): self
    {
        $this->city = $city;

        return $this;
    }


}

You did not set an identifier on any attribute, which you can do using:您没有在任何属性上设置标识符,您可以使用:

#[ORM\Id] // Php Attribute

/**
* @ORM\Id // Annotation
**/
enter code here

In your code:在你的代码中:

#[Id, Column] // seems wrong

You are mixing annotation and Php Attribute to define how your entity work: dont do that .您正在混合注释和 Php 属性来定义实体的工作方式:不要那样做 Personnally, i recommend php attribute over annotation if you can use it.就个人而言,如果可以使用,我建议使用 php 属性而不是注释。

https://symfony.com/blog/new-in-symfony-5-2-php-8-attributeshttps://symfony.com/blog/new-in-symfony-5-2-php-8-attributes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM