在CakePHP 3中保存属于自己的关系

问题描述 投票:0回答:2

我一直坚持这个问题一段时间了,我似乎无法找到我的代码有什么问题。我有以下3个表,其中零售商有许多客户,客户可以有许多零售商,这是CakePHP 3中的属于关系.2之间的关系存储在clients_retailers表中。

class ClientsTable extends Table
{
        ...
        $this->belongsToMany('Retailers', [
            'joinTable' => 'clients_retailers'
        ]);
}

class RetailersTable extends Table
{

    public function initialize(array $config)
    {
        ...

        $this->belongsToMany('Clients', [
            'joinTable' => 'clients_retailers'
        ]);
    }
}

class ClientsRetailersTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('clients_retailers');
        $this->displayField('client_id');
        $this->primaryKey(['client_id', 'retailer_id']);

        $this->belongsTo('Clients', [
            'foreignKey' => 'client_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Retailers', [
            'foreignKey' => 'retailer_id',
            'joinType' => 'INNER'
            ]);
        }
}

然后在客户端控制器的添加功能中,我试图在新客户端和零售商之间保存这种关系。

 public function add()
        {
            $client = $this->Clients->newEntity();
            if ($this->request->is('post')) {
                $retailer_id = $this->Auth->user('retailer_id');
                $retailer = [
                    'retailers' => [
                        [
                            'id' => $retailer_id
                        ]
                    ]
                ];
                $client = $this->Clients->patchEntity($client, $this->request->data);
                $client = $this->Clients->patchEntity($client, $retailer, ['associated' => [
                        'Retailers'
                    ]]);


                if ($this->Clients->save($client)) {
                    $this->Flash->success(__('The client has been saved.'));
                   return $this->redirect(['action' => 'index']);
                } else {
                    $this->Flash->error(__('The client could not be saved. Please, try again.'));
                }
            }
        }

新客户端永远不会添加到数据库中。我在网上看到它可能是一个验证问题所以我尝试使用['validate' => false],但它似乎没有改变任何东西。保存功能失败,没有错误。

最糟糕的是,当从api控制器中的另一个函数调用时,这个关系曾经被正确保存。我当然试过复制它,但它似乎不能在这个控制器中工作。有谁知道问题可能是什么。非常感谢!

更新:这是我想要保存的客户端实体值

object(App\Model\Entity\Client) {

    'firstname' => 'test',
    'lastname' => 'test',
    'email' => '[email protected]',
    'password' => 'hash',
    'retailers' => [
        (int) 0 => object(App\Model\Entity\Retailer) {

            'id' => '1',
            'name' => 'TestRetailer',
            'created' => object(Cake\I18n\Time) {

                'time' => '2017-12-20T20:13:58+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\Time) {

                'time' => '2017-12-20T20:13:58+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true,
                'id' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Retailers'

        }
    ],
    '[new]' => true,
    '[accessible]' => [
        '*' => true,
        'id' => true
    ],
    '[dirty]' => [
        'firstname' => true,
        'lastname' => true,
        'email' => true,
        'password' => true,
        'retailers' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Clients'

}
cakephp cakephp-3.0
2个回答
0
投票

您需要更改代码才能工作,

public function add()
        {
            $client = $this->Clients->newEntity();
            if ($this->request->is('post')) {
                $retailer_id = 'XXXXXX'
                $retailer = [
                    'retailers' => [
                        [
                            'id' => $retailer_id
                        ]
                    ]
                ];
                $client = $this->Clients->patchEntity($client, $this->request->data, ['validate' => false]);   //Validation false
                $client = $this->Clients->patchEntity($client, $retailer, ['associated' => [
                        'Retailers' => ['validate' => false]   //You need to disable validation here too
                    ]]);


                if ($this->Clients->save($client)) {
                    $this->Flash->success(__('The client has been saved.'));
                   return $this->redirect(['action' => 'index']);
                } else {
                    $this->Flash->error(__('The client could not be saved. Please, try again.'));
                }
            }
        }

0
投票

我终于弄明白了问题所在。我在客户端表的查找功能上有一个beforeFind行为,我没有意识到这导致保存失败

© www.soinside.com 2019 - 2024. All rights reserved.