鉴别器列:Class 没有字段或关联命名类型

Discriminator column : Class has no field or association named type

我有一个 class 包含抽象属性 contractInfo Type

的合同
/**
 * @var ContractInfoType|null
 *
 * @ORM\OneToOne(targetEntity="ContractInfoType", cascade={"persist"})
 * @ORM\JoinColumn(name="contract_info_type_id", referencedColumnName="id", nullable=true)
 */
private $contractInfoType;

ContractInfoType 是一个抽象实体:ContractInfoPro 和 ContractInfoAsso 继承自它们

/**
 * ContractInfoType
 *
 * @ORM\Table(name="contract_info_type",
 *   indexes={
 *     @ORM\Index(name="IDX_TYPE", columns={"type"}),
 *   }
  * )
 * @ORM\Entity(repositoryClass="App\Repository\Contract\ContractInfoTypeRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap(
 *     {
 *      "pro" = "App\Entity\Contract\Type\ContractInfoPro",
 *      "asso" = "App\Entity\Contract\Type\ContractInfoAsso"
 *     }
 * )
 * @ORM\HasLifecycleCallbacks
 */
abstract class ContractInfoType
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    public function setId(int $id): self
    {
        $this->id = $id;

        return $this;
    }

    abstract public function getType(): string;
}

和继承ContractInfoType

的class
/**
 * ContractInfoAsso
 *
 * @ORM\Table(name="contract_info_asso")
 * @ORM\Entity(repositoryClass="App\Repository\Contract\ContractInfoAssoRepository")
 * @ORM\HasLifecycleCallbacks
 */
class ContractInfoAsso extends ContractInfoType
{
    const TYPE = 'asso';

    /**
     * @var string
     *
     * @ORM\Column(name="num_prefecture", type="string", length=40, nullable=true)
     *
     * @Assert\Type(
     *     type="string",
     *     message="The value {{ value }} is not a valid {{ type }}."
     * )
     * @Assert\Length(
     *      max = 40,
     *      maxMessage = "num_prefecture value cannot be longer than {{ limit }} characters"
     * )
     */
    private $numPrefecture;

    /**
     * @return string
     */
    public function getNumPrefecture(): string
    {
        return $this->numPrefecture;
    }

    /**
     * @param string $numPrefecture
     */
    public function setNumPrefecture(string $numPrefecture): self
    {
        $this->numPrefecture = $numPrefecture;

        return $this;
    }

    public function getType(): string
    {
        return self::TYPE;
    }
}

我的目标是像在 SQL 中那样按鉴别器列类型过滤合同:

 select * from contract left join contract_info_type cit on contract.contract_info_type_id = cit.id where cit.type LIKE "pro"

但是,当我尝试以这种方式使用教义时

$queryBuilder->leftJoin('contract.contractInfoType', 'type');
$queryBuilder->andWhere('type.type = :type");

我收到错误

[Semantical Error] line 0, col 183 near 'type = :type': Error: Class App\Entity\Contract\ContractInfoType has no field or association named type

这是有道理的,因为我没有在实体中声明真实的字段类型。

我应该如何使它与学说一起工作?

看来你不能以这种方式使用鉴别器。在您的 DQL 中尝试使用 "INSTANCE OF" 而不是连接。

More info

Possible duplicate