Professional Documents
Culture Documents
0
Phil Wright
phil@origin3.net
current as of 4/19/2007
$this->redirect($this->referer());
====
here are some more tips, if you have built the associations for your models you can access other models like this
$this->CurrentModel->AssocModel->save();
and if you want to do a custom query instead of $this->Model->findBySql() you can also use
$this->Model->Query()
here is some code to for the AppController when using admin_ or for potentially any controller.
function beforeFilter() {
//checks for var User in Session and if page is an admin page and that User is
admin level
if(!$this->Session->check('User') && stristr($this->action, 'admin_') && $this-
>Session->read('User.user_level') !='admin')
$this->redirect('/users/login');
}
===
Never use ‘url’ as a querystring parameter with Cake. Cake uses this.
The folder /app/webroot/files is not passed to cake with mod_rewrite rules. Use this for files (such as java .jar
files, etc..)
Controller
View
echo $html->selectTag("Gallery/title",$gallery_options,0,array('class'=>'gallery_select'),null,false);
Set showEmpty to true for a blank option selected first or false to eliminate the blank option.
I have kept track of things I learned about cake over the past month. I also pooled from sources on the web including:
Please let me know if you have something to add. Especially Model tricks and tips.
Good Stuff
In the controller add vendor(‘test.class’); // make sure above the class definition
$my_object->display_message();
CakePHP Manual
15 Minute Blog Tutorial
CakePHP API Documentation
CakePHP Bakery
Bakery - Pagination
Bakery - FCKeditor Integration
CakePHP Naming Conventions
http://cakeforge.org/
Change the default extension from .thtml
Edit the file /cake/libs/view/view.php
Line: 154
$this->CurrentModel->AssocModel->save();
If you want to do a custom query instead of $this->Model->findBySql() you can also use:
$this->Model->query($sql)
Also, here is some code to for the AppController when using admin_ or for potentially any controller.
function beforeFilter() {
//checks for var User in Session and if page is an admin page and that User is admin level
$this->redirect('/users/login');
}
define('CAKE_ADMIN', 'admin');
This will then make all actions that are prefixed with "admin_" to be accessible via:
/admin/yourcontroller/youraction.
For example, if you created an action in your ‘posts’ controller called "admin_add," you would access this via:
www.example.com/admin/posts/add
From there you can simply password protect the admin folder to prohibit unwanted users from adding posts.
Using BAKE is really easy. Once you have your table(s) created for your database, do the following:
At a command prompt or shell change directories to the /cake/scripts/ folder and type: php bake.php
If you choose to bake interactively it'll walk you through the steps required to create your model, controller, and views.
Once everything has been baked go through all the generated code and make custom modifications (as needed).
Logging Errors
There is a build in loggin system with Cake. Insert into your code:
$this->log('Something broke');
i.e.
$this->redirect("controller/action"); == /app/webroot/controller/action
echo $html->css(‘myfile’); == /app/webroot/css/myfile.css
echo $javascript->link(‘myjs’); == /app/webroot/js/myjs.js
Sometimes the rendering of the debug table at the bottom of your site can break your layout during development. You can
easily style this table to be hidden by adding this to your views css (it normally has no style):
.cakeSqlLog { display: none; }
This will give you a ton of information and variables about your controller/view/model
$this->here == the current url and view file name (without the extension)
I also highly recommend checking out the $this->params variable (in your CONTROLLER)
pr($this->params);
Highlights:
var $uses = array("<MODEL NAME>"); // make sure the model you use is loaded
In the view:
App: CORE.PHP
If you want to define a global variable for the whole application you can put it in the /app/config/CORE.PHP file. It is
loaded with all pages.
** note: You could also put them in your app/app_controller.php (if created) I recommend including only those functions
that are you will use in ALL your controllers, in the app/app_controller.php file and save the rest in the vendor.
$components
Just like $helpers and $uses, this variable is used to load up components you will need:
MODEL: Creating a model for a table that doesn't actually exist in the
database
Cake will throw an error if you create a model for a table that doesn't exist. Adding this to the model fixes the problem:
generateList()
(Populate select boxes with data from associated tables)
findBySql()
SQL Query
findCount()
Returns number of rows matching given SQL condition
hasAny()
Returns true if a record that meets the given conditions exists.
getLastInsertID()
After you save a record you can get the last id
Controller & Model: Saving model data
When saving a Model, make sure to put the $this->data[‘ModelName’] in the save method
(i.e., $this->EmailSent->save($this->data[‘EmailSent’])
Also, remember that all data form one or more models is saved in $this->data. ie. $this->data[‘EmailSent’][id’] is there
as well as another included model $this->data['EmailReceived][‘id’];
$retval = $this->requestAction('/controllera/getUserList);
$this->set(userListResults', $reval);
In your controller, use the $this->params[‘url’] object. In this case it will return (upon Form Submit from your view)
$this->params =>
Array(
[url] => medium/add_media
[project] => 2
[media_type] => 3
)
Make sure to check to see if the variable has been set before using it. I use the code:
You could also use isset. ** Note that you must use “POST” as your form method and not “GET”.
function beforeRender() {
$this->set('colors',array('red','blue','green');
}
This would make $colors accessible in every view rendered by that controller. beforeRender() is called after the controller
logic and just before a view is rendered.
There's also beforeFilter() and afterFilter(), which are called before and after every controller action. For more information,
read up on callbacks in the models section of the manual.