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

时间:2017-04-08 11:00:19

标签: 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');
    }
} 

我想要实现的是,当我插入新产品时,我想在 IngredientsProducts 木匠表。

我一直在阅读这本食谱,并且看到在将joean表数据保存在joiner表上时(在我的情况下是如下所述的字段数量),你必须使用_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();

    ?>

并且在控制器上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']);

然而,当我提交时,它并没有保存任何东西。调试显示以下内容:

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;

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

1 个答案:

答案 0 :(得分:3)

保存失败时,请检查errors信息

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

'[errors]' => [
    'name' => [
        '_required' => 'This field is required'
    ]
],

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

提供目标关联的主键

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

根据您对ingredient_id的使用情况,我怀疑您想要将产品与现有成分相关联,将ingredient_id放在_joinData中,但这不是如何运作的。编辑/链接现有记录要求记录的主键存在于特殊_ids键或目标关联的主键字段(Ingredients)中。

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

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');

另见