-
Notifications
You must be signed in to change notification settings - Fork 25
Working with Repository
A repository consists of read connection / write connection and the related table information.
Each schema has its own repo class, which is generated by the schema generator. And this is for getting pre-built constants like SQL statements, table name constants, column names faster.
To create a Repository of Book schema, simply call masterRepo static method on Book class:
$repo = Book::masterRepo();The above code creates an instance of BookRepo with connections to master database.
To customize the repository connection, like separating read/write
connections, you can simply pass Maghead\Connection instances:
$repo = Book::repo($write, $read);If $read is omit, then the read connection will be same as $write:
$repo = Book::repo($write);Generally, if you don't need to customize the connections, calling
::masterRepo will be faster than ::repo since it doesn't check the
parameter types.
-
BaseRepo::create(array $args) : Maghead\Runtime\ResultTo create a record, simply call
::create.$ret = Book::masterRepo()->create([ 'title' => 'Programming PHP', ]);This method returns
Maghead\Runtime\Resultobject.
-
BaseRepo::findWith(array $conditions)Find the record base on the condition array.
-
BaseRepo::findByPrimaryKey($key)Find the record by the given primary key.
-
BaseRepo::findByKeys(array $conditions, array $keys)Find the record base on the condition array, the condition array will be filtered by the keys. This method calls
::findWithwith the filtered condition array. -
BaseRepo::load($condition)Find the record base on either primary key or a condition array.
Note: this method is slower than
findWithorfindByPrimaryKeyif you know what will be used to find the record. -
BaseRepo::loadForUpdate($condition)Find the record base on either primary key or a condition array.
This method executes SQL query with
SELECT ... FOR UPDATEsyntax to provide the row-level lock for the selected row.Note: this method is only supported for MySQL.
After you created an instance of Repository for one schema, you can create query objects from it to customize your query rather than the simple CRUD operation for just one record object.
Currently Maghead supports 3 basic query factory methods:
BaseRepo::select($sel) : SelectQueryBaseRepo::update($data) : UpdateQueryBaseRepo::delete() : DeleteQuery
To create a simple select query and then query the collection from the db:
$books = Book::masterRepo()->select('*')->fetch(); // Returns BookCollection object.
$cnt = count($books); // BookCollection implements Countable interface.
foreach ($books as $book) {
// do something with $book
}To create a select query with complex conditions:
$query = Book::masterRepo()->select('*');
$query->where()
->equal('member_id', $memberId)
->in('status', [ VERIFIED, VIP ])
;
$query->orderBy('created_at', 'DESC');
$query->limit(10);
$books = $query->fetch();To see more examples, please visit https://github.com/c9s/SQLBuilder for more details.
$query = Book::masterRepo()->update([
'title' => 'Updated'
])
$query->where()->between('created_at', $from, $to)
$query->execute();$q = Book::masterRepo()->delete();
$ret = $q->where()
->equal('title', 'Book 1')
->execute();DeleteQuery implements Maghead\Query\Executable and therefore you can call
execute method.
(coming soon)
(coming soon)