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

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

PHP: Fuzzy Address Matching Algo (Levenshtein)

Category : MySQL, PHP

Few days back i encountered a weird problem.
I had three tables in my DB viz: students, sites, company. Student table has the address fields and a column named “site_id” to connect it to the sites table. Sites table also has its address fields.

Now the challenge was to update the “site_id” field of the students table based on the matching address in the student and the site table. The address fields in both the tables were divided into following fields

  • address1
  • address2
  • city
  • state
  • country
  • zip

Now i had to think of a solution of how to do it. After googling a while i found a function called “levenshtein()” in PHP. This function calculated the distance between two given strings, bingo i got an idea and implemented the sddress matching algorithm. I have attached the file with this post.
Kindly let me if that was helpful to you.

Download the code from here -> fuzzy-match

  • Share/Bookmark

Facebook: HipHop for PHP

Category : PHP, Technology

Facebook officially announced the project, titled HipHop, this morning and confirmed that it would be released as open source this evening.

Facebook has been working on a PHP compiler that will increase speed by around 80% and offer a just-in-time (JIT) compilation engine that will offer a number of advantages.

Less CPU means fewer servers, which means less overhead. This project has had a tremendous impact on Facebook

HipHop for PHP isn’t technically a compiler itself, it is a source code transformer. HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it.

HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance.

HipHop includes a code transformer, a reimplementation of PHP’s runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations.

You can read more about HipHop here

  • Share/Bookmark

Nice to visit Websites

Category : Design, General

General Resources

PHP Resources

 

Reblog this post [with Zemanta]
  • Share/Bookmark

Basic CMS for converting static sites to dynamic!

2

Category : AJAX, PHP

Here is a solution that can convert a static sites to dynamic site on the fly with very little inputs.

Here is a content management system that will manage the content of your sites. This is one of the project that i worked on, its still in very basic version but i am still working on it. This CMS has following features:

- Create pages and one level sub pages for your site.
- Enable/Disable pages that you do not want other to see while they are still under construction.
- Drag and drop one sub page to other parent without any hassle.
-TinyMCE is integrated as the WYSIWYG editor through which you can easily manage your sites pages.

The file attached contains all the needed code and the sql file.

If anyone need my help in getting things work, please let me know.

So guys lets get the best out of the things.

Download

  • Share/Bookmark

Improved Error Messages in PHP 5

1

Category : PHP

Sometimes its the little things that make a difference. If you run the this test program in PHP 4 (tested on 4.4.7):

< ?php
function test($arg) { echo "talk like a pirate."; }
test();
?>

You get the following message:Warning: Missing argument 1 for test() in /usr/bin/- on line 2

The error message here is reported at the position of the definition of the function, but really the error was in how the function was called. The required parameter to test was not passed. This error can be annoying, forcing you to consult a stack trace to find the actual error location. Something some beginners may not know how to do.

However, if you run the same message in PHP 5 (tested on 5.2.2):

Warning: Missing argument 1 for test(), called in /Users/jeff/- on line 3 and defined in /Users/jeff/- on line 2

Sweet improvement!

One more reason to ditch PHP 4 and go php 5.

  • Share/Bookmark

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