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.
In the previous two tutorials we discussed adding associated records. But what if you want to select a record from an associated table where there are thousands of records? Autocomplete can help with this issue, but it requires a combination of parts to work.
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.
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:
You may have noticed references to password() and reset() functions, which are used to allow users to reset their password.
We can Add and Index (list) our users, but we still haven't set up the functions and views to allow editing and viewing our users.
Now that we have a user, complete with username and password, we can add authentication to restrict access to our application.
Now we'll build our Controller to retrieve the data and some Views to display it.