Cake\ORM\Table::newEntity PHP Method

newEntity() public method

By default all the associations on this table will be hydrated. You can limit which associations are built, or include deeper associations using the options parameter: $article = $this->Articles->newEntity( $this->request->data(), ['associated' => ['Tags', 'Comments.Users']] ); You can limit fields that will be present in the constructed entity by passing the fieldList option, which is also accepted for associations: $article = $this->Articles->newEntity($this->request->data(), [ 'fieldList' => ['title', 'body', 'tags', 'comments'], 'associated' => ['Tags', 'Comments.Users' => ['fieldList' => 'username']] ] ); The fieldList option lets remove or restrict input data from ending up in the entity. If you'd like to relax the entity's default accessible fields, you can use the accessibleFields option: $article = $this->Articles->newEntity( $this->request->data(), ['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. You can use the Model.beforeMarshal event to modify request data before it is converted into entities.
public newEntity ( $data = null, array $options = [] )
$options array
    public function newEntity($data = null, array $options = [])
    {
        if ($data === null) {
            $class = $this->entityClass();
            $entity = new $class([], ['source' => $this->registryAlias()]);
            return $entity;
        }
        if (!isset($options['associated'])) {
            $options['associated'] = $this->_associations->keys();
        }
        $marshaller = $this->marshaller();
        return $marshaller->one($data, $options);
    }

Usage Example

 /**
  * Helper generator for use with importTable().
  *
  * Yields a single new Entity instance approciate for $Table for each
  * of $records where the values are merged with $defaults.
  *
  * Will skip any records that fail to validate, dumping validation
  * errors to the console in the process.
  *
  * Used by imporTables().
  *
  * @param Cake\ORM\Table $Table A Table instance to save records into.
  * @param array $records An array of Entity records to save into the Table.
  * @param array $defaults Optional array of default field values to merge into each record.
  * @param array $options Optional array of newEntity() options to use.
  * @return void
  */
 public function entityGenerator(Table $Table, array $records, array $defaults = [], array $options = [])
 {
     $defaultOptions = ['validate' => true, 'accessibleFields' => ['*' => true]];
     $options = $options + $defaultOptions;
     $keyField = $Table->primaryKey();
     foreach ($records as $r) {
         $r = Hash::merge($defaults, $r);
         $id = !empty($r[$keyField]) ? $r[$keyField] : false;
         if ($id) {
             $entity = $Table->find()->where([$keyField => $id])->first();
             if ($entity) {
                 $entity = $Table->patchEntity($entity, $r, $options);
                 if (!$entity->dirty()) {
                     $this->verbose("<success>{$Table->alias()} ({$id}): No changes.</success>");
                     continue;
                 }
             } else {
                 $entity = $Table->newEntity($r, $options);
                 $entity->isNew(true);
             }
         } else {
             $entity = $Table->newEntity($r, $options);
         }
         $errors = $entity->errors();
         if ($errors) {
             $this->printValidationErrors($Table->alias(), $id, $errors);
             continue;
         }
         (yield $entity);
     }
 }
All Usage Examples Of Cake\ORM\Table::newEntity