Category Archives: CakePhp

Cakephp Get Base Url


Cakephp Get Base Url – You can get base url in cake php using the $this->webroot;. Here in this tutorial we are going to explain how you can get the base url in cakephp.


Cakephp Get Base Url Example

You can get base url using the following method –

Method 1

Using the $this->webroot; you can get the base url in cakephp.

Cakephp Get Base Url Example:

$baseUrl = $this->webroot; 

The above example will give you the base url.

Method 2

Using the $this->html->url(‘/’, true); you can also get the base url in cakephp.

Cakephp Get Base Url Example:

$baseUrl = $this->html->url('/', true);; 

The above example will also give you the base url. You can use any of the above.

More Examples

Let’s have look over more example and demo here.

Cakephp get base url in view

If you want base url in view you can use the following method described above-

Cakephp Get Base Url Example:

$baseUrl = $this->webroot;  

Cakephp get base url in Controller

If you want base url in controller you can use the following method –

Cakephp Base Url In Controller Example:

$baseUrl = Router::url('/', true); 

Cakephp Get Table name And Column Details


Cakephp Get Table name And Column Details: Here in this tutorial we are going to explain how to get table name and its column in cakephp.


Cakephp Get Table name And Column Details

Here is syntax to get table details in cakephp –

Get Table Name in Cakephp

Cakephp Get Table name And Column Details:

$tableName = $this->Model->table;

The above example will give you table name.

Get Table Columns in Cakephp

Cakephp Get Table name And Column Details:

$tableColumns = $this->Model->schema();
var_dump($tableColumns);

The above example will give you table columns.

Cakephp Right Join Query


Cakephp Right Join Query : Right join basically pulls all rows records from the right table and only matching rows from left table. We pass the type parameter as “RIGHT” for Right joins. Here in this tutorial we are going to explain the RIGHT JOIN with example in cakephp framework. We will use cakephp model syntax for RIGHT JOIN in Cakephp.


Cakephp Right Join Query

Suppose we have a model called User which is associated with user table. Here in this example we are going to RIGHT JOIN this table with profile table which has user_id as foreign key.

Cakephp Right Join Query Example with syntax


Cakephp Right Join Query Example

   $userProfile = $this->User->find('all',
                array('joins' => array(
                                       array('table' => 'profile',
                                             'alias' => 'p',
                                             'type' => 'RIGHT',
                                             'foreignKey' => false,
                                             'conditions'=> array('user.id = p.user_id')                                             
                                        )
                                 ),
                 
                ));

The above example will RIGHT JOIN the two tables. it will generate the mysql query something like this –

Query Generated : SELECT user.id, user.name, user.email, user.phone FROM user RIGHT JOIN p on (user.id = p.user_id)

Cakephp Left Join Query


Cakephp Left Join Query : Join is an important part for getting data collectively from two or more than two table. We pass the type parameter as “LEFT” for left joins. Here in this tutorial we are going to explain the LEFT JOIN with example in cakephp framework. We will use cakephp model syntax for LEFT JOIN in Cakephp.


Cakephp Left Join Query

Suppose we have a model called User which is associated with user table. Here in this example we are going to LEFT JOIN this table with profile table which has user_id as foreign key.

Cakephp Left Join Query Example with syntax


Cakephp Left Join Query Example

   $userProfile = $this->User->find('all',
                array('joins' => array(
                                       array('table' => 'profile',
                                             'alias' => 'p',
                                             'type' => 'LEFT',
                                             'foreignKey' => false,
                                             'conditions'=> array('user.id = p.user_id')                                             
                                        )
                                 ),
                 
                ));

The above example will LEFT JOIN the two tables. it will generate the mysql query something like this –

Query Generated : SELECT user.id, user.name, user.email, user.phone FROM user LEFT JOIN p on (user.id = p.user_id)

Cakephp get ip Address


Cakephp get ip Address : In cakephp 2.x $this->request->clientIp(); is used to get the ip Address and In cakephp 1.x RequestHandlerComponent::getClientIp(); is used to get the ip address. You can use the as per your cakephp version. In this tutorial we are going to explain how to get the ip address of current user in cakephp.


Cakephp get ip Address

You can use the below syntax to get the address in cakephp as per the version-

In Cakephp 1.x

Use the below syntax to get the ip address in cakephp 1.x –

Cakephp get ip Address

$userIpAddress = RequestHandlerComponent::getClientIp();

Which will give you the current visitor ip address in cakephp 1.x.

In Cakephp 2.x

RequestHandlerComponent::getClientIp(); is deprecated in cakephp 2.x so use the below syntax to get the ip address in cakephp 2.x –

Cakephp get ip Address

$userIpAddress = $this->request->clientIp();

Which will give you the current visitor ip address in cakephp 2.x.

Note : You can still use the server variable $_SERVER[‘REMOTE_ADDR’] to get ip address.

Cakephp get url parameters


