From the course: Learning CakePHP 3

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Entity methods

Entity methods - CakePHP Tutorial

From the course: Learning CakePHP 3

Start my 1-month free trial

Entity methods

- [Voiceover] Entity methods are a way for us to encapsulate logic for the data manipulation access of a record, and to be able to use it at the View or Controller level. Let's work through an example. We have bookmarks, tags and users. A common question that we want to answer is how many tags a bookmark has associated with it? Since the Entity class has access to the associated data, we can answer this question in the entity. So let's try it. First, open up the Bookmarks Entity class, which is located at source, Model, Entity, Bookmark.php. Now, to do this, our entities are more or less just normal php classes, so we can create custom methods that we can call from the instance of the entity in our Controller and View, and use the responses. So let's add a method called numberOfTags that will return the number of tags tied to a bookmark. To start, we'll go to line 33, and add a few blank lines here. And on line 35, add the method numberOfTags. Remember to add your opening and closing…

Contents