/** * Deletes all products associated with this category prior to deleting the category * @see testProjectModel::beforeDelete() */ public function beforeDelete() { $products = products::model()->getAll(); foreach ($products as $product) { if ($product->category->id == $this->id) { $product->delete(); } } return true; }
public function actionUpdateProduct() { $errors = array(); if (!isset($_GET['value'])) { $errors[] = 'A product ID must be supplied'; } else { if (!is_numeric($_GET['value'])) { $errors[] = "Provided product ID must be an integer"; } else { if ($product = products::model()->getbyPK($_GET['value'])) { $updateAttributes = array('name' => null, 'description' => null, 'price' => null, 'category_id' => null); foreach ($updateAttributes as $updateAttribute => $value) { if (isset($_GET[$updateAttribute])) { $updateAttributes[$updateAttribute] = $_GET[$updateAttribute]; } else { unset($updateAttributes[$updateAttribute]); } } $product->setAttributes($updateAttributes); if ($product->save()) { $attributes = array('id', 'name', 'created', 'modified', 'description'); $datum = $this->returnValues($product, $attributes); $datum['category_name'] = $product->category->name; $datum['category_id'] = $product->category->id; $data = array($datum); } else { $errors[] = 'The product could not be updated'; } } else { $errors[] = 'Could not find product with ID of ' . $_GET['value']; } } } if (count($errors) > 0) { $this->renderApi(false, null, $errors); } else { $this->renderApi(true, $data); } }
/** * Deletes a product * * If no product ID is passed or if it does not match a valid product ID in the database, an error * message is generated. Otherwise the product is deleted and a success message is generated. In either * case the user is redirected to the products index. */ public function actionDelete() { testProject::setAlert('There was a problem with your request. Please try again.', 'error'); if (isset($_GET['value'])) { //check if ID is provided $model = products::model()->getByPK($_GET['value']); $name = $model->name; if ($model->delete()) { //attempt to delete the item testProject::setAlert('The item ' . $name . ' was deleted.', 'info'); } } $this->redirect('products/index'); }