使用YAML定义一对多关系

时间:2014-01-23 12:16:29

标签: php symfony doctrine-orm mapping yaml

我承担了一个项目,其中学说的映射基于YAML。

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

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: {  }
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: {  }

万阿英,蒋达清: 当我更新数据库架构时,我收到以下错误:

 [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

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

编辑:这是使用--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)

1 个答案:

答案 0 :(得分:6)

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

manyToOne:
  post:
    targetEntity: TEST\Bundle\BlogBundle\Entity\Post
    joinColumn:
      name: post_id
      referencedColumnName: id

小心缩进。