rss

CakePHP Migrations Plugin

1

Category : Application Design, PHP

I regularly read through http://cakedc.com/, they have lots of CakePHP plugin that i can use in my CakePHP development. I was facing some problems with cakePHP migrations, i am not saying that CakePHP does not support that but its not come by default with the framework, you have to put some efforts to get it done.
I came across this CakePHP migration plugin that seems to be very useful in keeping my DB migration manageable.

The Migrations plugin provides a comprehensive management system whereby the database schema for a CakePHP project can fluctuate during development involving any number of developers. This is achieved by incrementally managing database changes, providing customisable hooks and callbacks for data migration and changes to meet migration paths, and migration maps to provide flexible application of migration instances.

Migrations is great for any development team, or individual that wants to manage database schema and data changes throughout the development lifetime of a project.

Requirements

CakePHP 1.3 or higher.

If you need a version to use with CakePHP 1.2 please contact us directly.

You can download the plugin from CakeDC site http://cakedc.com/eng/downloads/view/cakephp_migrations_plugin

Share

CakePHP Paginate Count with group by

14

Category : General, PHP

I am working on a CakePHP project these days, today i ran into a different problem with CakePHP. Sometimes i feel that there are small-small things that are sometimes missed by the Cake developers, but thank God that there are some workarounds available to them.  Here is the problem statement that i encountered, i had a table in which i was having duplicate records and i wanted to show only distinct records in the data grid which was using the CakePHP paginator class to create a paginated data grid.

Now to show only the distinct rows i used the group by clause to eliminate the duplicate records, the records were coming fine but when i saw the paging it was not working right at all. The problem was that the paginator’s “paginateCount” function was also considering the “Group By” clause while doing the counting of the total records. This was crazy.

I searched through Google to get a solution to that and found this link, where people have already reported a his error to the cake developers. The solution that worked for me was that i needed to override the “paginateCount” function and use a different function in my model to get the correct number of total records. I added this function to my model and whola it worked for me. Here is the function that needs to be added model to get things right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
		$parameters = compact('conditions', 'recursive');
 
		if (isset($extra['group'])) {
			$parameters['fields'] = $extra['group'];
 
			if (is_string($parameters['fields'])) {
				// pagination with single GROUP BY field
				if (substr($parameters['fields'], 0, 9) != 'DISTINCT ') {
					$parameters['fields'] = 'DISTINCT ' . $parameters['fields'];
				}
				unset($extra['group']);
				$count = $this->find('count', array_merge($parameters, $extra));
			} else {
				// resort to inefficient method for multiple GROUP BY fields
				$count = $this->find('count', array_merge($parameters, $extra));
				$count = $this->getAffectedRows();
			}
		} else {
			// regular pagination
			$count = $this->find('count', array_merge($parameters, $extra));
		}
		return $count;
	}
Share

CakePHP Sharing sessions between apps on the same domain

1

Category : General, PHP

Few days back i was working on two separate CakePHP applications, suddenly there was a need to maintain session between these two separate apps working on the same domain. Let me explain a bit more.

Cake-logo
Cake-logo
Image via Wikipedia

I was having two apps (i)wishlist (ii) lighthouse

These are two separate apps under the root folder having the directory structure as:
/htdocs
/wishlist
/app
/config
….
/lighthouse
/app
/config ….

By default the sessions are created relative to the apps directory, and this was the problem i was dealing with, not a big thing but i spent a lot of time figuring this, but for you its will work like a charm.

Steps that i follow to make the two apps share sessions between them.
1. Edit “core.php” for wishlist and the lighthouse and add the line
ini_set(‘session.cookie_path’, “/”);
This is to direct the CakePHP app to create the session on the root

2. The Session.cookie name should be same for the two apps

3. The Session.salt should be same for the two apps

4. Security.level should be low in both the apps

This is it what is required. Let me know if you are still stuck with the things, happy coding.

Share

CakePHP – Pagination Sorting on 2 Columns

Category : PHP

I was just wondering how one can sort on two columns at the same time through CakePHP framework. It was a need for one om project and i was using whole lot of other components and helpers along with pagination, so i found a way out for doing this.

Here is my solution to the problem.
The way to do it is inject the second sort column into the passed argument array before calling the $this->paginate function. So here is what i did.


$this->mergeSortParameters(array(“xyz.firstname”=> ($this->passedArgs['direction'])? $this->passedArgs['direction'] : ‘asc’));

Here is the function that needs to be called before calling the paginate method


  1. //Function to merge all the sorting parameters
  2. function mergeSortParameters($sort_extras){
  3. $sortParams = array();
  4. if( isset($this->passedArgs["sort"]) ) {
  5. // Use merge of sort request and extras
  6. $sortParams = am( $sort_extras, array($this->passedArgs["sort"] => $this->passedArgs["direction"]));
  7. } elseif( isset( $this->paginate['order'] ) ) {
  8. // Use default sort
  9. $sortParams = $this->paginate['order'];
  10. }
  11. $this->passedArgs = array( ‘order’ => $sortParams, ‘keywords’ => $this->passedArgs['keywords']);
  12. }
Share

Amit Yadav is Stephen Fry proof thanks to caching by WP Super Cache