rss

CakePHP Paginate Count with group by

1

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/Bookmark

A new look for Gmail

Category : General

Today when i logged in to my Gmail account i saw that Gmail has a new look. The new look has an easy access to the contacts and tasks. There was an update to the contacts too, now one can sort by last name and add custom labels for phone numbers and other fields.

Gmail_update

Gmail new update

  • Share/Bookmark

Lots of improvements for Firefox 4

Category : General, Product Releases

Some of the cool new stuff coming in Firefox 4.
* New Add-Ons Builder based on Bespin
* HTML5 Video display
* Painting with Canvas
* Image manipulation with Canvas – pixel testing, face detection with opencivitas
* Green screen technologies in images and video by detecting pixel colours.
* HTML5 embedded inside SVG (yes!)
* SVG as an IMG
* SVG as a CSS background
* SVG filter/mask/clip
* SVG animations
* Inline SVG inside HTML5
* CSS3 (selectors, @font-face, 2D Transforms, Transitions, Shadow, Gradients ,calculations – calc(2em-10px) )
* APIs: Geolocation, Offline (IndexDB, localStorage, AppCache, FileAPI – binary content of a file input, file drag and drop, web workers, websockets)
* Websockets controller running the presentation from the mobile.
* WebGL

  • Share/Bookmark

Microsoft Rolls Out Office Web Apps

Category : General, Information

Microsoft rolled out Microsoft Office Web Apps on Skydrive to users in the U.S., U.K., Canada and Ireland June 7th

Microsoft_office_live
  • Share/Bookmark

CakePHP Sharing sessions between apps on the same domain

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/Bookmark

CakePHP: Paginate using multiple tables/associations

Category : General

I was stuck with a scenario while working on one of the CakePHP application where i have the following association:

City belongsTo => State
State belongsTo => country

In case of Ruby on Rail we can use ‘through’ to create a relationship between the City and Country, but in case of CakePHP we have two things 1) recursive 2) Contain, but in my case both were not working so i searched and found an alternate way of establishing relationship between City and Country so when i run a find on City i also get the Country data in the resultset. Here is the function that i used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
function unbindAndBind(){
		// unbind State model for entire request
		$this->City->unbindModel(array('belongsTo' =>array('State')), false);
 
		// unbind State->Country model for entire request
		$this->City->State->unbindModel(array('belongsTo' =>array('Country')), false);
 
		// rebind Site and Company models with custom condition for Company model forcing join
		$this->City->bindModel(array
					(
					   'belongsTo' => array(
					   'State' => array(),
					   'Country' => array(
					  			     'foreignKey' => false, 
								     'conditions' => array('State.country_id = Country.id')
								   )
					    )
					),
					false
		);
	}
  • Share/Bookmark

Retrieving nth Salary from DB

1

Category : General

This query lets you find the nth highest salaries employees.

Select * From @Table [T1] Where
(2 = (Select Count(Distinct [ESalary]) From @Table [T2] where [T1]।[ESalary] <= [T2].[ESalary]))

Replace 2 with 3,4 ,5,6 etc to get the 3,4 ,5,6 etc highest salaried employees

  • Share/Bookmark

iGoogle new celebrity showcase

Category : General

iGoogle announce a new iGoogle Showcase, which allows one to see and share the homepages of some of your favorite icons. These 30 preeminent people, including Dave Matthews, Rachael Ray and Katie Couric, are sharing their iGoogle pages with users in full.

The iGoogle Showcase allows one to either add a celebrity’s entire iGoogle page to his/her own, or browse through the collection and choose different gadgets and themes from several pages. For example, you might select Al Gore’s iGoogle page for his preferred theme and gadgets for keeping up on the latest media news, supporting the Alliance for Climate protection cause or browsing through photos from National Geographic.

  • Share/Bookmark

Great to see this:)

Category : General

I was just browsing and came cross this link

http://en.wikipedia.org/wiki/Yadav

This talks about the past of Yadav cast. I was thrilled to see that i belong to such a great past.

  • Share/Bookmark

Essential Mac Software

Category : General

  • Share/Bookmark

Amit Yadav is Digg proof thanks to caching by WP Super Cache