Skip to content

Commit

Permalink
Model::create() clarifications
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Sep 20, 2012
1 parent 7678523 commit fac4163
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 8 additions & 3 deletions en/models/saving-your-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Otherwise a new record is created::

.. tip::

When calling save in a loop, don't forget to call ``create()``
When calling save in a loop, don't forget to call ``create()``.


If you want to update a value, rather than create a new one, make sure
Expand All @@ -171,6 +171,11 @@ set, it will only reset fields that have already been set, and
leave the rest unset. Use this to avoid updating fields in the
database that were already set.

.. tip::

If you want to insert a new row instead of updating an existing one you should always call create() first.
This avoids conflicts with possible prior save calls in callbacks or other places.

:php:meth:`Model::saveField(string $fieldName, string $fieldValue, $validate = false)`
======================================================================================

Expand Down Expand Up @@ -387,7 +392,7 @@ And save this data with::
$Article->saveAssociated($data, array('deep' => true));

.. versionchanged:: 2.1
``Model::saveAll()`` and friends now support passing the `fieldList` for multiple models.
``Model::saveAll()`` and friends now support passing the `fieldList` for multiple models.

Example of using ``fieldList`` with multiple models::

Expand Down Expand Up @@ -680,7 +685,7 @@ passed to ``save()`` for the Tag model is shown below::
(
[id] => 42
)
[Tag] => Array
[Tag] => Array
(
[name] => Italian
)
Expand Down
5 changes: 3 additions & 2 deletions en/tutorials-and-examples/blog/part-two.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ nice table, our view code might look something like this::
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'],
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
Expand Down Expand Up @@ -278,6 +278,7 @@ PostsController:

public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
Expand Down Expand Up @@ -382,7 +383,7 @@ requirements? Validation rules are defined in the model. Let's look
back at our Post model and make a few adjustments::

<?php
class Post extends AppModel {
class Post extends AppModel {
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
Expand Down

0 comments on commit fac4163

Please sign in to comment.