سؤال

I have 3 entities where the mapping is causing some issues.

The two problems im facing are:

  1. When i use doctrine command line schema update, it removes the column "item_type" in the item_type_field table. It looks like doctrine is not seeing this column in the YML file, while it is there.

  2. In the Symfony application the mapping between TypeField and FieldType is not working. When i dump the FieldType of a field it returns null.

Am i missing something?

Entities and mappings:

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;
    }
}

Mapping files:

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
هل كانت مفيدة؟

المحلول

In TypeField you have manyToOne repeated twice. The second one overrides the first one whcih is why doctrine is not seeing it.

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

This may or may not fix all your issues. And it certainly does not address the issue of your very confusing naming conventions.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top