Search Engine

Saturday, May 15, 2010

Adding Posts

Reading from the database and showing us the posts is a great start, but let's allow for the adding of new posts.

First, start by creating an add() action in the PostsController:

Plain Text View
class PostsController extends AppController {

var $name = 'Posts';

function index() {
$this->set('posts', $this->Post->find('all'));

function view($id) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());


function add() {
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
  1. class PostsController extends AppController {
  2. var $name = 'Posts';
  3. function index() {
  4. $this->set('posts', $this->Post->find('all'));
  5. }
  6. function view($id) {
  7. $this->Post->id = $id;
  8. $this->set('post', $this->Post->read());
  9. }
  10. function add() {
  11. if (!empty($this->data)) {
  12. if ($this->Post->save($this->data)) {
  13. $this->Session->setFlash('Your post has been saved.');
  14. $this->redirect(array('action' => 'index'));
  15. }
  16. }
  17. }
  18. }
  19. ?>

Here's what the add() action does: if the submitted form data isn't empty, try to save the data using the Post model. If for some reason it doesn't save, just render the view. This gives us a chance to show the user validation errors or other warnings.

When a user uses a form to POST data to your application, that information is available in $this->data. You can use the pr() or debug functions to print it out if you want to see what it looks like.

We use the Session component's setFlash() function to set a message to a session variable to be displayed on the page after redirection. In the layout we have $session->flash() which displays the message and clears the corresponding session variable. The controller's redirect function redirects to another URL. The param array('action'=>'index) translates to URL /posts i.e the index action of posts controller. You can refer to Router::url function on the api to see the formats in which you can specify a URL for various cake functions.

Calling the save() method will check for validation errors and abort the save if any occur. We'll discuss how those errors are handled in the following sections.


Post a Comment