Posts Tagged ‘framework’
When I first started using CakePHP I was stuck for about 3 to 4 days with associations. After scouring the web, reading probably about 100 articles I have realized they are quite confusing when you first start with out using cake. I am hoping this post assists other people who were in my position for the past 3 days.
Associations are used for those tasks where you need to look up a row or multiple rows in another table based on an id number of sorts. The first thing to do in CakePHP is to make sure you have all the conventions correct to the very last point. If you want to make it really easy on yourself also I would recommend following their database naming conventions also. Here is a mini checklist:
- Model file names, var $name, and class names are singular
- Controller class names and var $name are plural also the file name is the plural version of model name followed by _controller.php
- The folder name for a view is plural and the file name should match the “action” name or function name in the controller followed with the extension .thtml
- Every table in your database should be the plural form of the model name.
- The primary key of each table should be id
- The associated key should be the name of another table singularly followed by _id (eg. I have a table named event_catogires, then in events my associated column name should be called event_category_id)
As far I can think right now. I think that may be all the key ones. If you have all the above correct the rest should be a breeze. I will now show you how I got something like a category name to appear next to a title of an article. This example isn’t meant to be copied. I don’t give enough information for that, but I hope this leads you guys in the right direction.
The Model:
class Event extends AppModel { var $name = 'Event'; var $useTable = 'events'; var $belongsTo = array('GalleryCategory' , 'EventCategory'); }
We are saying Event $belongsTo or $hasOne of each of these models (‘GalleryCategory’ , ‘EventCategory’). When find is called in the controller it will look up the following models and bring up that single row we are looking for in each of these.
The Controller:
class EventsController extends AppController { var $name = 'Events'; function index() { $this->set('events', $this->Event->findAll()); $this->set('title', 'All Events'); } }
Now I won’t show the view because it doesn’t really matter. You could just enable scaffolding if you had wished. $this->set(‘events’, $this->Event->findAll()); set up a variable named events so that in the view we can retrieve an array with all database fields. This includes the rows that were found set up in the belongs to of the events model. You will need to make a model and controller for every related table.
On the reverse side. Let us say you wanted to show all events with a event_category. Then all you need to do is simply use the following line rather than belongsTo:
var $hasMany = 'Events';
There are other associations, but I feel that these 2 would be the most used. For example if you had something like tags which unlike categories have and belong to many pages you would use: var $hasAndBelongsToMany = ‘Events’;.
The most import thing to get this stuff to work is following the conventions. If you don’t everything falls to pieces somethings work, somethings don’t and you will have holes in your system.
If you have any questions feel free to post a comment. I am a beginner with cake myself, but I am hoping by the end of august I will be a pro haha.
if ($error): echo " Login denied. Please try again. "; endif; echo $form->create('Member',array('type' => 'post', 'action' => 'login', 'id' => 'MemberLogin')); echo $form->inputs(array('Member.email','Member.password','legend' => 'Member Login')); echo $form->end('Login'); ?>
6 lines of code (if even want to call it that). For a login form with email field, password field, submit button and form opening and closing tags. It also adds in the correct labels for each form field, css div labels and a legend div around all the inputs. If i wanted I could make an entire table go into the fields just by specifying a model name, and then adding an array to exclude the columns I don’t want in there.
This is what I was working on today. After a little bit of trouble to figure out what was going on I quickly realized everything in CakePHP is about 50x easier that I think it is going to be. It almost feels like I am cheating on normal PHP with its better looking older sister. Thus far I have not written any sql my self and I already have linked up 5 database tables together so that when I look up info on say an event, the pictures(which exist within a gallery) come up. Also it brings up who wrote event or article or whatever. I realize this is a lot like the ruby on rails model, but you know its awesome none-the-less. I can’t wait to get this CMS out of the way so I can start sprucing it up with some cool ajax things that cake has built in.
Right now I am working on using their sanatize classes to prevent SQL injection and their validation classes to help with user sign up! This is truely awesome!
I am currently using Cake PHP 1.2 RC2. Bottom line… It rocks!