_defaultTable = 'CakeProducts.Products'; // $this->_tableConfigKey = 'CakeProducts.Products.table'; } /** * Index method * * @return \Cake\Http\Response|null|void Renders view */ public function index() { $query = $this->getTable()->find() ->contain(['ProductCategories']); $products = $this->paginate($query); $this->set(compact('products')); } /** * View method * * @param string|null $id Product id. * @return \Cake\Http\Response|null|void Renders view * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function view($id = null) { $product = $this->getTable()->get($id, contain: [ 'ProductCategories', 'ProductAttributes', 'ProductAttributes.ProductCategoryAttributes', 'ProductAttributes.ProductCategoryAttributeOptions', 'ProductCategoryVariants', 'ProductSkus' ]); $this->set(compact('product')); } /** * Add method * * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. */ public function add() { $productsTable = $this->getTable(); $product = $productsTable->newEmptyEntity(); if ($this->request->is('post')) { $postData = $this->request->getData(); $saveOptions = [ 'associated' => ['ProductAttributes'], ]; // Log::debug(print_r('$postData', true)); // Log::debug(print_r($postData, true)); // Log::debug(print_r('$saveOptions', true)); // Log::debug(print_r($saveOptions, true)); $product = $productsTable->patchEntity($product, $postData, $saveOptions); if ($productsTable->save($product, $saveOptions)) { $this->Flash->success(__('The product has been saved.')); return $this->redirect(['action' => 'index']); } Log::debug(print_r('$product->getErrors() next - failed in products/add', true)); Log::debug(print_r($product->getErrors(), true)); $this->Flash->error(__('The product could not be saved. Please, try again.')); } $productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null; $productCatalogs = $productsTable->ProductCategories->ProductCatalogs->find('list')->toArray(); $this->set(compact('product', 'productCatalogs', 'productCategory')); } /** * Edit method * * @param string|null $id Product id. * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function edit($id = null) { $productsTable = $this->getTable(); $product = $productsTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { $product = $productsTable->patchEntity($product, $this->request->getData()); if ($productsTable->save($product)) { $this->Flash->success(__('The product has been saved.')); return $this->redirect(['action' => 'index']); } Log::debug(print_r('$product->getErrors() next - failed in products/edit', true)); Log::debug(print_r($product->getErrors(), true)); $this->Flash->error(__('The product could not be saved. Please, try again.')); } $productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null; $productCatalogs = $productsTable->ProductCategories->ProductCatalogs->find('list')->toArray(); $this->set(compact('product', 'productCatalogs', 'productCategory')); } /** * Delete method * * @param string|null $id Product id. * @return \Cake\Http\Response|null Redirects to index. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); $productsTable = $this->getTable(); $product = $productsTable->get($id); if ($productsTable->delete($product)) { $this->Flash->success(__('The product has been deleted.')); } else { $this->Flash->error(__('The product could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']); } }