Get PDF CakePHP - Quick Reference Guide

Free download. Book file PDF easily for everyone and every device. You can download and read online CakePHP - Quick Reference Guide file PDF Book only if you are registered here. And also you can download or read online all Book PDF file that related with CakePHP - Quick Reference Guide book. Happy reading CakePHP - Quick Reference Guide Bookeveryone. Download file Free Book PDF CakePHP - Quick Reference Guide at Complete PDF Library. This Book have some digital formats such us :paperbook, ebook, kindle, epub, fb2 and another formats. Here is The CompletePDF Book Library. It's free to register here to get Book file PDF CakePHP - Quick Reference Guide Pocket Guide.

After setting those points it will run a scan through your source files to find all the localization calls. After every scan PoEdit will display a summary of what was found and what was removed from the source files. Save it and a. As you may have noticed before, there are two main types of localized strings: simple ones and those with plural forms. The first ones have simply two boxes: source and localized string. Tip: you may right-click a translation line and it will hint you with the source files and lines where that string is being used.

On the other hand, plural form strings include two boxes to show the two source strings, and tabs so you can configure the different final forms. Whenever you change your sources and need to update the translations, just hit Refresh and Poedit will rescan the code, removing non-existent entries, merging the ones that changed and adding new ones. It may also try to guess some translations, based on other ones you did. It is also useful if you have a translation team and someone tries to write something they are not sure about: just mark Fuzzy, and someone else will review later.

From that menu, you can also open parts of the UI that allow you to leave contextual information for translators if needed. It happens the first time it is read, and then, to update it, you might need to restart the server. Many custom i18n libraries from frameworks use something similar to t as well, to make translated code shorter.

However, that is the only function that sports a shortcut. It is just a field in the. You need to include there the specifications of those new functions, following a specific format :. After including those new rules in the. Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time.

This quote makes the concept sound much more complicated than it actually is. Dependency Injection is providing a component with its dependencies either through constructor injection, method calls or the setting of properties. It is that simple. Here we have a Database class that requires an adapter to speak to the database. We instantiate the adapter in the constructor and create a hard dependency.

This makes testing difficult and means the Database class is very tightly coupled to the adapter. Now we are giving the Database class its dependency rather than creating it itself. These are the complex problems that Dependency Injection solves. In terms of Dependency Injection, this means loosening our dependencies by controlling and instantiating them elsewhere in the system. For years, PHP frameworks have been achieving Inversion of Control, however, the question became, which part of control are we inverting, and where to?

For example, MVC frameworks would generally provide a super object or base controller that other controllers must extend to gain access to its dependencies. This is Inversion of Control, however, instead of loosening dependencies, this method simply moved them. Dependency Injection allows us to more elegantly solve this problem by only injecting the dependencies we need, when we need them, without the need for any hard coded dependencies at all. The Single Responsibility Principle is about actors and high-level architecture.

The largest benefit of this approach is that it enables improved code reusability. By designing our class to do just one thing, we can use or re-use it in any other program without changing it. Practically speaking, this means that we should write classes that implement and adhere to interfaces , then type-hint against those interfaces instead of specific classes. The largest benefit of this approach is that we can very easily extend our code with support for something new without having to modify existing code, meaning that we can reduce QA time, and the risk for negative impact to the application is substantially reduced.

We can deploy new code, faster, and with more confidence. The Liskov Substitution Principle is about subtyping and inheritance. For example, if we have a FileInterface interface which defines an embed method, and we have Audio and Video classes which both implement the embed method, then we can expect that the usage of the embed method will always do the thing that we intend. If we later create a PDF class or a Gist class which implement the FileInterface interface, we will already know and understand what the embed method will do.

The largest benefit of this approach is that we have the ability to build flexible and easily-configurable programs, because when we change one object of a type e. For example, a Car or Bus class would be interested in a steeringWheel method, but a Motorcycle or Tricycle class would not. Conversely, a Motorcycle or Tricycle class would be interested in a handlebars method, but a Car or Bus class would not.

There is no need to have all of these types of vehicles implement support for both steeringWheel as well as handlebars , so we should break-apart the source interface. The Dependency Inversion Principle is about removing hard-links between discrete classes so that new functionality can be leveraged by passing a different class. Do not depend on concretions.

We can easily refactor the above example to follow this principle. There are several benefits to the Database class now depending on an interface rather than a concretion. Consider that we are working in a team and the adapter is being worked on by a colleague. In our first example, we would have to wait for said colleague to finish the adapter before we could properly mock it for our unit tests. An even bigger benefit to this method is that our code is now much more scalable.

If a year down the line we decide that we want to migrate to a different type of database, we can write an adapter that implements the original interface and injects that instead, no more refactoring would be required as we can ensure that the adapter follows the contract set by the interface. The first thing you should understand about Dependency Injection Containers is that they are not the same thing as Dependency Injection.

