【问题标题】:Create columns for inherited fields in child-class usine Doctrine 2 ORM使用 Doctrine 2 ORM 为子类中的继承字段创建列
【发布时间】:2016-08-18 13:55:22
【问题描述】:

我使用 Symfony 3。我有一个父类 Licence,它有一些字段:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

class Licence
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
protected $id;

/**
 * @ORM\Column(
 *    type="string",
 *     unique=true,
 *     length=30,
 *     nullable=false
 *     )
 */
protected $licenceKey;
}

我有一个扩展这个的子类。当然,它具有相同的字段,但我需要将它们存储在特定位置的数据库中: 所以我写了以下内容:

namespace AppBundle\Entity\LicenceCase\FirstLicenceCase;

use AppBundle\Entity\Licence as BaseLicence;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="lic_first")
 * @ORM\InheritanceType("SINGLE_TABLE")
 */
class FirstLicenceCase extends BaseLicence
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    public function getLicenceKey()
    {
        return $this->licenceKey;
    }
   public function setLicenceKey($licenceKey)
    {
        $this->licenceKey = $licenceKey;
    }
}

我需要获取任何特定许可案例的唯一列。我将为每个具有不同字段和方法的 LicenceCase 创建新类。但是使用此代码,我的数据库中没有正确的列。我该如何解决这个问题?

【问题讨论】:

标签: php orm doctrine-orm symfony


【解决方案1】:

解决方案是在我需要指定所有子级的地方指定继承树。例如在父类中编写以下注释。

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="integer")
 * @DiscriminatorMap({"1" = "LicenceCase1", "2" = "LicenceCase2", "3" = "LicenceCase3"})
 */
 abstract class Licence
 {
   // ...
 }

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多