Trait Yiisoft\ActiveRecord\Trait\RepositoryTrait
Trait to support static methods {@see find()}, {@see findOne()}, {@see findAll()}, {@see findBySql()} to find records.
For example:
use Yiisoft\ActiveRecord\ActiveRecord;
use Yiisoft\ActiveRecord\Trait\RepositoryTrait;
final class User extends ActiveRecord
{
use RepositoryTrait;
public int $id;
public bool $is_active;
}
$user = User::find(['id' => 1])->one();
$users = User::find(['is_active' => true])->limit(5)->all();
$user = User::findByPk(1);
$user = User::findOne(['id' => 1]);
$users = User::findAll(['is_active' => true]);
$users = User::findBySql('SELECT * FROM customer')->all();
Public Methods
| Method | Description | Defined By |
|---|---|---|
| find() | Returns an instance of {@see ActiveQueryInterface} instantiated by {@see ActiveRecordInterface::query()} method. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findAll() | Shortcut for {@see find()} method with calling {@see ActiveQueryInterface::all()} method to get all records. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findAllOrFail() | Shortcut for {@see findAll()} method with throwing {@see NotFoundException} if no records found. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findByPk() | Finds an ActiveRecord instance by the given primary key value. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findByPkOrFail() | Shortcut for {@see findByPk()} method with throwing {@see NotFoundException} if no records found. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findBySql() | Creates an {@see ActiveQueryInterface} instance with a given SQL statement. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findOne() | Shortcut for {@see find()} method with calling {@see ActiveQueryInterface::one()} method to get one record. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
| findOneOrFail() | Shortcut for {@see findOne()} method with throwing {@see NotFoundException} if no records found. | Yiisoft\ActiveRecord\Trait\RepositoryTrait |
Method Details
Returns an instance of {@see ActiveQueryInterface} instantiated by {@see ActiveRecordInterface::query()} method.
If the $condition parameter is not null, it calls {@see \Yiisoft\ActiveRecord\ActiveQueryInterface::andWhere()} method.
Do not to pass user input to this method, use {@see \Yiisoft\ActiveRecord\Trait\findByPk()} instead.
| public static Yiisoft\ActiveRecord\ActiveQueryInterface find ( array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $condition = null, array $params = [] ) | ||
| $condition | array|\Yiisoft\Db\Expression\ExpressionInterface|string|null |
The condition to be applied to the query where clause.
No condition is applied if |
| $params | array |
The parameters to be bound to the SQL statement during execution. |
public static function find(array|string|ExpressionInterface|null $condition = null, array $params = []): ActiveQueryInterface
{
$query = static::query();
if ($condition === null) {
return $query;
}
return $query->andWhere($condition, $params);
}
Shortcut for {@see find()} method with calling {@see ActiveQueryInterface::all()} method to get all records.
Do not to pass user input to this method, use {@see \Yiisoft\ActiveRecord\Trait\findByPk()} instead.
// find the customers whose primary key value is 10, 11 or 12.
$customers = Customer::findAll(['id' => [10, 11, 12]]);
// the above code line is equal to:
$customers = Customer::find(['id' => [10, 11, 12]])->all();
// find customers whose age is 30 and whose status is 1.
$customers = Customer::findAll(['age' => 30, 'status' => 1]);
// the above code line is equal to.
$customers = Customer::find(['age' => 30, 'status' => 1])->all();
[!WARNING] Do NOT use the following code! It is possible to inject any condition to filter by arbitrary column values!
$id = $request->getAttribute('id');
$posts = Post::findAll($id); // Do NOT use this!
Explicitly specifying the column to search:
$posts = Post::findAll(['id' => $id]);
// or use {@see \Yiisoft\ActiveRecord\Trait\findByPk()} method
$post = Post::findByPk($id);
| public static Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] findAll ( array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $condition = null, array $params = [] ) | ||
| $condition | array|\Yiisoft\Db\Expression\ExpressionInterface|string|null |
The condition to be applied to the query where clause.
Returns all records if |
| $params | array |
The parameters to be bound to the SQL statement during execution. |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] |
An array of ActiveRecord instance, or an empty array if nothing matches. |
|---|---|---|
public static function findAll(array|string|ExpressionInterface|null $condition = null, array $params = []): array
{
return static::find($condition, $params)->all();
}
Shortcut for {@see findAll()} method with throwing {@see NotFoundException} if no records found.
$customers = Customer::findAllOrFail(['is_active' => true]);
| public static Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] findAllOrFail ( array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $condition = null, array $params = [] ) | ||
| $condition | array|\Yiisoft\Db\Expression\ExpressionInterface|string|null |
The condition to be applied to the query where clause.
Returns all records if |
| $params | array |
The parameters to be bound to the SQL statement during execution. |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] |
An array of ActiveRecord instance, or throws {@see \Yiisoft\ActiveRecord\NotFoundException} if nothing matches. |
|---|---|---|
| throws | Yiisoft\ActiveRecord\NotFoundException | |
public static function findAllOrFail(array|string|ExpressionInterface|null $condition = null, array $params = []): array
{
return static::findAll($condition, $params) ?: throw new NotFoundException('No records found.');
}
Finds an ActiveRecord instance by the given primary key value.
In the examples below, the id column is the primary key of the table.
$customer = Customer::findByPk(1); // WHERE id = 1
$customer = Customer::findByPk([1]); // WHERE id = 1
In the examples below, the id and id2 columns are the composite primary key of the table.
$orderItem = OrderItem::findByPk([1, 2]); // WHERE id = 1 AND id2 = 2
If you need to pass user input to this method, make sure the input value is scalar or in case of array, make sure the array values are scalar:
public function actionView(ServerRequestInterface $request)
{
$id = (string) $request->getAttribute('id');
$customer = Customer::findByPk($id);
}
| public static Yiisoft\ActiveRecord\ActiveRecordInterface|array|null findByPk ( array|float|integer|string $values ) | ||
| $values | array|float|integer|string |
The primary key value(s) to find the record. |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null |
Instance matching the primary key value(s), or |
|---|---|---|
public static function findByPk(array|float|int|string $values): array|ActiveRecordInterface|null
{
return static::query()->findByPk($values);
}
Shortcut for {@see findByPk()} method with throwing {@see NotFoundException} if no records found.
$customer = Customer::findByPkOrFail(1);
| public static Yiisoft\ActiveRecord\ActiveRecordInterface|array|null findByPkOrFail ( array|float|integer|string $values ) | ||
| $values | array|float|integer|string |
The primary key value(s) to find the record. |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null |
Instance matching the primary key value(s), or throws {@see \Yiisoft\ActiveRecord\NotFoundException} if nothing matches. |
|---|---|---|
| throws | Yiisoft\ActiveRecord\NotFoundException | |
public static function findByPkOrFail(array|float|int|string $values): array|ActiveRecordInterface|null
{
return static::findByPk($values) ?? throw new NotFoundException('No records found.');
}
Creates an {@see ActiveQueryInterface} instance with a given SQL statement.
Note: That because the SQL statement is already specified, calling more query modification methods (such as {@see \Yiisoft\ActiveRecord\Trait\where()}, {@see order()) on the created {@see ActiveQueryInterface} instance will have no effect.
However, calling {@see with()}, {@see asArray()}, {@see indexBy()} or {@see resultCallback()} is still fine.
Below is an example:
$customers = Customer::findBySql('SELECT * FROM customer')->all();
| public static Yiisoft\ActiveRecord\ActiveQueryInterface findBySql ( string $sql, array $params = [] ) | ||
| $sql | string |
The SQL statement to be executed. |
| $params | array |
The parameters to be bound to the SQL statement during execution. |
| return | Yiisoft\ActiveRecord\ActiveQueryInterface |
The newly created {@see \Yiisoft\ActiveRecord\ActiveQueryInterface} instance. |
|---|---|---|
public static function findBySql(string $sql, array $params = []): ActiveQueryInterface
{
return static::query()->sql($sql)->params($params);
}
Shortcut for {@see find()} method with calling {@see ActiveQueryInterface::one()} method to get one record.
Do not to pass user input to this method, use {@see \Yiisoft\ActiveRecord\Trait\findByPk()} instead.
// find a single customer whose primary key value is 10
$customer = Customer::findOne(['id' => 10]);
// the above code line is equal to:
$customer = Customer::find(['id' => 10])->one();
// find the first customer whose age is 30 and whose status is 1
$customer = Customer::findOne(['age' => 30, 'status' => 1]);
// the above code line is equal to:
$customer = Customer::find(['age' => 30, 'status' => 1])->one();
[!WARNING] Do NOT use the following code! It is possible to inject any condition to filter by arbitrary column values!
$id = $request->getAttribute('id');
$post = Post::findOne($id); // Do NOT use this!
Explicitly specifying the column to search:
$post = Post::findOne(['id' => $id]);
// or use {@see \Yiisoft\ActiveRecord\Trait\findByPk()} method
$post = Post::findByPk($id);
| public static Yiisoft\ActiveRecord\ActiveRecordInterface|array|null findOne ( array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $condition = null, array $params = [] ) | ||
| $condition | array|\Yiisoft\Db\Expression\ExpressionInterface|string|null |
The condition to be applied to the query where clause.
Returns the first record if |
| $params | array |
The parameters to be bound to the SQL statement during execution. |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null |
Instance matching the condition, or |
|---|---|---|
public static function findOne(
array|string|ExpressionInterface|null $condition = null,
array $params = [],
): ActiveRecordInterface|array|null {
return static::find($condition, $params)->one();
}
Shortcut for {@see findOne()} method with throwing {@see NotFoundException} if no records found.
$customer = Customer::findOneOrFail(['id' => 1]);
| public static Yiisoft\ActiveRecord\ActiveRecordInterface|array|null findOneOrFail ( array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $condition = null, array $params = [] ) | ||
| $condition | array|\Yiisoft\Db\Expression\ExpressionInterface|string|null |
The condition to be applied to the query where clause.
Returns the first record if |
| $params | array |
The parameters to be bound to the SQL statement during execution. |
| return | Yiisoft\ActiveRecord\ActiveRecordInterface|array|null |
Instance matching the condition, or throws {@see \Yiisoft\ActiveRecord\NotFoundException} if nothing matches. |
|---|---|---|
| throws | Yiisoft\ActiveRecord\NotFoundException | |
public static function findOneOrFail(
array|string|ExpressionInterface|null $condition = null,
array $params = [],
): ActiveRecordInterface|array|null {
return static::findOne($condition, $params) ?? throw new NotFoundException('No records found.');
}
Signup or Login in order to comment.