Cake\ORM\Table::find PHP Method

find() public method

### Model.beforeFind event Each find() will trigger a Model.beforeFind event for all attached listeners. Any listener can set a valid result set using $query By default, $options will recognize the following keys: - fields - conditions - order - limit - offset - page - group - having - contain - join ### Usage Using the options array: $query = $articles->find('all', [ 'conditions' => ['published' => 1], 'limit' => 10, 'contain' => ['Users', 'Comments'] ]); Using the builder interface: $query = $articles->find() ->where(['published' => 1]) ->limit(10) ->contain(['Users', 'Comments']); ### Calling finders The find() method is the entry point for custom finder methods. You can invoke a finder by specifying the type: $query = $articles->find('published'); Would invoke the findPublished method.
public find ( $type = 'all', $options = [] ) : Query
return Query The query builder
    public function find($type = 'all', $options = [])
    {
        $query = $this->query();
        $query->select();
        return $this->callFinder($type, $query, $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::find