A container is a convenience utility that helps us implement Dependency Injection, however, they can be and often are misused to implement an anti-pattern, Service Location. Injecting a DI container as a Service Locator in to your classes arguably creates a harder dependency on the container than the dependency you are replacing. It also makes your code much less transparent and ultimately harder to test.

Most modern frameworks have their own Dependency Injection Container that allows you to wire your dependencies together through configuration. What this means in practice is that you can write application code that is as clean and de- coupled as the framework it is built on. Many times your PHP code will use a database to persist information.

You have a few options to connect and interact with your database. The recommended option until PHP 5. Native drivers are great if you are only using one database in your application, but if, for example, you are using MySQL and a little bit of MSSQL, or you need to connect to an Oracle database, then you will not be able to use the same drivers. The mysql extension for PHP is incredibly old and has been superseded by two other extensions:.

Not only did development stop long ago on mysql , but it was deprecated as of PHP 5. To save digging into your php. Even if you are not using PHP 7. Not only is that a gross oversimplification, it misses out on the advantages that mysqli provides, such as parameter binding, which is also offered in PDO.

More importantly, PDO allows you to safely inject foreign input e. This is possible using PDO statements and bound parameters. This ID should be used to fetch a user record from a database. This is the wrong way to do this:. This is terrible code. You are inserting a raw query parameter into a SQL query. This will get you hacked in a heartbeat, using a practice called SQL Injection. This is correct code. It uses a bound parameter on a PDO statement. This escapes the foreign input ID before it is introduced to the database preventing potential SQL injection attacks.

You should also be aware that database connections use up resources and it was not unheard-of to have resources exhausted if connections were not implicitly closed, however this was more common in other languages. Using PDO you can implicitly close the connection by destroying the object by ensuring all remaining references to it are deleted, i. When developers first start to learn PHP, they often end up mixing their database interaction up with their presentation logic, using code that might look like this:.

While there are many other solutions to doing this - depending on if you prefer OOP or functional programming - there must be some element of separation. That is a good start. Create a simple. This is essentially the same as what most modern frameworks are doing, albeit a little more manual. You might not need to do all of that every time, but mixing together too much presentation logic and database interaction can be a real problem if you ever want to unit-test your application. PHPBridge has a great resource called Creating a Data Class which covers a very similar topic, and is great for developers just getting used to the concept of interacting with databases.

Many frameworks provide their own abstraction layer which may or may not sit on top of PDO. These will often emulate features for one database system that is missing from another by wrapping your queries in PHP methods, giving you actual database abstraction instead of just the connection abstraction that PDO provides. This will of course add a little overhead, but if you are building a portable application that needs to work with MySQL, PostgreSQL and SQLite then a little overhead will be worth it the sake of code cleanliness.

Some abstraction layers have been built using the PSR-0 or PSR-4 namespace standards so can be installed in any application you like:. Templates provide a convenient way of separating your controller and domain logic from your presentation logic. The main benefit to using templates is the clear separation they create between the presentation logic and the rest of your application.

Templates have the sole responsibility of displaying formatted content. They are not responsible for data lookup, persistence or other more complex tasks.

Install CakePHP 3 using Composer

This leads to cleaner, more readable code which is especially helpful in a team environment where developers work on the server-side code controllers, models and designers work on the client-side code markup. Templates also improve the organization of presentation code. This approach encourages code reuse where larger blocks of code are broken into smaller, reusable pieces, often called partials. For example, your site header and footer can each be defined as templates, which are then included before and after each page template. Finally, depending on the library you use, templates can offer more security by automatically escaping user-generated content.

Some libraries even offer sand-boxing, where template designers are only given access to white-listed variables and functions. They are a natural choice since PHP is actually a template language itself. This is beneficial to PHP developers as there is no new syntax to learn, they know the functions available to them, and their code editors already have PHP syntax highlighting and auto-completion built-in. Further, plain PHP templates tend to be very fast as no compiling stage is required. Outside of frameworks, libraries like Plates or Aura. View make working with plain PHP templates easier by offering modern template functionality such as inheritance, layouts and extensions.

From automatic escaping, to inheritance and simplified control structures, compiled templates are designed to be easier to write, cleaner to read and safer to use. Compiled templates can even be shared across different languages, Mustache being a good example of this. Since these templates must be compiled there is a slight performance hit, however this is very minimal when proper caching is used. While it does have exceptions and more of the core is starting to use them when working with objects, most of PHP itself will try to keep processing regardless of what happens, unless a fatal error occurs.

This is only a notice error, and PHP will happily carry on. The only real difference is that Python will freak out over any small thing, so that developers can be super sure any potential issue or edge-case is caught, whereas PHP will keep on processing unless something extreme happens, at which point it will throw an error and report it.

