Retrieving Entities

Once you have created your first entities and configured one of the adapter packages, you can begin retrieving entities from the database.

Entities that extends ActiveRecord class includes powerful Cycle-ORM Query Builder allowing you to fluently query the database table associated with the Entity.

Here is example with findAll() method, which will retrieve all of the records from the entity associated database table:

Example with private properties and public getter methods. The findAll() method returns all the records from the users table, and we can access the name property using the name() method.

<?php

use App\Entities\User;
 
$users = User::findAll();
 
foreach ($users as $user) {
    echo $user->name();
}

🛠️ Building Queries

The ActiveRecord findAll() method will return all of the results in the entity table. However, since each ActiveRecord Entity includes a query builder, you may add additional constraints using query() method and then invoke the fetchAll() method to retrieve the results:

<?php

use App\Entities\User;

$users = User::query()
    ->where('active', 1)
    ->orderBy('name')
    ->fetchAll();

In this example, we use the query() method to start building a query.

Since ActiveRecord Entities includes access to query builder, you should review all of the methods provided by Cycle query builder. You may use any of these methods when writing your queries.

For advanced usage of the query builder, this package provides a way to group your queries into separate Query classes by creating classes that extend the ActiveQuery class.

🗂️ Collections

When working with Active Record entities, methods like findAll() retrieve multiple records from the database. However, instead of returning a plain PHP array, an instance of a collection class is returned. The specific type of collection class depends on the adapter package being used, such as:

  • spiral/cycle-bridge,

  • yiisoft/yii-cycle

  • wayofdev/laravel-cycle-orm-adapter

Last updated