【问题标题】:Mapping issue Doctrine & Symfony映射问题 Doctrine & Symfony
【发布时间】:2014-04-23 19:40:42
【问题描述】:

我有 3 个实体的映射导致了一些问题。

我面临的两个问题是:

  1. 当我使用教义命令行模式更新时,它会删除 item_type_field 表中的“item_type”列。看起来学说没有在 YML 文件中看到此列,而它确实存在。

  2. 在 Symfony 应用程序中,TypeField 和 FieldType 之间的映射不起作用。当我转储字段的 FieldType 时,它​​返回 null。

我错过了什么吗?

实体和映射:

class ItemType
{
    protected $id;  
    protected $title;
    protected $description;
    protected $fields;


    public function __construct(){  
         $this->fields = new ArrayCollection();
    }


    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    public function getDescription()
    {
        return $this->description;
    }   

    public function addField(\Aveqcms\CoreBundle\Entity\TypeField $field)
    {
        $field->setItemType($this);
        $this->fields[] = $field;

        return $this;
    }

    public function removeField(\Aveqcms\CoreBundle\Entity\TypeField $field)
    {
        $this->fields->removeElement($field);
    }

    public function getFields()
    {
        return $this->fields;
    }
}


class TypeField
{

    private $id;  
    private $item_type;
    private $field_type;


    public function getId()
    {
        return $this->id;
    }

    public function setItemType($itemType)
    {
        $this->item_type = $itemType;
        return $this;
    }

    public function getItemType()
    {
        return $this->item_type;
    }

    public function setFieldType($fieldType)
    {
        $this->field_type = $fieldType;

        return $this;
    }

    public function getFieldType()
    {
        return $this->field_type;
    }
}


class FieldType
{

    private $id;
    private $title; 
    private $fields;

    public function __construct()
    {
        $this->fields = new ArrayCollection();
    }   

    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }


    public function getTitle()
    {
        return $this->title;
    }
}

映射文件:

Acme\CoreBundle\Entity\ItemType:
    type: entity
    table: item_type
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
            length: 255
        description:
            type: string
            length: 255  
    oneToMany:
        fields:
            targetEntity: TypeField
            mappedBy: item_type
            cascade: [persist]   

Acme\CoreBundle\Entity\TypeField:
    type: entity
    table: item_type_field
    id:
        id:
            type: integer
            generator: { strategy: AUTO }  
    manyToOne:
        field_type:
            targetEntity: FieldType
            inversedBy: fields
            joinColumn:
                name: field_type
                referencedColumnName: id        
    manyToOne:
        item_type:
            targetEntity: ItemType
            inversedBy: fields
            joinColumn:
                name: item_type
                referencedColumnName: id

Acme\CoreBundle\Entity\FieldType:
    type: entity
    table: field_type
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        title:
            type: text
    oneToMany:
        fields:
            targetEntity: TypeField
            mappedBy: field_type

【问题讨论】:

    标签: php symfony doctrine-orm doctrine yaml


    【解决方案1】:

    在 TypeField 中有 manyToOne 重复了两次。第二个覆盖第一个,这就是为什么教义没有看到它。

    Acme\CoreBundle\Entity\TypeField:
    type: entity
    table: item_type_field
    id:
        id:
            type: integer
            generator: { strategy: AUTO }  
    manyToOne:
        field_type:
            targetEntity: FieldType
            inversedBy: fields
            joinColumn:
                name: field_type
                referencedColumnName: id        
    #manyToOne: *** Get rid of this ***
        item_type:
            targetEntity: ItemType
            inversedBy: fields
            joinColumn:
                name: item_type
                referencedColumnName: id
    

    这可能会也可能不会解决您的所有问题。它当然不能解决您非常混乱的命名约定的问题。

    【讨论】:

    • 谢谢我不知道你只需要指定“manyToOne:”一次,删除这个解决了主要问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2013-09-02
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多