Cakephp 3 ORM使用patchEntity

时间:2017-04-12 14:43:14

标签: cakephp orm

SupplierSchemasTable

$this->hasMany('SupplierSchemaItems');

$this->belongsToMany('Suppliers');

+----+----------+
| id |  title   |
+----+----------+
|  1 | schema_1 |
|  2 | schema_2 |
+----+----------+

SuppliersTable

$this->belongsToMany('SupplierSchemas');

$this->belongsToMany('SupplierSchemaItems', [
    'through' => 'SupplierSchemaItemsSuppliers',
    ]);

+----+------------+
| id |    name    |
+----+------------+
|  1 | supplier_1 |
+----+------------+

SupplierSchemasInputTypesTable

$this->hasMany('SupplierSchemaItems'); 

+----+----------+
| id |  title   |
+----+----------+
|  1 | Text     |
|  2 | Textarea |
|  3 | Select   |
+----+----------+

SupplierSchemaItemsTable

$this->belongsTo('SupplierSchemas');

$this->belongsToMany('Suppliers', [
        'through' => 'SupplierSchemaItemsSuppliers',
    ]);

+----+--------------------------+--------------------+--------------------------------+
| id |          title           | supplier_schema_id | supplier_schemas_input_type_id |
+----+--------------------------+--------------------+--------------------------------+
|  1 | Partners                 |                  1 |                              1 |
|  2 | Bio                      |                  1 |                              2 |
|  3 | Identification Documents |                  1 |                              3 |
+----+--------------------------+--------------------+--------------------------------+

SupplierSchemaItemsSuppliersTable

$this->belongsTo('SupplierSchemasInputTypes');
$this->belongsTo('SupplierSchemaItems');
$this->belongsTo('Suppliers');

+----+-------------------------+-------------+-------------------------+
| id | supplier_schema_item_id | supplier_id |          value          |
+----+-------------------------+-------------+-------------------------+
|  1 |                       1 |           1 | 4                       |
|  2 |                       2 |           1 | Supplier Bio Text       |
|  3 |                       3 |           1 | Current Signed Passport |
|  4 |                       3 |           1 | Driving Licence         |
+----+-------------------------+-------------+-------------------------+

我需要允许admin能够更新SupplierSchemaItemsSuppliersTable中的数据。但是,当我尝试使用下面的

$SuppliersTable->patchEntity($supplier, $this->request->data(), [
                    'associated' => [

'SupplierSchemas.SupplierSchemaItems.Suppliers',
                    ]
                ]);

适用于supplier_schema_item_id和supplier_id不同的行。但是对于可能有多个项目的选择(标识文档),它会失败,即只更新第一个记录并删除第二个记录。

以下是请求数据的数据转储:

[
    (int) 0 => [
        'id' => '1',
        'supplier_schema_items' => [
            (int) 2 => [
                'id' => '3',
                'suppliers' => [
                    (int) 0 => [
                        'id' => '1',
                        '_joinData' => [
                            'id' => '3',
                            'value' => 'Current Signed Passport - Edit Test'
                        ]
                    ],
                    (int) 1 => [
                        'id' => '1',
                        '_joinData' => [
                            'id' => '4',
                            'value' => 'Driving Licesnce - Edit Test 2'
                        ]
                    ]
                ]
            ]
        ]
    ]
]

0 个答案:

没有答案