PHP has several levels of error severity. The three most common types of messages are errors, notices and warnings. Notices are advisory messages caused by code that may or may not cause problems during the execution of the script, execution is not halted.

Warnings are non-fatal errors, execution of the script will not be halted. These messages are used to suggest changes to your code to help ensure best interoperability and forward compatibility with upcoming versions of PHP. You can also control whether or not errors are displayed to the screen good for development or hidden, and logged good for production. For more information on this check out the Error Reporting section.

This might seem like a good idea, but there are a few undesirable tradeoffs. PHP handles expressions using an in a less performant way than expressions without an. Secondly, the error control operator completely swallows the error. The error is not displayed, and the error is not sent to the error log. For example, our code above could be rewritten like this:. One instance where error suppression might make sense is where fopen fails to find a file to load. You could check for the existence of the file before you try to load it, but if the file is deleted after the check and before the fopen which might sound impossible, but it can happen then fopen will return false and throw an error.

This is potentially something PHP should resolve, but is one case where error suppression might seem like the only valid solution. However, Xdebug has an xdebug. You can set this via your php. Use scream with care, and as a temporary debugging tool. This is a common practice implemented by a large number of modern frameworks such as Symfony and Laravel.

In debug mode or dev mode both of these frameworks will display a nice and clean stack trace. There are also some packages available for better error and exception handling and reporting. Like Whoops! By throwing errors as exceptions in development you can handle them better than the usual result, and if you see an exception during development you can wrap it in a catch statement with specific instructions on how to handle the situation.

Each exception you catch instantly makes your application that little bit more robust. More information on this and details on how to use ErrorException with error handling can be found at ErrorException Class. Exceptions are a standard part of most popular programming languages, but they are often overlooked by PHP programmers.

Languages like Ruby are extremely Exception heavy, so whenever something goes wrong such as a HTTP request failing, or a DB query goes wrong, or even if an image asset could not be found, Ruby or the gems being used will throw an exception to the screen meaning you instantly know there is a mistake. The problem here is that you have to go looking for a mistake and check the docs to see what the error method is for this class, instead of having it made extremely obvious.

Another problem is when classes automatically throw an error to the screen and exit the process. When you do this you stop another developer from being able to dynamically handle that error. Exceptions should be thrown to make a developer aware of an error; they then can choose how to handle this. The generic Exception class provides very little debugging context for the developer; however, to remedy this, it is possible to create a specialized Exception type by sub-classing the generic Exception class:. This means you can add multiple catch blocks and handle different Exceptions differently.

This can lead to the creation of a lot of custom Exceptions, some of which could have been avoided using the SPL Exceptions provided in the SPL extension. It is very important for every PHP developer to learn the basics of web application security , which can be broken down into a handful of broad topics:. There are bad people ready and willing to exploit your web application.

This is a must read for the security-conscious developer. Eventually everyone builds a PHP application that relies on user login. Usernames and passwords are stored in a database and later used to authenticate users upon login. It is important that you properly hash passwords before storing them. Hashing and encrypting are two very different things that often get confused.

Hashing is an irreversible, one-way function. This produces a fixed-length string that cannot be feasibly reversed. This means you can compare a hash against another to determine if they both came from the same source string, but you cannot determine the original string. If passwords are not hashed and your database is accessed by an unauthorized third-party, all user accounts are now compromised. Unlike hashing, encryption is reversible provided you have the key.

Encryption is useful in other areas, but is a poor strategy for securely storing passwords. Passwords should also be individually salted by adding a random string to each password before hashing. Hashing and salting are vital as often users use the same password for multiple services and password quality can be poor. Additionally, you should use a specialized password hashing algoithm rather than fast, general-purpose cryptographic hash function e. The short list of acceptable password hashing algorithms as of June to use are:. In PHP 5. It will be updated in the future to support more algorithms as needed though.

Below we hash a string, and then check the hash against a new string. Never ever ever trust foreign input introduced to your PHP code. Always sanitize and validate foreign input before using it in code. Remember, foreign input is not limited to form data submitted by the user. Uploaded and downloaded files, session values, cookie data, and data from third-party web services are foreign input, too.

While foreign data can be stored, combined, and accessed later, it is still foreign input. Every time you process, output, concatenate, or include data in your code, ask yourself if the data is filtered properly and can it be trusted. Data may be filtered differently based on its purpose.

Another example is passing options to be executed on the command line. One last example is accepting foreign input to determine a file to load from the filesystem. The first of these is admin routing. If your application has a ProductsController as well as a NewsController, you might want to set up some special URLs so users with administrative privileges can access special actions in those controllers.

Remember this string, because you'll need to prepend it to your administrative actions in your controller. Using admin routes allows you to keep your logic organized while making the routing very easy to accomplish. Note Please note that enabling admin routes or using them does not enable any sort of authentica- tion or security. You'll need implement those yourself. Similarly, you can enable Cake's webservices routing to make easier there as well. Have a controller action you'd like to expose as a webservice? This enables some automatic routing somewhat similar to ad- min routing, except that a certain set of route prefixes are enabled:.

