Skip to content
Permalink
Browse files

Add table methods to hydrate entities from request data.

  • Loading branch information...
markstory committed Dec 11, 2013
1 parent fcf5ad5 commit bd5029a93a7cab51a313e9edb01111b1a759b730
Showing with 63 additions and 0 deletions.
  1. +63 −0 Cake/ORM/Table.php
@@ -27,6 +27,7 @@
use Cake\ORM\BehaviorRegistry;
use Cake\ORM\Entity;
use Cake\ORM\Error\MissingEntityException;
use Cake\ORM\Marshaller;
use Cake\Utility\Inflector;
use Cake\Validation\Validator;
@@ -1502,6 +1503,68 @@ public function __call($method, $args) {
);
}
/**
* Get the object used to marshal/convert array data into objects.
*
* Override this method if you want a table object to use custom
* marshalling logic.
*
* @return Cake\ORM\Marhsaller;
*/
public function marshaller() {
return new Marshaller($this);
}
/**
* Create a new entity + associated entities from an array.
*
* This is most useful when hydrating request data back into entities.
* For example, in your controller code:
*
* {{{
* $article = $this->Articles->newEntity($this->request->data());
* }}}
*
* The hydrated entity will correctly do an insert/update based
* on the primary key data existing in the database when the entity
* is saved. Until the entity is saved, it will be a detached record.
*
* @param array $data The data to build an entity with.
* @param array $associations A whitelist of associations
* to hydrate. Defaults to all associations
*/
public function newEntity(array $data, $associations = null) {
if ($associations === null) {
$associations = array_keys($this->_associations);
}
$marshaller = $this->marshaller();
return $marshaller->one($data, $associations);
}
/**
* Create a list of entities + associated entities from an array.
*
* This is most useful when hydrating request data back into entities.
* For example, in your controller code:
*
* {{{
* $articles = $this->Articles->newEntities($this->request->data());
* }}}
*
* The hydrated entities can the be iterated and saved.
*
* @param array $data The data to build an entity with.
* @param array $associations A whitelist of associations
* to hydrate. Defaults to all associations
*/
public function newEntities(array $data, $associations = null) {
if ($associations === null) {
$associations = array_keys($this->_associations);
}
$marshaller = $this->marshaller();
return $marshaller->many($data, $associations);
}
/**
* Get the Model callbacks this table is interested in.
*

0 comments on commit bd5029a

Please sign in to comment.
You can’t perform that action at this time.