小编Tzi*_*Tzi的帖子

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

在 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 …

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

3
推荐指数
1
解决办法
3049
查看次数