This allows you to create a single action that can have two views: one for normal HTML viewiers, and another for webservices users. By doing this, you can easily allow much of the functionality of your application to be available via webservices. For example, let's say I have some logic in my application that tells users who is on the phone in my office.

First I need to enable Cake's webservice routing:. Default setting is off. Optional Custom Inflections Configura- tion Cake's naming conventions can be really nice - you can name your model Box, your con- troller Boxes, and everything just works out. If Cake won't recognize your Foci or Fish, editing the custom inflections configuration file is where you'll need to go. Follow the notes inside the file to make adjustments, or use the examples in the file by un- commenting them.

You may need to know a little regex before diving in. Scaffolding 5. Cake's Scaffolding is Pretty Cool So cool that you'll want to use it in production apps. Now, we think its cool, too, but please realize that scaffolding is It's a bunch of stuff you throw up real quick during the beginning of a project in order to get started.

It isn't meant to be com- pletely flexible. So, if you find yourself really wanting to customize your logic and your views, its time to pull your scaffolding down in order to write some code.

Cakephp: A Rapid Development Framework

Scaffolding is a great way of getting the early parts of developing a web application started. Early database schemas are volatile and subject to change, which is perfectly normal in the early part of the design process. This has a downside: a web developer hates creating forms that never will see real use.

To reduce the strain on the developer, scaffolding has been included in Cake. Scaffolding analyzes your database tables and creates standard lists with add, delete and edit buttons, standard forms for editing and standard views for in- specting a single item in the database. With this release, it would be best to call this parentid. Also, when you have a foreign key in your table e. To continue our example of a category having a title:. Customizing Scaffold Views If you're looking for something a little different in your scaffolded views, you can create them yourself. We still don't recommend using this technique for production applications, but such a customization may be extremely useful for prototyping iterations.

If you find yourself wanting to change the controller logic at this point, it's time to take the scaffolding down from your application and start building it. One feature you might find helpful is Cake's code generator: Bake. Bake allows you to gen- erate a coded version of scaffolded code you can then move on to modify and customize as your application requires. Models 6. It is the M of the MVC pattern. What does it do? It separates domain logic from the presentation, isolating application lo- gic. A model is generally an access point to the database, and more specifically, to a certain ta- ble in the database.

By default, each model uses the table who's name is plural of its own, i. Models can also contain data validation rules, as- sociation information, and methods specific to the table it uses. Here's what a simple User model might look like in Cake:. It should contain methods that are shared between two or more models. Important While this section will treat most of the often-used functions in Cake's Model, it's important to.

Retrieving Your Data Below are a few of the standard ways of getting to your data using a model:. If your property has many owners who in turn have many contracts, a recursive findAll on your Property model will return those associated models. The re- cursive find can go up to three levels deep. If your property has many owners who in turn have many contracts, a recursive find on your Property model will return up to three levels deep of associated models. These magic functions can be used as a shortcut to search your tables for a row given a certain field, and a certain value.

Just tack on the name of the field you wish to search, and CamelCase it. Examples as used in a Controller might be:. This is useful in situations where you want 'Previous' and 'Next' links that walk users through some ordered sequence through your model entries. It only works for numeric and date based fields. This function is a shortcut to getting a list of key value pairs - especially handy for creating a html select tag from a list of your models. For example, if you wanted to generate a list of roles based on your Role model, keyed by their integer ids, the full call might look something like:.

To gain additional levels, use find or fin- dAll. Custom SQL calls can be made using the model's query and execute methods.

Share This Cheat Sheet!

The dif- ference between the two is that query is used to make custom SQL queries the results of which are returned , and execute is used to make custom SQL commands which require no return value. Complex Find Conditions using arrays Most of the model's finder calls involve passing sets of conditions in one way or another. Using arrays is clearer and easier to read, and also makes it very easy to build queries. This syntax also breaks out the elements of your query fields, values, operators, etc.

This allows Cake to gen- erate the most efficient query possible, ensure proper SQL syntax, and properly escape each individual part of the query. The structure is fairly self-explanatory: it will find any post where the title matches the string "This is a post". Note that we could have used just "title" as the field name, but when build- ing queries, it is good practice to always specify the model name, as it improves the clarity of the code, and helps prevent collisions in the future, should you choose to change your schema.

What about other types of matches? These are equally simple. Let's say we wanted to find all the posts where the title is not "This is a post":. The one exception here is IN Let's say you wanted to find posts where the title was in a given set of values:. By default, Cake joins multiple conditions with boolean AND; which means, the snippet above would only match posts that have been created in the past two weeks, and have a title that matches one in the given set.

