/** * Creates a new product in the database * * If no data has been posted, a form with the needed fields will be rendered. If the data has * been posted, an attempt will be made to save. If successful, the user will be redirected to the * 'view' page for the new record, otherwise they will be notified of the failure. */ public function actionCreate() { if (isset($_POST['model'])) { //check if data has been posted $model = new products($_POST['model']); if ($model->save()) { //redirect to product view if successful testProject::setAlert('The item ' . $model->name . ' was successfully created.', 'success'); $this->redirect("products/view/{$model->id}"); } else { //create the alert message to notify of failure testProject::setAlert('We\'re sorry, but your changes could not be saved. Please try again.', 'danger'); } } //show the form $categories = categories::model()->getAll(); $this->render('create', array('categories' => $categories)); }
public function actionCreateProduct() { $errors = array(); $apiAttributes = array('name' => '', 'description' => '', 'price' => '', 'category' => ''); $attributes = array_intersect_key($_GET, $apiAttributes); $product = new products($attributes); if (!$product->validate()) { $errors = $product->getValidationErrors(); } if (!$errors) { $product = new products(array('name' => $_GET['name'], 'description' => $_GET['description'], 'category' => $_GET['category'], 'price' => $_GET['price'])); if (!$product->save()) { $errors[] = 'The product could not be saved'; } else { $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); } } if ($errors) { $this->renderApi(false, null, $errors); } else { $this->renderApi(true, $data); } }