使用Cakephp 3.5添加具有多个类别的产品

时间:2018-03-04 15:36:20

标签: php sql cakephp-3.0 categories product

我正在为电子商务项目工作,我有一些问题

每个产品可以属于多个类别

这是我的数据库表:

+-------+-------+
|    Product    |
+-------+--------
|product_id (PK)|
+---------------+
|name           |
+---------------+

+-------+--------+
|    Category    |
+-------+--------+
|category_id (PK)|
+----------------+
|name            |
+----------------+

+-------+--------+
Products_Category|
+-------+--------+
|product_id (PK) |
+----------------+
|category_id (PK)|
+----------------+

这是我的代码ProductsController:

   public function add()
    {
        $product= $this->Products->newEntity();
        if ($this->request->is('post')) {
            $product= $this->Products->patchEntity($product, $this->request->getData());
            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.'));
        }
        $users = $this->Products->Users->find('list', ['limit' => 200]);

        $this->loadModel('Categories');
        $categories =$this->Categories->find('all');


        $this->set(compact('product', 'users', 'categories'));
    }

添加产品时遇到问题

产品在产品表中成功添加

但我还需要将product_id和category_id添加到Products_Category表

我正在使用cakephp框架

0 个答案:

没有答案