However, we could just as easily find posts that match either condition:. These conditions are also infinitely nestable. Let's say you wanted to find all the posts that contained a certain keyword or were created in the past two weeks, but you want to restrict your search to posts written by Bob:. Saving Your Data To save data to your model, you need to supply it with the data you wish to save.

The data handed to the save method should be in the following form:. In order to get your data posted to the controller in this manner, it's easiest just to use the HTML Helper to do this, because it creates form elements that are named in the way Cake expects. You don't need to use it however: just make sure your form elements have names that look like data[Modelname][fieldname].

General Configuration

An edit function for a property controller might look something like the following:. Notice how the save operation is placed inside a conditional: when you try to save data to your model, Cake automatically attempts to validate your data using the rules you've provided. To learn more about data validation, see Chapter Model Callbacks We've added some model callbacks that allow you to sneak in logic before or after certain model operations. To gain this functionality in your applications, use the parameters provided and override these functions in your Cake models.

The beforeFind callback is executed just before a find operation begins. Place any pre- find logic in this method.

When you override this in your model, return true when you want the find to execute, and false when you want it to abort. Use this callback to modify results that have been returned from a find operation, or per- form any other post-find logic. The parameter for this function is the returned results from the model's find operation, and the return value is the modified results. Use this callback to modify model data before it is validated. It can also be used to add ad- ditional, more complex validation rules, using Model::invalidate.

This function must also return true, otherwise save ex- ecution will abort. Place any pre-save logic in this function. This function executes immediately after model data has been validated assuming it validates, otherwise the save call aborts, and this callback will not execute , but before the data is saved. This function should also return true if you want the save operation to continue, and false if you want to abort.

One usage of beforeSave might be to format time data for storage in a specifc database engine:. Place any pre-deletion logic in this function. This function should return trueif you want the deletion to continue, andfalse if you want to abort. Place any logic that you want to be executed after every deletion in this callback method. Model Variables When creating your models, there are a number of special variables you can set in order to gain access to Cake functionality:. If this model relates to a database table, and the table's primary key is not named 'id', use this variable to tell Cake the name of the primary key.

This sets the number of levels you wish Cake to fetch associated model data in find and findAll operations. Table 6. Tells Cake whether or not to enable transactions for this model i. Set to a boolean value. Only available for supporting databases. If the database table you wish to use isn't the plural form of the model name and you don't wish to change the table name , set this variable to the name of the table you'd like this model to use.

Use this variable to switch between them - just use the name of the database connection variable you've created in your database configuration file. The default is, you guessed it, 'default'. Associations 6. Introduction One of the most powerful features of CakePHP is the relational mapping provided by the model. In CakePHP, the links between tables are handled through associations.

Associ- ations are the glue between related logical units. When associations between models have been defined, Cake will automagically fetch models related to the model you are working with. If you use CakePHP's naming conventions, you can use scaffolding to visual- ize your application data, because scaffolding detects and uses the associations between models. Of course you can always customize model associations to work outside of Cake's naming conventions, but we'll save those tips for later.

For now, let's just stick to the con- ventions. The naming conventions that concern us here are the foreign keys, model names, and table names. Here's a review of what Cake expects for the names of these different elements: see Ap- pendix B for more information on naming. Since we'd like to store information about blog posts and their authors, the table names are "posts" and "authors", respectively. The model name for the "posts" table is "Post", and the model name for the "authors" table is "Author".

Note CakePHP's scaffolding expects your associations to be in the same order as your colums. Scaffolding would expect your associ- ations in the same order as the keys in the table e. In order to illustrate how some of these associations work, let's continue using the blog ap-. Imagine that we're going to create a simple user management system for the blog. I suppose it goes without saying we'll want to keep track of Users, but we'd also like each user to have an associated Profile User hasOne Profile.

Users will also be able to create comments and remain associated to them User hasMany Com- ments. Defining and Querying with hasOne In order to set up this association, we'll assume that you've already created the User and Profile models. To define the hasOne assocation between them, we'll need to add an array to the models to tell Cake how they relate. Here's how that looks like:. Each key in the array allows you to further configure the association:. We could use this to tell Cake to only associate a Profile that has a green header, if we wished. To define conditions like this, you'd specify a SQL conditions fragment as the value for this key: "Profile.

If you'd like your associated models in a specific order, set the value for this key using an SQL order predicate: "Profile. For example, if the "Cool Blue" profile is associated to "Bob", and I delete the user "Bob", the profile "Cool Blue" will also be deleted. This is here in case you're working with a database that doesn't follow Cake's naming conventions.

Now, when we execute find or findAll calls using the Profile model, we should see our associated User model there as well:. Defining and Querying with belongsTo Now that a User can see its Profile, we'll need to define an association so Profile can see its User. This is done in Cake using the belongsTo assocation. In the Profile model, we'd do the following:. We could use this to tell Cake to only associate a User that is active.

