model

CakePHP 3 Tutorial 21: Conditional Validation

Submitted by naidim on Fri, 01/20/2017 - 12:21

CakePHP's validation is very robust, and while this is immensely helpful, getting to grips with all its possibilities can be daunting.

One handy feature is Conditional Validation. When adding a record, what if we want to ensure a field is not empty only when a matching field is also not empty? For example a schedule for work should have a start time and an end time, but if you wanted to schedule the day off it would have neither.

CakePHP 3 Tutorial 19: Display One Thing, Save Another

Submitted by naidim on Tue, 10/25/2016 - 13:37

There may come a case, as in the previous tutorial (CakePHP 3 Tutorial 18: Autocomplete) that you want to display one thing in a form field, yet save a different value. In the previous case, you select a user's name from an autocomplete list, yet the user's id is the value you want to save in the database.

CakePHP 3 Tutorial 17: Adding Associated Records

Submitted by naidim on Tue, 10/25/2016 - 11:42

In the previous tutorial (CakePHP 3 Tutorial 16: Table Relationships) we set up associating tables and displaying associated data, but there's no point to displaying associated data if you can't add it first.

There are multiple ways to save associated data. We'll start with the easiest way first.

CakePHP 3 Tutorial 16: Table Relationships

Submitted by naidim on Fri, 10/21/2016 - 15:43

When working with CakePHP it is important to remember "Convention over Configuration." If you start from the bottom, or back end, the database, and work your way forward correctly, CakePHP will do much of legwork for you, saving tons of redundant and repetitive work. If you don't, there are almost always ways to make things work the hard way.

An example of this is the relationships between tables. If you name your database tables and fields following CakePHP convention, it becomes trivial to set up relationships between them.

CakePHP 3 Tutorial 15: Neighbors

Submitted by naidim on Tue, 10/18/2016 - 07:58

In CakePHP 2 there was a handy feature called "neighbors" where you could simply request the neighboring records (previous and next) for easy linking. This is not present in CakePHP 3, probably due to the limited use of such a feature, however, with custom finders you can replicate the functionality.

In your model (e.g. src/Models/Table/UsersTable.php) add the following custom finder: