Archive for the ‘CakePHP’ Category

CakePHP 1.2 Pagination Note

Friday, April 4th, 2008

I just noticed that in CakePHP 1.2 beta (6311), if you’re using the paginator helper, and attempting to sort data with the sort() function, AND you’re passing URL information, you need to specify the model explicitly in order to have the function take care of flipping the asc/desc sort direction.

Maybe this will make it more clear:

If I was just doing this:


< ?php
     echo $paginator->sort(’Name’,'name’,array(’url’=>Router::getParam(’pass’)));
?>

The generated sort link would always have the sort direction set to ‘asc’. Is this a bug? I’m not sure, probably. (Notice how I feed the Router’s pass params into the URL to preserve the current passed arguments to the controller function). To make this work, just specify the model explicitly:


< ?php
     echo $paginator->sort(’Name’,'name’,array(’url’=>Router::getParam(’pass’),’model’=>’Image’));
?>

And now the sort direction in the generated URL will flip between ‘asc’ and ‘desc’ depending on the current listing.

Also, if you want to show the user which way we’re currently sorting, you can including the current sort direction as a class on the link:


< ?php
     echo $paginator->sort(’Name’,'name’,array(’url’=>Router::getParam(’pass’),’model’=>’Image’,'class’=>$paginator->sortDir()));
?>

This will add the classes ‘asc’ or ‘desc’ to the link, and you can do some easy CSS styling to show directionality:


a.asc {
     padding-right:20px;
     background-image: url(../img/up-arrow.gif) top right no-repeat;
}

a.desc {
     padding-right:20px;
     background-image: url(../img/down-arrow.gif) top right no-repeat;
}


There’s lots more on pagination, make sure to check out the two Bakery articles: Basic Pagination Overview, and Advanced Pagination, both by Rob Conner.

A PHP Developer’s Keyboard

Monday, March 31st, 2008

Too many times writing <?php echo(’stuff’); ?>? Or just a dirty pinky finger? Why would my little finger be so much grittier than the others? Why is it red? Pounding out that damn PHP code too hard, probably.

Throw Down 404 Errors in Cake

Tuesday, February 26th, 2008

Say you’re doing some simple display logic for a controller view:


function view($id=null)
{
    if ($id) {
           $this->set($data,$this->Model->findById($id));
    } else {
           return false;
    }

}

In a situation like this, you always want to handle the (inevitable) situation when someone requests an invalid ID number, at which point no data would be returned in the $data variable.

Luckily, there’s a handy little function called cakeError, which lets you call a variety of Cake errors. Most of the time, you’ll want a 404 error, but it also lets you call any Cake-standard errors (you can see the full list here).

Regardless, if you want Cake to display a “404 Not Found” error message if the ID isn’t found or returns no data, do something like the following:


function view($id=null)
{
    if ($id) {
           $data = $this->Model->findById($id);
           if (!$data) {
                 $this->cakeError('error404',array(array('url'=>'/')));
           } else {
                 $this->set('data',$data);
           }

    } else {
           return false;
    }

}

That will throw up Cake’s generic 404 error page and stop further script execution for ya. If you want to have a custom 404 page, add a file named “error404.ctp” to your /app/views/error/ directory.

Beware Testing Group Models in Cakephp

Wednesday, December 19th, 2007

If you haven’t read the Bakery article on Testing Models in Cakephp, by Mariano Iglesias, you should go and read it now.

Having read that, there is something to watch out for. I have a authorization system that uses a model called Group which hasMany users. Ok. After making a series of tests in the vein of Mariano’s article, I came across this error when trying to run the test:


Fatal error: Cannot redeclare class GroupTest in /xxxx/app/tests/cases/models/group.test.php on line 6

Line six of group.text.php was pretty basic, just declaring the GroupTest class model to use in the test:


class GroupTest extends Group {
    var $name = 'GroupTest';
    var $useDbConfig = 'test_suite';
}

After poking around a bit, I found this, on line 608 of test_case.php in the /vendors/simpletest/ directory.


class GroupTest extends TestSuite { }

Ah! The SimpleTest program itself was declaring the GroupClass for doing group tests. So just renaming my model to “GroupTestModel” or the like fixed the problem.

MAMP, PHP5, CakePHP, and Strict Standards

Tuesday, December 11th, 2007

We’re making the move to PHP5 for our latest project, and it was much to my dismay when I saw a series of errors unleashed during the first test of the Cake pre-beta:



Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/basics.php on line 279

Strict Standards: Redefining already defined constructor for class Object in /Applications/MAMP/htdocs/xxxx/cake/libs/object.php on line 65

Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/libs/object.php on line 92

Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/libs/inflector.php on line 65

Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/libs/configure.php on line 96

Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/libs/configure.php on line 154

Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/libs/cache.php on line 71

Strict Standards: Assigning the return value of new by reference is deprecated in /Applications/MAMP/htdocs/xxxx/cake/libs/cache.php on line 157

Strict Standards: Non-static method Configure::getInstance() should not be called statically in /Applications/MAMP/htdocs/xxxx/cake/bootstrap.php on line 48

Now I know that this wasn’t Cake’s problem. This is PHP5’s new error reporting standard, which was turned on for some reason.

To disable Strict Error Reporting, edit your php.ini file and set the error_reporting to (line 270):


error_reporting  =  E_ALL & ~E_STRICT

Or, in your .htaccess file in your root cake installation, you can add the following line:


php_value error_reporting 6143

Which is the bit value of E_ALL & ~E_STRICT.

Either way, the errors should disappear. The reason they exist in the first place? According to Gwoo, it’s to maintain Cake’s functionality with PHP4.

If you’re still confused, you can always visit PHP’s page on error reporting for more on error-lovin’.