CakePHP 3 includes the ability to find and retrieve data from your database easily and quickly. In this video, you will learn how to use CakePHP's tools to retrieve just the data you want, through the use of the where method.
- [Voiceover] Probably the most common thing we'll want to do is to retrieve data from the database. We've already seen a bit of this in our controllers. We used pagination and a find command to retrieve data. Let's explore in more detail exactly how a find command works, and what we can find. The most basic way to find things is using the get method. Let's take a look at this. Open up our bookmarks controller, which is in source, controller, bookmarks controller.php On line 42 we have the view method for our bookmarks controller.
Which is the method to view a single bookmark record from our database. If we look on line 44, we have a get method being used to retrieve the bookmark data. What's special about a get find method? A get method takes two properties. The first is the primary key of the record. The second is an array of options for the find. In this case, we pass an additional array of other models to contain. The get retrieves only one matching record for a primary key.
And if the record does not exist in the database it throws a not found exception that winds up looking like a 404 http error to any users. Let's take a look at this now. First, remember to start up your CakePHP server, and open up your browser to local host port 8765, and the url bookmarks. If we click the view link on any of the bookmarks, we'll see the result of a view controller action, and our get find method in action.
If you click on the SQL log link in debug kit you'll see the SQL that CakePHP used to find our data. Great, let's change the url to attempt to find a bookmark that doesn't exist. If you change the number at the end of the url from being one to instead being the letter a, like so, you'll see that an error page is displayed. Telling us that this record could not be found in the table. That's the get method in action. What happens though if we want to do something more complex, or interesting? Well we still have the basic find method on our table classes to find data.
Let's explore that now. We actually already wrote a find command in our bookmarks controller, so let's go back to that. Go back to your code editor, and we want to go to our export method on line 30. In our export action on line 31, we wrote a find method to find all the bookmarks with a particular limit. Let's see what else we can do here. What about adding some conditions to find a bookmark that belongs to a specific user. We can do that. Let's modify line 31 to remove the semicolon from the end.
Add a new line for 32. Do arrow where. Where takes an array of key value pairs. The key being the field to apply the conditions on, and the value being the actual condition. So again I add a key value pair of string user underscore id is equal to one. This will limit all bookmarks returned to those that match the user id being equal to one. We can see this in action by going back to our browser, and going to the url bookmarks\export.
And here we see only two bookmarks because there's only two bookmarks in our database that belong to the user id one. If you look at the SQL log in debug kit, you'll see our condition being applied to this command. Where user id is equal to one. The find command and the query tools in CakePHP can do much more than this. You can even build your own custom find methods, and your table classes to easily reuse a common query. The CakePHP book has much more information and depth, of all that you can do with the CakePHP query tools.
CakePHP is a critical framework for PHP developers. It helps them build complex web applications faster and more efficiently. If you want to use CakePHP 3 (the latest version of the framework) in your own development workflow, this is the place to start.
Justin Yost provides an overview of the underlying MVC pattern in CakePHP, and the installation and configuration process for Mac and Windows. He shows how to use the CakePHP shell to build your first basic CakePHP app, and then discusses each application element in depth: controllers, models, views, components, behaviors, helpers, and utilities. At each step, he discusses the relevant new features and enhancements in CakePHP 3, including new components; performance, session management, and ORM improvements; and localization.
In later chapters, the course gets a little more advanced. Watch these tutorials to learn how to send email with CakePHP, extend CakePHP with plugins, and write unit tests to identify and eliminate bugs in your code. Justin also shows how to add security to your CakePHP apps with a basic user authentication system.
- Installing and configuring CakePHP
- Using the CakePHP shell console
- Creating CakePHP controllers
- Saving data in a CakePHP model
- Finding and deleting data
- Working with entity methods
- Creating CakePHP views
- Using and customizing components to share functionality between controllers
- Creating behaviors
- Formatting data with helpers
- Developing faster with CakePHP utilities: hash, collections, and logging
- Sending CakePHP email
- Creating a custom plugin
- Testing CakePHP applications
- Authorizing users of CakePHP applications