0

My Item.orm.yml:

AppBundle\Entity\Item:
    type: entity
    manyToMany:
        categories:
            targetEntity: AppBundle\Entity\Category
            cascade: ['persist']
            inversedBy: items
            joinTable:
                name: item_category
                joinColumns:
                    item_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    category_id:
                        referencedColumnName: id
    oneToMany:
        attributes:
            targetEntity: AppBundle\Entity\AttrValue
            mappedBy: item
            cascade: ['persist','remove']
    table: items
    repositoryClass: AppBundle\Repository\ItemRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255

AttrValue.orm.yml:

AppBundle\Entity\AttrValue:
    type: entity
    manyToOne:
        item:
            targetEntity: AppBundle\Entity\Item
            inversedBy: attributes
            cascade: ['persist','remove']
            joinColumn:
                name: item_id
                referencedColumn: id
                nullable: false
        attribute:
            targetEntity: AppBundle\Entity\Attribute
            inversedBy: attrValues
            cascade: ['persist','remove']
            joinColumn:
                name: attribute_id
                referencedColumn: id
                nullable: false
    table: attr_value
    repositoryClass: AppBundle\Repository\AttrValueRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        value:
            type: string
            length: 255

Attribute.orm.yml:

AppBundle\Entity\Attribute:
    type: entity
    oneToMany:
            attrValues:
                targetEntity: AppBundle\Entity\AttrValue
                mappedBy: attribute
                cascade: ['persist','remove']
    table: attribute
    repositoryClass: AppBundle\Repository\AttributeRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        alias:
            type: string
            length: 255
        name:
            type: string
            length: 255

When I want to send a POST request, in the form of a JSON array, everything seems to be written, but except for one field: item_id, NULL is written there. For deserialization I use JMSSerializer. What could be the problem? Entities getters and setters are standard. JSON array input:

{
    "name": "Book",
    "categories": [
        {
            "id": 2
        },
        {
            "id": 11
        }
    ],
    "attributes": [
        {
            "value": "black",
            "attribute": {
                "alias": "color",
                "name": "color"
            }
        },
        {
            "value": "1",
            "attribute": {
                "alias": "price",
                "name": "price"
            }
        }
    ]
}

In attr_value table:

+----+---------+-------+--------------+
| id | item_id | value | attribute_id |
+----+---------+-------+--------------+
| 1 |    NULL | 6     |           8   |
| 2 |    NULL | black |           9   |
| 3 |    NULL | 6     |           10  |
| 4 |    NULL | bk    |           11  |
| 5 |    NULL | 1     |           12  |
+----+---------+-------+--------------+

I think that I need to add some relations to setters, but I have no idea how to do that (I already tore Google, but did not find anything).

Update: When I try to send something like this, everything works (but not correct), but i don't need this lines:

{
    "name": "Book",
    "categories": [
        {
            "id": 2
        },
        {
            "id": 11
        }
    ],
    "attributes": [
        {
            "value": "black",

            "attribute": {
                "alias": "color",
                "name": "color"
            }
        },
        {
            "value": "1",
            "item": {       #<----this line
            "name": "Book"  #<----and this
            },
            "attribute": {
                "alias": "price",
                "name": "price"
            }
        }
    ]
}
Dialkord
  • 111
  • 1
  • 3
  • 12
  • show the code of your form, do you want to add existing entities or new ones? – Robert Mar 12 '18 at 21:02
  • I am not use form and I want to add new values (do not update) to my entities that are related with each other. – Dialkord Mar 12 '18 at 21:08
  • Oddly similar to: https://stackoverflow.com/questions/49220634/doctrine-relation-write-null-in-table – Cerad Mar 12 '18 at 21:21
  • @Cerad yes, because I need help) I use all methods, because I need to solve this issue – Dialkord Mar 12 '18 at 21:29
  • Repeating the same question is seldom useful and often frowned upon. Might want to rethink your approach. But here is a hint. AttrValue::setItem() is not being called. – Cerad Mar 12 '18 at 21:44
  • @Cerad I know that setter is not being called, but I don't understand why... – Dialkord Mar 12 '18 at 21:48

0 Answers0