/** * Creates a new post if all inputs are valid. * This method responds to the OnClick event of the "create" button. * @param mixed event sender * @param mixed event parameter */ public function createButtonClicked($sender, $param) { if ($this->IsValid) { // populates a PostRecord object with user inputs $postRecord = new PostRecord(); // using SafeText instead of Text avoids Cross Site Scripting attack $postRecord->title = $this->TitleEdit->SafeText; $postRecord->content = $this->ContentEdit->SafeText; $postRecord->author_id = $this->User->Name; $postRecord->create_time = time(); $postRecord->status = 0; // saves to the database via Active Record mechanism $postRecord->save(); // redirects the browser to the newly created post page $url = $this->Service->constructUrl('posts.ReadPost', array('id' => $postRecord->post_id)); $this->Response->redirect($url); } }
/** * Fetches posts from database with offset and limit. */ protected function getPosts($offset, $limit) { // Construts a query criteria $criteria = new TActiveRecordCriteria(); $criteria->OrdersBy['create_time'] = 'desc'; $criteria->Limit = $limit; $criteria->Offset = $offset; // query for the posts with the above criteria and with author information return PostRecord::finder()->withAuthor()->findAll($criteria); }
/** * Returns the post data to be editted. * @return PostRecord the post data to be editted. * @throws THttpException if the post data is not found. */ protected function getPost() { // the ID of the post to be editted is passed via GET parameter 'id' $postID = (int) $this->Request['id']; // use Active Record to look for the specified post ID $postRecord = PostRecord::finder()->findByPk($postID); if ($postRecord === null) { throw new THttpException(500, 'Post is not found.'); } return $postRecord; }
/** * Fetches the post data. * This method is invoked by the framework when initializing the page * @param mixed event parameter */ public function onInit($param) { parent::onInit($param); // post id is passed via the 'id' GET parameter $postID = (int) $this->Request['id']; // retrieves PostRecord with author information filled in $this->_post = PostRecord::finder()->withAuthor()->findByPk($postID); if ($this->_post === null) { // if post id is invalid throw new BlogException(500, 'Unable to find the specified post.'); } // set the page title as the post title $this->Title = $this->_post->title; }