You would do this by setting the value of the key to be "User. If you'd like your associated models in a specific order, set the value for this key using an SQL order predicate: "User. Defining and Querying with hasMany Now that User and Profile models are associated and working properly, let's build our sys- tem so that User records are associated to Comment records. This is done in the User model like so:. We could use this to tell Cake to only associate a Comment that has been moderated. You would do this by setting the value of the key to be "Comment.

If you'd like your associated models in a specific order, set the value for this key using an SQL order predicate: "Comment. This is a good way to go for complex associations that depends on multiple tables. If Cake's automatic assocations aren't working for you, here's where you customize it. Now, when we execute find or findAll calls using the User model, we should see our as- sociated Comment models there as well:.

Note While we won't document the process here, it would be a great idea to define the "Comment belongsTo User" association as well, so that both models can see each other. Not defining assocations from both models is often a common gotcha when trying to use scaffolding. This last one is the hardest to wrap your head around, but it is also one of the most useful. The join table holds the individual rows that are related to each other. This is great for what we're about to do next: associate Post models to Tag models.

When a Tag belongs to a Post, we don't want it to be 'used up', we want to continue to associate it to other Posts as well. In order to do this, we'll need to set up the correct tables for this association. Of course you'll need a "tags" table for you Tag model, and a "posts" table for your posts, but you'll also need to create a join table for this association. HABTM join tables need to at least consist of the two foreign keys of the models they link.

We could use this to tell Cake to only associate a Tag that has been approved. You would do this by setting the value of the key to be "Tag. If you'd like your associated models in a specific order, set the value for this key using an SQL order predicate: "Tag. Basically, if the associations are distinct, set this to true.

That way the Tag "Awesome- ness" can only be assigned to the Post "Cake Model Associations" once, and will only show up once in result arrays. If you don't like the way Cake is performing deletes, or your setup is customized in some way, you can change the way deletion works by supplying your own query here. Now, when we execute find or findAll calls using the Post model, we should see our as- sociated Tag models there as well:.

Saving Related Model Data One important thing to remember when working with associated models is that saving model data should always be done by the corresponding Cake model. If you are saving a new Post and its associated Comments, then you would use both Post and Comment mod- els during the save operation. If neither of the associated models exists in the system yet for example, you want to save a new Post and a related Comment at the same time , you'll need to first save the primary, or parent model.

To get an idea of how this works, let's imagine that we have an action in our PostsController that handles the saving of a new Post and a related Comment. The ex- ample action shown below will assume that you've posted a single Post and a single Com- ment. If, however, the parent model already exists in the system for example, adding a Com- ment to an existing Post , you need to know the ID of the parent model before saving. If the ID was passed as a hidden element in the form, you might want to name the field if you're using the HtmlHelper so it ends up in the posted data where it needs to be:.

These same basic techniques will work if you're saving multiple child models, just place those save calls in a loop and remember to clear the model information using Mod- el::create. In summary, if you're saving associated data for belongsTo, hasOne, and hasMany rela- tions , the main point is getting the ID of the parent model and saving it to the child model.

Once that's done, you just call the save method on the model, and everything gets linked up correctly. With hasAndBelongsToMany, its a bit trickier, but we've gone out of our way to make it as simple as possible. In keeping along with our example, we'll need to make some sort of form that relates Tags to Posts.

Let's now create a form that creates posts, and associates.

You might actually like to create a form that creates new tags and associates them on the fly - but for simplicity's sake, we'll just show you how to associate them and let you take it from there. Let's just start out with the part of the form that cre- ates our post:. The form as it stands now will just create Post records. Let's add some code to allow us to bind a given Post to one or many Tags:. The submitted data must be a single ID, or an array of IDs of linked records.

Changing Associations on the Fly using bind- Model and unbindModel You might occasionally wish to change model association information for exceptional situ- ations when building your application. If your association settings in the model file are giv- ing you too much or not enough information, you can use two model functions to bind and unbind model associations for your next find. Let's set up a few models so we can see how bindModel and unbindModel work.

We'll start with two models:. Now, in a LeadersController, we can use the find method in the Leader Model to come up with a Leader and its associated followers. As you can see above, the association array in the Leader model defines a "Leader hasMany Followers" relationship. For demonstration purposes, let's use unbindModel to remove that association mid-controller.

The unbindModel function works similarly with other associations: just change the name of the association type and model classname. The basic usage for unbindModel is:. Now that we've successfully removed an association on the fly, let's add one. Our as-of-yet unprincipled Leader needs some associated Principles. Let's associate some Principles to our Leader on the fly but only for just the following find function call :.

The bindModel function can be handy for creating new assocations, but it can also be useful if you want to change the sorting or other parameters in a given association on the fly. There you have it. The basic usage for bindModel is to encapsulate a normal association array inside an array who's key is named after the type of assocation you are trying to cre- ate:.

