Browse Source

Allowing null to be passed to newEntity()

This helps creting new entities without running any validation
Jose Lorenzo Rodriguez 11 years ago
parent
commit
8bb72e2f84
3 changed files with 49 additions and 3 deletions
  1. 2 2
      src/Datasource/RepositoryInterface.php
  2. 30 1
      src/ORM/Table.php
  3. 17 0
      tests/TestCase/ORM/TableTest.php

+ 2 - 2
src/Datasource/RepositoryInterface.php

@@ -145,11 +145,11 @@ interface RepositoryInterface {
  * 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.

+ 30 - 1
src/ORM/Table.php

@@ -1742,8 +1742,27 @@ class Table implements RepositoryInterface, EventListenerInterface {
  *   ['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();
 		}
@@ -1802,6 +1821,16 @@ class Table implements RepositoryInterface, EventListenerInterface {
  *	]
  * );
  * }}}
+ *
+ * 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'])) {

+ 17 - 0
tests/TestCase/ORM/TableTest.php

@@ -2100,6 +2100,23 @@ class TableTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void