CakePHP 3.x:更新多条记录

时间:2015-07-06 20:06:34

标签: cakephp-3.0 updating multiple-records

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

我是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(App\Model\Entity\RecipyIngredient) {

        '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(App\Model\Entity\Quantity) {

            '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(App\Model\Entity\Ingredient) {

            '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(App\Model\Entity\RecipyIngredient) {

        '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(App\Model\Entity\Quantity) {

            '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(App\Model\Entity\Ingredient) {

            '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(App\Model\Entity\RecipyIngredient) {

        '[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'

    }
]

1 个答案:

答案 0 :(得分:3)

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

我将patchEntities()行更改为:

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

一切都很好!