Please note that your tables will need to be keyed correctly or association array properly configured to bind models on the fly. Controllers A controller is used to manage the logic for a certain section of your application. Most com- monly, controllers are used to manage the logic for a single model. For example, if you were building a site that manages a video collection, you might have a VideoController and a RentalController managing your videos and rentals, respectively. In Cake, controller names are always plural. Your application's controllers are classes that extend the Cake AppController class, which in turn extends a core Controller class.

Controllers can include any number of actions: functions used in your web application to display views. It itself extends the Controller class which is a standard Cake library. An action is a single functionality of a controller. It is run automatically by the Dispatcher if an incoming page request specifies it in routes configuration.

Returning to our video collec- tion example, our VideoController might contain the view , rent , and search actions. But how would these pages look? You would need to define a view for each of these ac- tions - check it out in the next chapter, but stay with me: the following sections will show you how to harness the power of the Cake controller and use it to your advantage.

Spe- cifically, you'll learn how to have your controller hand data to the view, redirect the user, and much more. This function is the main way to get data from your controller to your view. You can use it to hand over anything: single values,whole arrays, etc. Validates a model data according to the model's defined validation rules.

For more on val- idation, see Chapter You may not often need this function, because render is automatically called for you at the end of each controller action, and the view named after your action is rendered. Alternat- ively, you can call this function to render the view at any point in the controller logic. Tell your users where to go using this function. Important Cake's redirect and flash functions do not include an exit call.

If you wish your application to halt after a redirect or flash , you'll need to include your own exit call immediately after. You may also want to return rather than exit , depending on your situation for example, if you need some callbacks to execute. Controller Callbacks Cake controllers feature a number of callbacks you can use to insert logic before or after important controller functions.

To utilize this functionality, declare these functions in your controller using the parameters and return values detailed here. Called before every controller action. A useful place to check for active sessions and check roles. Other Useful Functions While these are functions part of Cake's Object class, they are also available inside the Controller:. This function calls a controller's action from any location and returns the rendered view. You can use requestAction to get data from another controller action, or get a fully rendered view from a controller.

First, getting data from a controller is simple. You just use request action in the view you need the data. Imagine that we needed to create a simple table showing the users in the system. Instead of duplicating code in another controller, we can get the data from UsersControl- ler::getUserList instead by using requestAction. If you have an oft used element in your application that is not static, you might want to use requestAction to inject it into your views.

Let's say that rather than just passing the data from UsersController::getUserList, we actually wanted to render that action's view which might consist of a table , inside another controller. This saves you from duplicating view code. Please note that actions called using requestAction are rendered using an empty layout - this way you don't have to worry about layouts getting rendered inside of layouts. You can use this function to log different events that happen within your web application. Controller Variables Manipulating a few special variables inside of your controller allows you to take advantage of some extra Cake functionality:.

PHP 4 doesn't like to give us the name of the current class in CamelCase. Use this vari- able to set the correct CamelCased name of your class if you're running into problems. Does your controller use more than one model? Use this variable to have your controller load helpers into its views.

The HTML helper is automatically loaded, but you can use this variable to specify a few others:. This functionality is really nice for access control - you can check to see a user's permissions before any action takes place. Just set this variable us- ing an array containing the controller action s you'd like to run:.

This variable is used to get data into the controller and provide access to information about the current request. Stores the name of the current controller handling the request. Stores the name of the current action handling the request. Stores the GET query string passed with the current request. Stores the current URL requested, along with key-value pairs of get variables.

Views 8. Views A view is a page template, usually named after an action. Although most of your view files will contain HTML, a view could be any perspective on a certain set of data, be it XML, and image, etc. In the view template file, you can use the data from the corresponding Model. Any data that you've handed to the view using set in the controller is also now available in your view. Note The HTML helper is available in every view by default, and is by far the most commonly used helper in views.

It is very helpful in creating forms, including scripts and media, linking and aiding in data validation. Most of the functions available in the views are provided by Helpers. Because views shouldn't contain much logic, there aren't many well used public functions in the view class. One that is helpful is renderElement , which will be discussed in section 1. Layouts A layout contains all the presentational code that wraps around a view. Anything you want to see in all of your views should be placed in your layout.

Once a new default layout has been created, controller view code is placed inside of the default layout when the page is rendered. Here's an example of what a default layout might look like:. For example, if a section of my site included a smaller ad banner space, I might create a new layout with the smaller advertising space and specify it as the layout for all controller's actions using something like:. Elements Many applications have small blocks of presentational code that needs to be repeated from page to page, sometimes in different places in the layout.

Cake can help you repeat parts of your website that need to be reused. These reusable parts are called Elements. Ads, help boxes, navigational controls, extra menus, and callouts are often implemented in Cake as elements. An Element is basically a mini-view that can be included in other Views.

