Doctrine - 引用的列名“id”必须是目标实体类的主键列

时间:2017-11-19 12:58:03

标签: php symfony doctrine-orm orm

我在验证和架构创建方面遇到问题,错误日志是:

[Mapping]  FAIL - The entity-class 'App\Api\Entity\User' mapping is invalid:
* The referenced column name 'id' has to be a primary key column on the target entity class 'App\Acme\Entity\User\BaseUser'.
* The join columns of the association 'createdBy' have to match to ALL identifier columns of the target entity 'App\Acme\Entity\User\BaseUser', however '' are missing.
* The referenced column name 'id' has to be a primary key column on the target entity class 'App\Acme\Entity\User\BaseUser'.
* The join columns of the association 'modifiedBy' have to match to ALL identifier columns of the target entity 'App\Acme\Entity\User\BaseUser', however '' are missing.

我正在使用Symfony 4,我有继承问题

的src / Acme公司/实体/碱/ BaseUser.php

<?php

namespace App\Acme\Entity\User;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\MappedSuperclass
 * @UniqueEntity("email")
 * Class Document
 */
class BaseUser implements UserInterface, \Serializable
{
/**
 * @var int The id
 *
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
protected $id;

/**
 * @var BaseUser
 * @ORM\ManyToOne(targetEntity="App\Acme\Entity\User\BaseUser")
 * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
 */
protected $createdBy;

/**
 * @var BaseUser
 * @ORM\ManyToOne(targetEntity="\App\Acme\Entity\User\BaseUser")
 * @ORM\JoinColumn(name="modified_by", referencedColumnName="id")
 */
protected $modifiedBy;

public function __construct( BaseUser $user)
{
    $this->createdBy    = $user;
    $this->modifiedBy   = $user;
}



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

/**
 * @param int $id
 */
public function setId(int $id)
{
    $this->id = $id;
}

并且有一个实体试图扩展此BaseUser

的src /阿比/实体/碱/ user.php的

<?php

namespace App\Api\Entity;

use App\Acme\Entity\User\BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class User extends BaseUser  { ... }

问题出现在

/**
 * @var BaseUser
 * @ORM\ManyToOne(targetEntity="App\Acme\Entity\User\BaseUser")
 * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
 */
protected $createdBy;

/**
 * @var BaseUser
 * @ORM\ManyToOne(targetEntity="\App\Acme\Entity\User\BaseUser")
 * @ORM\JoinColumn(name="modified_by", referencedColumnName="id")
 */
protected $modifiedBy;

当我删除它们时,一切正常。

我的映射文件config / packages / doctrine.yaml

doctrine:
dbal:
    # With Symfony 3.3, remove the `resolve:` prefix
    url: '%env(DATABASE_URL)%'
orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    mappings:
        Api:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Api/Entity'
            prefix: 'App\Api\Entity\'
            alias: Api
        Acme:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Acme/Entity'
            prefix: 'App\Acme\Entity\'
            alias: Acme

你能帮帮忙吗?

1 个答案:

答案 0 :(得分:0)

更改为

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

检查您的targetEntity命名空间。哪个是捆绑...部分Symfony Bundle Name Doc

/**
* @var BaseUser
* @ORM\ManyToOne(targetEntity="yourBaseUser")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
protected $createdBy;

/**
* @var BaseUser
* @ORM\ManyToOne(targetEntity="yourBaseUser")
* @ORM\JoinColumn(name="modified_by", referencedColumnName="id")
*/
protected $modifiedBy;