/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aBlogTag !== null) { if ($this->aBlogTag->isModified() || $this->aBlogTag->isNew()) { $affectedRows += $this->aBlogTag->save($con); } $this->setBlogTag($this->aBlogTag); } if ($this->aBlogArticle !== null) { if ($this->aBlogArticle->isModified() || $this->aBlogArticle->isNew()) { $affectedRows += $this->aBlogArticle->save($con); } $this->setBlogArticle($this->aBlogArticle); } // If this object has been modified, then save it to the database. if ($this->isModified()) { if ($this->isNew()) { $pk = BlogTagArticlePeer::doInsert($this, $con); $affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which // should always be true here (even though technically // BasePeer::doInsert() can insert multiple rows). $this->setNew(false); } else { $affectedRows += BlogTagArticlePeer::doUpdate($this, $con); } $this->resetModified(); // [HL] After being saved an object is no longer 'modified' } $this->alreadyInSave = false; } return $affectedRows; }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // Declare the rules for the form validation $rules = array('title' => 'required|min:3', 'content' => 'required|min:3|not_in:<p></p>', 'image' => 'image|max:3000000'); // Create a new validator instance from our validation rules $validator = Validator::make(Input::all(), $rules); // If validation fails, we'll exit the operation now. if ($validator->fails()) { // Ooops.. something went wrong return Redirect::back()->withInput()->withErrors($validator); } // Create a new blog post $post = BlogPost::find($id); // Update the blog post data $post->title = e(Input::get('title')); if (empty($post->slug)) { $post->slug = e(Str::slug(Input::get('title'))); } else { $post->slug = e(Str::slug(Input::get('slug'))); } $post->content = e(Input::get('content')); $post->draft = e(Input::get('draft')); $post->lang = e(Input::get('lang')); $post->content = e(Input::get('content')); $post->meta_title = e(Input::get('meta-title')); $post->meta_description = e(Input::get('meta-description')); $post->meta_keywords = e(Input::get('meta-keywords')); $image = Input::file('image'); if (!empty($image)) { unlink($this->destinationPath . $post->image); $filename = $post->slug . '_' . date('d-m-Y') . '.' . $image->getClientOriginalExtension(); $uploadSuccess = Input::file('image')->move($this->destinationPath, $filename); $post->image = e($filename); } // Was the blog post created? if ($post->save()) { $post->tags()->detach(); $tags = explode(',', Input::get('blogtags')); foreach ($tags as $tag) { if (BlogTag::where('name', '=', $tag)->count() == 0) { $newTag = new BlogTag(); $newTag->name = ucwords($tag); $newTag->slug = Str::slug($tag); $newTag->save(); } else { $newTag = BlogTag::where('name', '=', $tag)->first(); } $post->tags()->attach($newTag->id); } // Redirect to the new blog post page return Redirect::route('blog.show', array('blog' => $post->slug))->with('success', Lang::get('modules/blog/messages.success.edit')); } // Redirect to the blog post create page return Redirect::route('blog.create')->with('error', Lang::get('modules/blog/messages.error.edit')); }