The Element by default has no access to any data. To give it access to data, you send it in as a named parameter in an array. Inside the Element file, all the passed variables are available as the names of the keys of the passed array much like how set in the controller works with the views. Of course, it would be more useful to pass an array to the Element. Elements can be used to make a View more readable, placing the rendering of repeating elements in its own file. They can also help you re-use content fragments in your website.

Components 9. Presentation Components are used to aid controllers in specific situations. Rather than extend Cake's core libraries, special functionality can be made into components. The main goal in fact is: reusability. Com- ponents are to controllers what helpers are to views. The main difference is that compon- ents encapsulate business logic whereas helpers encapsulate presentation logic. This point actually is very important, a common confusing for new Bakers when trying to achieve reusability: I'm trying to do X, should this be a component or a helper?! Well, the answer is really simple, what does X do?

Does it do business logic or presentation logic, perhaps both? If it's business logic then it's a component. If it's presentation logic then it's a helper. If it's both, then.. An example of the later case would be an authentification system. You would want to login, logout, restrict access, and test permissions of a user to a ressource an action: edit, add, del..

But you also want to add some entries to the main menu when the user is logged in, and this is presentation logic. Let us assume you created foo. Inside of the file you need to define a class that cor- responds to the file name appending the word 'Component' to the file name. So in our case you would create the following contents:.

Now, to use your component, you need to add the following code in your controller's defini- tion:. A component gets access to the controller that loaded it through the startup method shown above. This method is called immediately after Controller::beforeFilter. You can also use other components inside your component. You simply have to declare in your component which components you want to use.

In the example below it is the session component. A component that becomes increasingly useful for the community may some day be included in the core distribution. Helpers Helpers Helpers are meant to provide functions that are commonly needed in views to format and present data in useful ways.

HTML The HTML helper has two main goals: to aid in the insertion of often-repeated sections of HTML code, and to aid in the quick-and-easy creation of web forms. Cake's core configuration contains a tags. The HTML helper uses the tag definitions in this file to generate tags you request. Using the HTML helper to create some of your view code can be helpful, as a change to the tags. There are functions in this helper that insert media, aid with tables, and there's even guiListTree which creates an unordered list based on a PHP array.

Creates a link to a CSS stylesheet. Renders an image tag. The code returned from this function can be used as an input for the link function to automatically create linked images. Use this function to create links in your view. For example, a link that deletes an object should probably have a "Are you sure? It generates all your form tags, automatically fills values back in during error situations, and spits out error messages. To help illustrate, let's walk through a quick example.

Imagine for a moment that your application has a Note model, and you want to create controller logic and a view to add and edit Note objects. In your NotesController, you would have an edit action that might look something like the following:. Our Note model is pretty simple at this point as it only contains an id, a submitter's id and a body. This view code is meant to display Note data. The HTML helper also includes a set of functions that aid in creating date-related option tags. Just provide the name of the field this date option tag is relevant to.

Once the data is processed, you'll see it in your controller with the part of the date it handles concat- enated to the end of the field name. You can then use this information to concatenate the time data in a format that is friendly to your current database configuration. Example Concatenating time data before saving a model excerpt from NotesController. In addition, any views that plan to use the Ajax Helper will need to in- clude those libraries.

This ar- ray is used to specify different things about your Ajax operation. Here are the different val- ues you can specify:. Replace the values in the Datasources. A sample completed configuration array might look something like the following:. Models are the heart of a CakePHP applications. They enable us to read and modify our data. They allow us to build relations between our data, validate data, and apply application rules.

Models build the foundations necessary to build our controller actions and templates. Table objects provide access to the collection of entities stored in a specific table. The completed file should look like this:. This also means that if you accidentally name your file wrong i. Entities represent a single record in the database, and provide row level behavior for our data.

With our model created, we need a controller for our articles. Actions are controller methods that have routes connected to them. For example, when a user requests www. This method should query the model layer, and prepare a response by rendering a Template in the View. The code for that action would look like this:. By defining function index in our ArticlesController , users can now access the logic there by requesting www. Similarly, if we were to define a function called foobar , users would be able to access that at www.

You may be tempted to name your controllers and actions in a way that allows you to obtain specific URLs. Resist that temptation. Our controller action is very simple. It fetches a paginated set of articles from the database, using the Articles Model that is automatically loaded via naming conventions. CakePHP will automatically render the template after our controller action completes. A layout is presentation code that is wrapped around a view. Layout files contain common site elements like headers, footers and navigation elements. Add the following code to your application:.

Variables passed into the view are available in the view templates as local variables which we used in the above code. CakePHP comes with a set of view helpers that make tasks like creating links, forms, and pagination buttons easy. These syntaxes allow you to leverage the reverse routing features CakePHP offers. You should see your list view, correctly formatted with the title and table listing of the articles.