Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allowing null to be passed to newEntity()
This helps creting new entities without running any validation
  • Loading branch information
lorenzo committed Dec 29, 2014
1 parent 831ec80 commit 8bb72e2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Datasource/RepositoryInterface.php
Expand Up @@ -145,11 +145,11 @@ public function delete(EntityInterface $entity, $options = []);
* 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|null $data The data to build an entity with.
* @param array $options A list of options for the object hydration.
* @return \Cake\Datasource\EntityInterface
*/
public function newEntity(array $data = [], array $options = []);
public function newEntity($data = null, array $options = []);

/**
* Create a list of entities + associated entities from an array.
Expand Down
31 changes: 30 additions & 1 deletion src/ORM/Table.php
Expand Up @@ -1742,8 +1742,27 @@ public function marshaller() {
* ['accessibleFields' => ['protected_field' => true]]
* );
* }}}
*
* By default, the data is validated before being passed to the new entity. In
* the case of invalid fields, those will not be present in the resulting object.
* The `validate` option can be used to disable validation on the passed data:
*
* {{{
* $article = $this->Articles->newEntity(
* $this->request->data(),
* ['validate' => false]
* );
* }}}
*
* You can also pass the name of the validator to use in the `validate` option.
* If `null` is passed to the first param of this function, no validation will
* be performed.
*/
public function newEntity(array $data = [], array $options = []) {
public function newEntity($data = null, array $options = []) {
if ($data === null) {
$class = $this->entityClass();
return new $class;
}
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}
Expand Down Expand Up @@ -1802,6 +1821,16 @@ public function newEntities(array $data, array $options = []) {
* ]
* );
* }}}
*
* By default, the data is validated before being passed to the entity. In
* the case of invalid fields, those will not be assigned to the entity.
* The `validate` option can be used to disable validation on the passed data:
*
* {{{
* $article = $this->patchEntity($article, $this->request->data(),[
* 'validate' => false
* ]);
* }}}
*/
public function patchEntity(EntityInterface $entity, array $data, array $options = []) {
if (!isset($options['associated'])) {
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -2099,6 +2099,23 @@ public function testValidatorSetter() {
$this->assertSame($table, $validator->provider('table'));
}

/**
* Tests that calling an entity with an empty array will run validation
* whereas calling it with no parameters will not run any validation.
*
* @return void
*/
public function testNewEntityAndValidation() {
$table = TableRegistry::get('Articles');
$validator = $table->validator()->requirePresence('title');
$entity = $table->newEntity([]);
$errors = $entity->errors();
$this->assertNotEmpty($errors['title']);

$entity = $table->newEntity();
$this->assertEmpty($entity->errors());
}

/**
* Test magic findByXX method.
*
Expand Down

0 comments on commit 8bb72e2

Please sign in to comment.