10.1.12 Editing Posts
Post editing: here we go. You're a CakePHP pro by now, so you
should have picked up a pattern. Make the action, then the view. Here's
what the edit() action of the PostsController would look like:
function edit($id = null) {
$this->Post->id = $id;
if (empty($this->data)) {
$this->data = $this->Post->read();
} else {
if ($this->Post->save($this->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
function edit($id = null) {$this->Post->id = $id;if (empty($this->data)) {$this->data = $this->Post->read();} else {if ($this->Post->save($this->data)) {$this->Session->setFlash('Your post has been updated.');$this->redirect(array('action' => 'index'));}}}
This action first checks for submitted form data. If nothing was submitted, it finds the Post and hands it to the view. If some data has been submitted, try to save the data using Post model (or kick back and show the user the validation errors).
The edit view might look something like this:
<!-- File: /app/views/posts/edit.ctp -->
<h1>Edit Post</h1>
<?php
echo $form->create('Post', array('action' => 'edit'));
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->input('id', array('type'=>'hidden'));
echo $form->end('Save Post');
?>
<!-- File: /app/views/posts/edit.ctp --><h1>Edit Post</h1><?phpecho $form->create('Post', array('action' => 'edit'));echo $form->input('title');echo $form->input('body', array('rows' => '3'));echo $form->input('id', array('type'=>'hidden'));echo $form->end('Save Post');?>
This view outputs the edit form (with the values populated), along with any necessary validation error messages.
One thing to note here: CakePHP
will assume that you are editing a model if the 'id' field is present in the data array. If no 'id' is present (look back
at our add view), Cake will assume that you are inserting a new model
when save() is called.
You can now update your index view with links to edit specific posts:
<!-- File: /app/views/posts/index.ctp (edit links added) -->
<h1>Blog posts</h1>
<p><?php echo $html->link("Add Post", array('action'=>'add')); ?>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Action</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $html->link($post['Post']['title'],array('action'=>'view', 'id'=>$post['Post']['id']));?>
</td>
<td>
<?php echo $html->link(
'Delete',
array('action'=>'delete', 'id'=>$post['Post']['id']),
null,
'Are you sure?'
)?>
<?php echo $html->link('Edit', array('action'=>'edit', 'id'=>$post['Post']['id']));?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<!-- File: /app/views/posts/index.ctp (edit links added) --><h1>Blog posts</h1><p><?php echo $html->link("Add Post", array('action'=>'add')); ?><table><tr><th>Id</th><th>Title</th><th>Action</th><th>Created</th></tr><!-- Here's where we loop through our $posts array, printing out post info --><?php foreach ($posts as $post): ?><tr><td><?php echo $post['Post']['id']; ?></td><td><?php echo $html->link($post['Post']['title'],array('action'=>'view', 'id'=>$post['Post']['id']));?></td><td><?php echo $html->link('Delete',array('action'=>'delete', 'id'=>$post['Post']['id']),null,'Are you sure?')?><?php echo $html->link('Edit', array('action'=>'edit', 'id'=>$post['Post']['id']));?></td><td><?php echo $post['Post']['created']; ?></td></tr><?php endforeach; ?></table>


