Cakephp get url parameters : $this->request->pass; is used to get the url parameters in cakephp. Basically Request & Response Objects are responsible for the http request and response. Here request parameter is used to get the url parameters so we are going to explain the request method. Request is default request object which is used in cakephp. By default the request object is assigned to $this->request. The $this->request is available in –

  • Controllers
  • Cells
  • Views
  • Helpers

You can access $this->request anywhere in controllers, Cells, Views Or Helpers. You can also access this is components. In this tutorial we are going to explain how to get parameters in cakephp and how to use them.


Cakephp get url parameters

Here are some main tasks which $this->requestis used to perform-

  • GET/POST and FILES Arrays.
  • Provides access to request parameters as array and object both.

Here is an syntax to get the passed arguments in url in cakephp-

Cakephp get url parameters Syntax –

// Get the Passed arguments
$this->request->pass;
$this->request['pass'];
$this->request->params['pass'];

The above syntax will give you the arguments of url passed along with controller. Now let us understand with example which will make more understanding on the above syntax.

Example

Suppose we have parameters something like www.example.com/posts/view/112/comment/id/444 we can access the parameters as below –

Cakephp get url arguments Example

//www.example.com/posts/view/112/comment/id/444 

$params = $this->params['pass'];

print_r($params);
//will give you
Array
(
    [0] => 112
    [1] => comment
    [2] => id
    [3] => 444
)

In the above example if you will print the params it will give you array as above. You can also use $this->request->pass; or $this->request->params[‘pass’]; to get the parameters. $this->request->pass; Will give the results as object and $this->request->params[‘pass’]; will give as array.

Tip for Cakephp get url parameters : You can use the above syntax to get the parameters in Controllers, Views, Cells and Helper also.

Cakephp Join Multiple tables


Cakephp Join Multiple tables : Join is an important part for getting data collectively from two or more than two table. Every framework has its own rule for joining two or multiple tables. Syntax for join depends upon the framework you are using. Here in this tutorial we are going to explain the join with two and more than two table in cakephp framework. We will use cakephp model syntax to join the tables.


Cakephp Join Multiple tables

Suppose we have a model called User which is associated with user table. Here in this example we are going to join this table with profile table which has user_id as foreign key.


Cakephp Join Multiple tables


Cakephp join Two Tables Example-

Cakephp Join two tables

   $userProfile = $this->User->find('all',
                array('joins' => array(
                                       array('table' => 'profile',
                                             'alias' => 'p',
                                             'type' => 'left',
                                             'foreignKey' => false,
                                             'conditions'=> array('user.id = p.user_id')                                             
                                        )
                                 ),
                 
                ));

The above example will join the two tables. If we go and see the query generated by this join in sql it will look something like this –

Query Generated : SELECT user.id, user.name, user.email, user.phone FROM user LEFT JOIN p on (user.id = p.user_id)

Cakephp join Three Tables Example-

Here is simple example of join three tables in cakephp.

Cakephp Join three tables

  $joins[] =  array(
									'table' => 'post', 
									'alias' => 'post', 
									'type' => 'LEFT',
									'conditions' => array(
										'user.id = post.user_id'
									)
							);
			    $joins[] =  array(
									'table' => 'profile', 
									'alias' => 'profile', 
									'type' => 'LEFT',
									'conditions' => array(
										'user.id = profile.user_id'
									)
							);
										
				$userDetail = $this->User->find('all', array("fields" => array('user.*','post.*','profile.*')
                                                 "joins" => $joins
); 

The above example will join the three tables. The above example will select all fields of the tables which are joined. You can specify the fields which you want to select in fields. If we go and see the query generated by this join in sql it will look something like this –

Cakephp join select fields

You can select fields using the fields tag to specify the fields as in above example- array(“fields” => array(‘user.*’,’post.*’,’profile.*’)

Get Current Url in Cakephp


Get Current Url in Cakephp : $this->here is used to get the current url in cakephp. it will give you the absolute current url. $this->request->here is also used to get current url. Here we are going to explain the method to get current url in cakephp on controller, view or model file.


Get Current Url in Cakephp

Syntax to get current url is as –

Absolute Current Url

Get Absolute Current Url in cakephp

$currentUrl = $this->here;
echo $currentUrl;

Will give you absolute current url such as “/users/profile/view”

Full Current Url

Get Full Current Url in cakephp

$currentUrl = Router::url( $this->here, true );
echo $currentUrl;

Will give you full current url such as “www.example.com/users/profile/view”

You can also use the below syntax to get current url –

Absolute Current Url in cakephp

$currentUrl = $this->request->here;
echo $currentUrl;

Will give you absolute current url such as “/users/profile/view”

Print last executed query in Cakephp 2.0


Print last executed query in Cakephp 2.0 : For each developer it becomes very important to print the last executed query in all frameworks. It helps much in development for debugging the queries. Here we are going to explain the method how you can print the last executed query in cakephp 2.0.


Print last executed query in Cakephp 2.0

You can print last executed query in cakephp as below-

Example:

  $dbo = $this->getDatasource();
  // or $dbo = $this->Model->getDatasource();
  $log = $dbo->getLog();
  $last_log = end($log['log']);
  return $last_log['query'];

The above example will print the last executed query in cakephp2.0