CakePHP 3.X:更新多条记录



我已经阅读了保存实体和修补HasMany和BelongsToMany,但我遇到了patchEntities()的麻烦,因为它似乎没有合并数据。修补后,$entities的结果是1个"空"/"新"记录而不是2个包含id的记录。保存实体会导致外键错误。

我是CakePHP 3.0的新手,所以我很可能在这里做了一些错误的事情。有人能帮帮我吗?

谢谢!

我代码:

if ($this->request->is(['patch', 'post', 'put'])) {
    $list = $this->RecipyIngredients->find()
        ->contain(['Ingredients', 'Quantities'])
        ->where(['RecipyIngredients.recipy_id' => $recipy_id])
        ->toArray();
    $entities = $this->RecipyIngredients->patchEntities($list, $this->request->data);
    debug($entities);
    foreach ($entities as $entity) {
        //$this->RecipyIngredients->save($entity);
        //debug($entity);
    }
}

各种结果:

debug($this->request->data);
[
    'RecipyIngredients' => [
        (int) 0 => [
            'id' => '1',
            'amount' => '25',
            'quantity_id' => '1',
            'ingredient_id' => '269',
            'remark' => ''
        ],
        (int) 1 => [
            'id' => '2',
            'amount' => '300',
            'quantity_id' => '1',
            'ingredient_id' => '88',
            'remark' => ''
        ],
    ]
]
debug($list);
[
    (int) 0 => object(AppModelEntityRecipyIngredient) {
        'id' => (int) 1,
        'recipy_id' => (int) 1,
        'ingredient_id' => (int) 269,
        'quantity_id' => (int) 1,
        'amount' => '25',
        'amount_in_gram' => (int) 25,
        'remark' => '',
        'grouping_term' => null,
        'version' => (int) 1,
        'created' => null,
        'modified' => null,
        'quantity' => object(AppModelEntityQuantity) {
            'id' => (int) 1,
            'name' => 'gram',
            '[new]' => false,
            '[accessible]' => [
                'name' => true,
                'ingredients' => true,
                'recipy_ingredients' => true,
                'shoppinglist' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Quantities'
        },
        'ingredient' => object(AppModelEntityIngredient) {
            'id' => (int) 269,
            'name' => 'citroensap',
            'average_weight' => null,
            'recipy_id' => null,
            'quantity_id' => null,
            '[new]' => false,
            '[accessible]' => [
                'name' => true,
                'average_weight' => true,
                'recipy_id' => true,
                'quantity_id' => true,
                'recipy' => true,
                'quantity' => true,
                'shoppinglist' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Ingredients'
        },
        '[new]' => false,
        '[accessible]' => [
            'recipy_id' => true,
            'ingredient_id' => true,
            'quantity_id' => true,
            'amount' => true,
            'amount_in_gram' => true,
            'remark' => true,
            'grouping_term' => true,
            'version' => true,
            'recipy' => true,
            'ingredient' => true,
            'quantity' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[repository]' => 'RecipyIngredients'
    },
    (int) 1 => object(AppModelEntityRecipyIngredient) {
        'id' => (int) 2,
        'recipy_id' => (int) 1,
        'ingredient_id' => (int) 88,
        'quantity_id' => (int) 1,
        'amount' => '300',
        'amount_in_gram' => (int) 300,
        'remark' => 'Gewicht zonder schil',
        'grouping_term' => null,
        'version' => (int) 1,
        'created' => null,
        'modified' => null,
        'quantity' => object(AppModelEntityQuantity) {
            'id' => (int) 1,
            'name' => 'gram',
            '[new]' => false,
            '[accessible]' => [
                'name' => true,
                'ingredients' => true,
                'recipy_ingredients' => true,
                'shoppinglist' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Quantities'
        },
        'ingredient' => object(AppModelEntityIngredient) {
            'id' => (int) 88,
            'name' => 'banaan',
            'average_weight' => null,
            'recipy_id' => null,
            'quantity_id' => null,
            '[new]' => false,
            '[accessible]' => [
                'name' => true,
                'average_weight' => true,
                'recipy_id' => true,
                'quantity_id' => true,
                'recipy' => true,
                'quantity' => true,
                'shoppinglist' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[repository]' => 'Ingredients'
        },
        '[new]' => false,
        '[accessible]' => [
            'recipy_id' => true,
            'ingredient_id' => true,
            'quantity_id' => true,
            'amount' => true,
            'amount_in_gram' => true,
            'remark' => true,
            'grouping_term' => true,
            'version' => true,
            'recipy' => true,
            'ingredient' => true,
            'quantity' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[repository]' => 'RecipyIngredients'
    }
]
debug($entities);
[
    (int) 0 => object(AppModelEntityRecipyIngredient) {
        '[new]' => true,
        '[accessible]' => [
            'recipy_id' => true,
            'ingredient_id' => true,
            'quantity_id' => true,
            'amount' => true,
            'amount_in_gram' => true,
            'remark' => true,
            'grouping_term' => true,
            'version' => true,
            'recipy' => true,
            'ingredient' => true,
            'quantity' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[repository]' => 'RecipyIngredients'
    }
]

好的,查看源代码,问题在于表名在$this->request->data中。

我将patchEntities()行改为:

$entities = $this->RecipyIngredients->patchEntities($list, $this->request->data['RecipyIngredients']);

一切都很好!

最新更新