Model


The model layer is the one that manages the reading and recording of information in the database.

Temma offers 3 mechanisms for this:

  • Direct access to the database, using the \Temma\Datasources\Sql abstraction object.
  • Automatic creation of a DAO object by the framework, to easily access a table without writing any code.
  • Writing your own DAO objects, to make queries more efficient than an automatic tool would.

How to use these mechanisms is up to you. However, we suggest that you do this:

  1. Start by using the DAO automatically generated by Temma. It allows you to write your application code quickly. As long as you only need simple queries for a single table, this works fine. For example, you would write:
    // retrieve a user from his username
    $user = $this->_dao->get($userId);
    
    // retrieve all users whose 'valid' field is true
    $validUsers = $this->_dao->search(
        $this->_dao->criteria()->is('valid')
    );
    
  2. If you want to simplify the use of DAO, you can create your own DAO, which extends the default one. For example, you can create a method:
    $proUsers = $this->_dao->getLastProfessionalUsers(5);
    
    which will be easier to use and much more explicit than:
    $proUsers = $this>_dao->search(
        $this->_dao->criteria()
            ->equal('type', 'pro')
            ->is('valid'),
        ['date_creation' => 'desc'],
        null,
        5
    );
    
    These additional methods will only be overlays serving to simplify the use of the DAO, but the implementation will always remain as simple and fast to write.
  3. Subsequently, you can enrich your personalized DAO, to make it perform complex queries (with joins on several tables, sub-queries, groupings, etc.). You will then have to access the database directly and write your own SQL queries, but you will benefit from the simplification mechanism for writing queries offered by the framework.

However, if you know SQL well, we encourage you to write your DAOs directly by placing SQL queries in them. It will always be more effective in the medium term.