DAOs

DAOs are classes where you can create, read, update, and delete data in your database according to a model.

note

📣  All commands have to be run in the api service (make api).

Generate#

console
php bin/console tdbm:generate

This command will regenerate the TDBM models and DAOs.

Like models, TDBM generates two kinds of classes. For instance:

  • BaseUserDao.
  • UserDao that extends BaseDao.

The first class contains most of the methods allowing you to interact with the corresponding table in the database.

You cannot modify it, but instead, edit the second class as TDBM does not override it.

Save / Update#

The base DAO provides a method save which takes the model instance as parameter:

$foo = new Foo($bar);
$this->fooDao->save($foo);
$foo->setBaz($baz);
$this->fooDao->save($foo);
note

📣  TDBM generates the primary key.

Delete#

The base DAO provides a method delete which takes the model instance as parameter:

$this->foodDao->delete($foo);
note

📣  This method also takes a boolean argument for cascade deletion. However, it would be best to handle such scenarios with Use Cases composition.

Get By#

The base DAO provides methods for retrieving a model from the primary key and unique properties:

$foo = $this->fooDao->getById($id);

Find All#

The base DAO provides the method findAll:

$foos = $this->fooDao->findAll();
note

📣  This method returns a ResultIterator, a sort of array of instances. See the Lists guide for more details.

Find#

The base DAO provides the method find:

$foos = $this->fooDao->find(
[
// "WHERE".
'bar LIKE :bar OR baz = :baz',
],
[
// Arguments of "WHERE".
'bar' => '%' . $bar . '%',
'baz' => $baz,
],
// Sort column and direction.
'id ASC'
);
note

📣  If a parameter's value is null, TDBM automatically removes the corresponding conditions in the first argument.

note

📣  This method returns a ResultIterator, a sort of array of instances. See the Lists chapter for more details.