By default, belongsTo and hasOne using LEFT JOIN relation between parent and child table or model. We can forcing the relationship to use either INNER JOIN or LEFT JOIN based on our need. Cakephp has add new option when binding model, name type: value is LEFT or INNER.
For example we have parent table Author and hasOne Post, we can set INNER JOIN relation by default on model class or in controller using bindModel. In this example the hasOne relation set on model class
<?php
class Author extends AppModel {
var $name = ‘Author’;
var $hasOne = array(’Post’=>array(’type’=>’INNER’));
}
?>
and running on controller
$this->Author->find(‘all’);
cakephp will generate query for author table.
SELECT `Author`.`id`, `Author`.`name`, `Post`.`id`, `Post`.`post_id`, `Post`.`title` FROM `authors` AS `Author` INNER JOIN `posts` AS `Post` ON (`Post`.`post_id` = `Author`.`id`) WHERE 1 = 1
The cakephp concept is MVC, Model-View-Controller. This three part will divide the area for database, business logic and design part. MVC give a good system to make a clean code for designer, database analyst and programmer. Design work will not interfere the business logic work, cakephp try to limit this thing.
A layout or html output is construct inside main layout and view for each actions. Main layout is a basic layout for your website. We can have some layout for different output type such as html, xml, rss, or etc. For each type we can have different layout as needed. For example registration page will have different layout with homepage.
By default cakephp use all layouts in folder cake\libs\view\layouts if you are not define your layout. You can override cakephp default layout by create ctp file in folder app/views/layouts. The default layout named as default.ctp. How to change this cakephp layout for certain controller action?
Simple create new ctp file inside app/views/layouts, for example mylayout.ctp. And then inside action function on controller put this code
$this->layout = ‘mylayout’;
or you can set folder path for the layout, for example we have layout file saved on app/views/layouts/xml/mylayout.ctp. you can set the path using this code
$this->layoutPath = ‘xml’;
so the complete code for changing the layout is
$this->layoutPath = ‘xml’;
$this->layout = ‘mylayout’;
It is easy to add RSS into your website using cakephp. Cakephp has a built in library to code or create a RSS output to your visitor. For you that doesn’t know yet RSS, it is a web 3.0 capability to communicate between two website about new update on one website. Its stand for Really Simple Syndication. The purpose is to give a standard output about new change on your website. For example a blog can have RSS to publish their latest post or article.
i will explain a method to add RSS capability into your cakephp website. In this example we have posts controller for our blog post and we want to create RSS for post table. The rss url will look like this
http://www.yoursite.com/posts/rss or change routing for http://www.yoursite.com/rss into posts controller and rss action.
(more…)
This article is telling you a code to implement BETWEEN Sql sintak for sql conditions when using cakephp framework. For example we have a table name ‘posts’ and field ‘id’, and then we want to query like this
SELECT * FROM posts WHERE id BETWEEN 1 AND 10
The syntax for above query can be constructed using this cakephp syntax
$this->Post->find(’all’, array(’conditions’=>array(’Post.id BETWEEN ? AND ?’ => array(1, 10))));
I think this syntax is the easiest one to create SQL BETWEEN sintak using cakephp