Difference between table, Entity and behavior

Asked

Viewed 210 times

4

What’s the difference between table, Entity and behavior in the context of Model? 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 put this code snippet?

2 answers

2

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

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

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

In which class should I put this code snippet?

As per its own documentation says:

In Cakephp your application domain model is divided into 2 types of main objects. The first ones are repositories (repositories) or table Objects (table objects). These objects provide access to data collections. They allow you to save new records, modify/delete existing ones, define relationships, and perform mass operations. The second type of objects are the entities (entities). Entities represent individual records and allow the you define line/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']);
    }
}

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 to the model what the Component is to the Controller. Behavior is to complete the Model.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.