使用YAML定义一对多关系

Adi*_*oui 1 php mapping yaml symfony doctrine-orm

我进行了一个项目,其中学说的映射基于YAML.

我需要在两个实体之间创建一对多的关系:Post和Image(帖子可以有零个或任意数量的图像).

在Post.orm.yml下:

TEST\Bundle\BlogBundle\Entity\Post:
    type: entity
    table: null
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        postUserId:
            type: integer
            column: post_user_id
        postDate:
            type: datetime
            column: post_date
        postContent:
            type: text
            column: post_content
        postTitle:
            type: string
            length: 255
            column: post_title
        commentStatus:
            type: boolean
            column: comment_status   
    oneToMany:
       images:
         targetEntity: Image
         mappedBy: pos
    lifecycleCallbacks: {  }
Run Code Online (Sandbox Code Playgroud)

在Image.orm.yml下:

TEST\Bundle\BlogBundle\Entity\Image:
    type: entity
    table: null
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        filePersistencePath:
            type: string
        subDir:
            type: string
    manyToOne:
      pos:
        targetEntity: Post
        inversedBy: images
      joinColumn:
        referencedColumnName: id

    lifecycleCallbacks: {  }
Run Code Online (Sandbox Code Playgroud)

probelm: 当我更新数据库架构时,我收到以下错误:

 [Symfony\Component\Debug\Exception\ContextErrorException]
 Notice: Undefined index: targetEntity in C:\xampp\htdocs\bghitn\vendor\doct
 rine\orm\lib\Doctrine\ORM\Mapping\Driver\YamlDriver.php line 399
Run Code Online (Sandbox Code Playgroud)

我不明白这个消息.我们非常感谢您的帮助.

编辑:这是使用--dump-sql更新模式的结果

C:\xampp\htdocs\bghitn>php app/console doctrine:schema:update --dump-sql
ALTER TABLE image ADD CONSTRAINT FK_C53D045F4B89032C FOREIGN KEY (post_id) REFER
ENCES post (id);
CREATE INDEX IDX_C53D045F4B89032C ON image (post_id)
Run Code Online (Sandbox Code Playgroud)

Pra*_*esh 6

从post实体中删除oneToMany关系,并在图像实体中保留manyToOne关系

manyToOne:
  post:
    targetEntity: TEST\Bundle\BlogBundle\Entity\Post
    joinColumn:
      name: post_id
      referencedColumnName: id
Run Code Online (Sandbox Code Playgroud)

缩进压痕.