简体   繁体   中英

Doctrine2 oneToMany relation yaml

I have an entity called “Object”, here is the yaml code:

Entities\Object:
  type: entity
  table: objects
  id:
    id:
      type: integer
      generator:
      strategy: AUTO
  fields:
    parent_id: 
      type : integer
  oneToOne:
    type:
      targetEntity: ObjectType
      joinColumn:
        name: type_id
        referencedColumnName: id

I want to add a children parent relation (oneToMany) but I don't know how? I want the mysql table to have following structure: id, type_id, parent_id and the entity to have those options $object->getParent() (single object) and $object->getChildren() (collection of objects) . Hope someone can help, thnx

You're trying to do One-to-Many, self referencing, it should be something like that:

Entities\Objects:
  type: entity
  table: objects
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  oneToMany:
    children:
      targetEntity: Objects
      mappedBy: parent
  manyToOne:
    parent:
      targetEntity: Objects
      inversedBy: children
      joinColumn:
        name: parent_id
        referencedColumnName: id

Take a look at the manual Association Mapping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM