yii\db\ActiveRecordInterface::find PHP Method

find() public static method

The returned ActiveQueryInterface instance can be further customized by calling methods defined in ActiveQueryInterface before one() or all() is called to return populated ActiveRecord instances. For example, php find the customer whose ID is 1 $customer = Customer::find()->where(['id' => 1])->one(); find all active customers and order them by their age: $customers = Customer::find() ->where(['status' => 1]) ->orderBy('age') ->all(); This method is also called by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to create a relational query. You may override this method to return a customized query. For example, php class Customer extends ActiveRecord { public static function find() { use CustomerQuery instead of the default ActiveQuery return new CustomerQuery(get_called_class()); } } The following code shows how to apply a default condition for all queries: php class Customer extends ActiveRecord { public static function find() { return parent::find()->where(['deleted' => false]); } } Use andWhere()/orWhere() to apply the default condition SELECT FROM customer WHERE deleted`=:deleted AND age>30 $customers = Customer::find()->andWhere('age>30')->all(); Use where() to ignore the default condition SELECT FROM customer WHERE age>30 $customers = Customer::find()->where('age>30')->all();
public static find ( ) : yii\db\ActiveQueryInterface
return yii\db\ActiveQueryInterface the newly created [[ActiveQueryInterface]] instance.
    public static function find();

Usage Example

 /**
  * Find max index stored in database.
  *
  * If no index found, startIndex - 1 will be returned.
  *
  * @param  ActiveRecordInterface $model
  * @param  string                $attribute
  * @param  string                $commonPart
  * @return integer
  */
 protected function findMaxIndex(ActiveRecordInterface $model, $attribute, $commonPart)
 {
     // Find all possible max values.
     $db = $model::getDb();
     $indexExpression = 'SUBSTRING(' . $db->quoteColumnName($attribute) . ', :commonPartOffset)';
     $query = $model->find()->select(['_index' => $indexExpression])->andWhere($this->getExcludeByPkCondition($model))->andWhere(['like', $attribute, $commonPart])->andHaving(['not in', '_index', [0]])->orderBy(['CAST(' . $db->quoteColumnName('_index') . ' AS UNSIGNED)' => SORT_DESC])->addParams(['commonPartOffset' => mb_strlen($commonPart) + 1])->asArray();
     $this->addFilterToQuery($query);
     foreach ($query->each() as $row) {
         $index = $row['_index'];
         if (!preg_match('/^\\d+$/', $index)) {
             continue;
         }
         return $index;
     }
     return $this->startIndex - 1;
 }