justrohu
justrohu

Reputation: 589

CakePHP 3 Entity Confusion

Here is the issue I am facing all the time since I started to learn CakePHP 3

What is this concept of entity a real world example would help alot.

public function add()
{
            // why do we have to create new entity / what is the role of entity here. 
    $comment = $this->Comments->newEntity();

    if ($this->request->is('post','put')) {

                    // why do we have to use this line after posting / what is the role of this line. 

        $comment = $this->Comments->patchEntity($comment,$this->request->data);

        if ($this->Comments->save($comment)) {
            $this->Flash->success('comment submitted successfully.');
        } else {
            $this->Flash->error('Sorry, comment could not be updated.');
        }
    }

    return $this->redirect($this->referer());
}

Upvotes: 3

Views: 5102

Answers (3)

Bruny
Bruny

Reputation: 39

Try this:

if ($this->request->is('post','put')) {
 $data = $this->request->getData();
 $comment = $this->Comments->newEntity();
 $comment = $this->Comments->patchEntity($comment, $data);
 $status = $this->Comments->save($comment);
   if ($status) {
     $this->Flash->success('comment submitted successfully.');
   } else {
     $this->Flash->error('Sorry, comment could not be updated.');
   }
}

return $this->redirect($this->referer());

}

My advice is never use Post and Put in the same function. Just for good pratice. Put works fine when you make a update using id like a parameter.

Upvotes: 0

Haresh Vidja
Haresh Vidja

Reputation: 8496

In simple word, Entity is a set of one record of table and their relational table, on that you can perform operation without touch of database and encapsulate property of entity (fields of table) as you want.

Advantages of Entity.

  • Modifying result sets outside of the database (for formatting or otherwise)
  • Needing to represent both the table and row in the same class.
  • Data validation was a fucking nightmare.
  • Inconsistent API in terms of both how we handled things internally as well as what (and how) we returned stuff.
  • Other random stuff as you want.

  • You can do run-time modification of result sets. Just add a method to your entity to return results in the way you want. This also means you can use composition for managing entities (yaya traits)

  • Validation is beautiful. We can validate data before it gets into an object and then validate the object state in a separate step.
  • It is easier for developers to understand what they are dealing with. You either have an object or an array of objects. An object can be linked to data which can also include other objects, but you no longer have to guess at what the array key will be, nor whether its nested funkily.
  • We can iterate on the interface for tables and entities separately. We couldn't easily change internals for the old Model class because of the implications on both, whereas now we can (in theory) change one without mucking about in the other.
  • It looks prettier simple.

Upvotes: 1

floriank
floriank

Reputation: 25698

Let me open the book for you:

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.

-

why do we have to create new entity / what is the role of entity here.

Almost everything, if not all, in Cake3 works with entities, what an entity is is explained above. You need to create a new entity so that the FormHelper can work with it, AFAIR it can still work with an array if configured to do so as well but the entity should be used.

The reason entities exist is to abstract the data. Some people think entities are the representation of a DB row - that's wrong. As the book says, they can be a row but don't have to represent a row because the 3.0 ORM can work with other resources as well. In theory you can have a CSV data source that returns an entity per line.

I suggest you to read the entity code in the CakePHP core to get a deeper understanding of what else entities provide, just saying they're "just" a set of properties is to short thought.

why do we have to use this line after posting / what is the role of this line.

The post data is merged into the previously created entity, that's it. Use the API if you have basic questions like that. See the API entry for patchEntity().

Upvotes: 6

Related Questions