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
- [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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Working with models2m 45s
-
Create a hasOne association5m 57s
-
Use belongsTo, hasMany, and belongsToMany associations4m 54s
-
Find your data4m 1s
-
Retrieve associated data4m 19s
-
Save your data4m 48s
-
Delete your data3m 47s
-
Standard CakePHP validation8m 3s
-
Create custom validation6m 53s
-
Entity methods5m 59s
-
Entity setters and getters4m 52s
-
-
-
-
-
-
-
-
-
-
-