/** * Checks if the product is new, and sets the created and modified times appropriately, * prior to saving. * @see testProjectModel::beforeSave() */ public function beforeSave() { if ($this->isNew) { $this->created = date("Y-m-d H:i:s"); $this->modified = $this->created; $this->user = Auth::User()->id; } else { $this->modified = date("Y-m-d H:i:s"); } $tags = isset($this->attributes['tags']) ? $this->attributes['tags'] : array(); foreach ($tags as $i => $tag) { if (!is_numeric($tag)) { $tagModel = new tags(array('name' => $tag)); $tagModel->save(); $tags[$i] = $tagModel->id; } else { $tags[$i] = intval($tag); } } $this->tagIds = $tags; unset($this->attributes['tags']); if (!is_numeric($this->vendor)) { $vendor = new vendors(array('name' => $this->vendor)); $vendor->save(); $this->vendor = $vendor->id; } $this->cleanDates(array('purchaseDate', 'saleDate', 'soldDate')); return true; }
/** * Creates a new tag 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 tags($_POST['model']); if ($model->save()) { //redirect to tag details page if successful testProject::setAlert('The tag ' . $model->name . ' was successfully created.', 'success'); $this->redirect('tags/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'); } } else { $this->render('create'); } }