guagay_wk
guagay_wk

Reputation: 28050

Would Entity or Table be better for converting this cakephp controller action?

I am using cakephp 3.1

Suppose I have this simple controller action below.

public function apiGetAccessKey($username)
{
    $query = $this->Customers
            ->find()
            ->select('access_key')
            ->where(['username' => $username]);  
}

I want to transfer it into the Model since it involves data extraction. With cakephp 3.x, model is divided into Table and Entity. Which should I use? Table or Entity? How should the code look like?

Upvotes: 0

Views: 225

Answers (1)

floriank
floriank

Reputation: 25698

TL;DR

Entities represent data, table objects access and process data.

Like I already recommended you in the other question I'll do it again: Read the manual. You approach of "trial and error and then ask" is not very effective. Read the book, try the code examples. If something remains unclear finally ask about that.

This is taken directly from the book:

Table Objects

Table objects provide access to the collection of entities stored in a specific table. Each table in your application should have an associated Table class which is used to interact with a given table.

Entities

While Table Objects represent and provide access to a collection of objects, entities represent individual rows or domain objects in your application. Entities contain persistent properties and methods to manipulate and access the data they contain.

Upvotes: 1

Related Questions