In order to select or create specific entity you must create repository class.
To create and automatically link a repository class to your Record
or RecordEntity
create class extending RecordSource
and define RECORD
constant pointing to your record class:
class UserRepository extends RecordSource
{
const RECORD = User::class;
}
You can access your repository right after running orm:schema
command:
protected function indexAction(UserRepository $users)
{
//Alternative approach
$users = $this->orm->source(User::class);
}
To create record entity call method create
of your repository:
$users = $users->create([
'name' => 'Antony'
]);
Note, that entity values will be populated using setFields
method, make sure that you have FILLABLE
and SECURED
constants property set.
Attention, create would only initiate blank model entity, you have to manually persist your changes to the database by calling
save()
.
Use methods findByPK
, findOne
and find
to load entities from database:
$user = $users->findOne(['name' => 'Antony']);
Use method findByPK
to select entity using id:
protected function indexAction(string $id, UsersRepository $users)
{
if (empty($user = $users->findByPK((int)$id))) {
throw new NotFoundException('No such user');
}
dump($user);
}
Method find
of your Repository will return entity specific RecordSelector
which can be used as QueryBuilder:
$users = $users->find()->where('name', 'Antony')->paginate(10)->fetchAll();
In order to simplify your domain layer, it is recommended to define custom find commands specific to your application into repository class:
public function findActive(): RecordSelector
{
return $this->find(['status' => 'active']);
}
You can also chain this methods inside your repository:
public function findAuthors(): RecordSelector
{
return $this->findActive()->where(['type' => 'author']);
}
In some cases you might want to set base query for all of your find method. You can do it in your repository constructor or use public method withSelector
:
$deletedUsers = $users->withSelector(
$users->find(['status' => 'deleted'])
);
All repositories are treated as immutable.
If you wish to access record repository and selector using static functions use Spiral\ORM\SourceTrait
.
class User extends Record
{
use SourceTrait;
const SCHEMA = [
'id' => 'primary',
'name' => 'string',
'email' => 'string'
];
const DEFAULTS = [];
const INDEXES = [];
}
//Repository
dump(User::source());
//Shortcut
dump(User::findOne());
Please note, this method will only work in global container scope (inside your application).