Difference between table, entity and behavior

4

What is the difference between table , entity and behavior in the Model context? For example, I have a table called module, I want to make a query like this

$modulo = TableRegistry::get('Modulo');
    $resultado = $modulo ->find()
        ->select(['Modulo.Id_Modulo', 'Modulo.Nome_Modulo'])
        ->where(['Modulo.Id_Modulo => $idModulo])
        ->contain(['Acao']);

In which class should I place this code snippet?

    
asked by anonymous 05.09.2017 / 16:41

2 answers

2
  

What is the difference between table, entity, and behavior in the Model context?

While Table is responsible for accessing and representing a collection of objects, a Entity represents a singular object in this collection.

The behavior , as its literal translation says, is behavior that can be extended to its Model , assigning it behaviors common to other models. It's similar to Traits .

  

In which class should I place this code snippet?

According to documentation says:

  

In CakePHP your application domain model is divided into 2 types of   main objects. The first are repositories (repositories) or   table objects. These objects provide access to   collections of data. They allow you to save new records,   modify / delete existing ones, define relationships, and   perform bulk operations. The second type of objects are entities   (entities). Entities represent individual records and allow the   you define row / record level behavior and   functionalities.

Exemplifying:

namespace App\Model\Modulo;

use Cake\ORM\TableRegistry;

class ModuloTable extends Table
{
    public getAllModuloAcao($idModulo)
    {
        $modulo = TableRegistry::get('Modulo');
        return $modulo ->find()
        ->select(['Modulo.Id_Modulo', 'Modulo.Nome_Modulo'])
        ->where(['Modulo.Id_Modulo' => $idModulo])
        ->contain(['Acao']);
    }
}
    
05.09.2017 / 19:30
0

Trying to simplify:

Table represents a table.

Entity represents only one record. Password handling, which handles a user record is done in the Entity class.

Behavior is for the model what the Component is for the Controller. The Behavior is completing the Model.

    
25.04.2018 / 17:35