CakePHP 3.4 如何在一个属于和有很多关系上插入

Tzi*_*Tzi 3 cakephp save associations has-and-belongs-to-many cakephp-3.x

在 cakePHP 3.4 上,我有 3 个表,其中一个属于并且有很多关系:成分、产品和成分产品:

class IngredientsTable extends Table
{
    public function initialize(array $config)
    {
        // Use through option because it looks like you 
        // have additional data on your IngredientsProducts table
        $this->belongsToMany('Products', [
            'through' => 'IngredientsProducts',
        ]);
    }
}

class ProductsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Ingredients', [
            'through' => 'IngredientsProducts',
        ]);
    }
 }

class IngredientsProductsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsTo('Ingredients');
        $this->belongsTo('Products');
    }
} 
Run Code Online (Sandbox Code Playgroud)

我想要完成的是,当我插入一个新产品时,我还想插入与该产品相关的每种成分的成分 ID 和字段数量,在成分产品连接表上。

我一直在阅读食谱,看到在 joiner 表上保存附加数据时(在我的情况下,字段 qty 如下所述),您必须使用 _joinData 属性,因此我的添加视图如下所示:

    <?php 

    $this->Form->create($product);
    // fields of the Products table
    echo $this->Form->control('name',array('class' => 'form-control'));
    echo $this->Form->control('retail_price');
    echo $this->Form->control('best_before');
    echo $this->Form->control('comments');

    // ingredient_id and qty fields of the ingredients_products table
    echo $this->Form->control('ingredients.0._joinData.ingredient_id',['options' => $ingredients);

    echo $this->Form->control('ingredients.0._joinData.qty');

    //and repetition of these last two as ingredients.1._joinData to ingredients.N._joinData

$this->Form->button(__('Save'));
    $this->Form->end();

    ?>
Run Code Online (Sandbox Code Playgroud)

在控制器上,add 方法如下所示:

$product = $this->Products->newEntity();
        if ($this->request->is('post')) {

            $product = $this->Products->patchEntity($product, $this->request->getData(),[
                'associated' => ['Ingredients._joinData']
            ]);


            if ($this->Products->save($product)) {
                $this->Flash->success(__('The product has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The product could not be saved. Please, try again.'));
        }
        $this->set(compact('product'));
        $this->set('_serialize', ['product']);
Run Code Online (Sandbox Code Playgroud)

但是,当我提交时,它不会保存任何内容。调试显示以下正在发布:

object(App\Model\Entity\Product) {

    'name' => 'salada',
    'retail_price' => (float) 23,
    'best_before' => (int) 234,
    'comments' => 'wer',
    'directions' => 'werwewerer',
    'ingredients' => [
        (int) 0 => object(App\Model\Entity\Ingredient) {

            '_joinData' => object(Cake\ORM\Entity) {

                'ingredient_id' => (int) 4,
                'qty' => (float) 100,
                '[new]' => true,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [
                    'ingredient_id' => true,
                    'qty' => true
                ],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[invalid]' => [],
                '[repository]' => 'IngredientsProducts'

            },
            '[new]' => true,
            '[accessible]' => [
                '*' => true,
                'id' => false
            ],
            '[dirty]' => [
                '_joinData' => true
            ],
            '[original]' => [
                '_joinData' => [
                    'ingredient_id' => '4',
                    'qty' => '100'
                ]
            ],
            '[virtual]' => [],
            '[errors]' => [
                'name' => [
                    '_required' => 'This field is required'
                ]
            ],
            '[invalid]' => [],
            '[repository]' => 'Ingredients'

        },
        (int) 1 => object(App\Model\Entity\Ingredient) {

            '_joinData' => object(Cake\ORM\Entity) {

                'ingredient_id' => (int) 5,
                'qty' => (float) 200,
                '[new]' => true,
                '[accessible]' => [
                    '*' => true
                ],
                '[dirty]' => [
                    'ingredient_id' => true,
                    'qty' => true
                ],
                '[original]' => [],
                '[virtual]' => [],
                '[errors]' => [],
                '[invalid]' => [],
                '[repository]' => 'IngredientsProducts'

            },
            '[new]' => true,
            '[accessible]' => [
                '*' => true,
                'id' => false
            ],
            '[dirty]' => [
                '_joinData' => true
            ],
            '[original]' => [
                '_joinData' => [
                    'ingredient_id' => '5',
                    'qty' => '200'
                ]
            ],
            '[virtual]' => [],
            '[errors]' => [
                'name' => [
                    '_required' => 'This field is required'
                ]
            ],
            '[invalid]' => [],
            '[repository]' => 'Ingredients'

        }
    ],
    '[new]' => true,
    '[accessible]' => [
        '*' => true,
        'id' => false
    ],
    '[dirty]' => [
        'name' => true,
        'retail_price' => true,
        'best_before' => true,
        'comments' => true,
        'directions' => true,
        'ingredients' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Products'

} 
Run Code Online (Sandbox Code Playgroud)

有没有人碰巧知道如何使这项工作?

以防万一这里是 mySQL 上的表结构:

TABLE `Ingredients` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `category_id` int(11) NOT NULL,
  `measure_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Table products
TABLE `Products` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `retail_price` float NOT NULL,
  `best_before` int(11) NOT NULL,
  `comments` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



TABLE `ingredients_products` (
  `ingredient_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `qty` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Run Code Online (Sandbox Code Playgroud)

任何帮助或指示将不胜感激!谢谢

ndm*_*ndm 6

保存失败时,检查errors信息

每当保存实体不起作用时,请检查errors信息:

'[errors]' => [
    'name' => [
        '_required' => 'This field is required'
    ]
],
Run Code Online (Sandbox Code Playgroud)

您的验证规则Ingredients.name根据需要定义该字段,但它不存在于您的表单中,因此保存失败。

提供目标关联的主键

仔细查看文档中保存连接数据的示例,数据结构看起来与您的略有不同。

从你的使用ingredient_id我怀疑你想增加产品链接到现有的成分,将ingredient_id_joinData不过是不是这个是如何工作的。编辑/链接现有记录要求记录的主键出现在特殊_ids键中或目标关联的主键字段中 ( Ingredients)。

因此,在您想要存储其他连接数据的情况下,您必须使用id该成分的属性,这样编组器就知道它需要加载现有记录:

echo $this->Form->control('ingredients.0.id', [
    // defining the type is required, as the input type
    // guessing only recognizes `_ids` fields, or fields
    // with `_id` appended as possible select types
    'type' => 'select'
    'options' => $ingredients
]);
echo $this->Form->control('ingredients.0._joinData.qty');
Run Code Online (Sandbox Code Playgroud)

也可以看看