CakePHP 3.4如何在一个属于并且有很多关系的

CakePHP 3.4如何在一个属于并且有很多关系的,cakephp,save,associations,has-and-belongs-to-many,cakephp-3.x,Cakephp,Save,Associations,Has And Belongs To Many,Cakephp 3.x,在cakePHP 3.4上,我有3个表,其中一个表属于,并且有很多关系:配料、产品和IngCreditsProducts: class IngredientsTable extends Table { public function initialize(array $config) { // Use through option because it looks like you // have additional data on your

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

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');
    }
} 
我想完成的是,当我插入一个新产品时,我还想在IngreditsProductsjoiner表中插入与该产品相关的每个成分的成分id和字段数量

我一直在阅读烹饪书,看到在joiner表上保存传统数据(在我的例子中,字段数量如下所述)时,必须使用_joinData属性,因此我的add视图如下所示:

    <?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();

    ?>
$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']);
但是,当我提交时,它不会保存任何内容。调试将显示正在发布的以下内容:

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'

} 
有人知道怎么做吗

以下是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;
任何帮助或指导都将不胜感激! 谢谢

如果保存失败,请检查
错误
信息 当保存实体无效时,请检查
错误
信息:

'[errors]' => [
    'name' => [
        '_required' => 'This field is required'
    ]
],
您的验证规则根据需要定义
成分.name
字段,但它不在表单中,因此保存失败

提供目标关联上的主键 仔细看看文档中保存连接数据的示例,数据结构与您的略有不同

从您对
配料id
的使用情况来看,我怀疑您想将产品与现有配料联系起来,将
配料id
放在
\u joinData
中,但这不是有效的方法。编辑/链接现有记录要求记录的主键出现在特殊的
\u id
键中,或出现在目标关联(
成分
)的主键字段中

因此,在您希望存储其他联接数据的情况下,必须使用配料的
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');
另见

我非常感谢您的帮助和所有信息。现在开始工作了,非常感谢!