This blog is about the PHP solutions I have to think of when I'm developing systems almost every single day...

Wednesday, February 27, 2008

Warning!!! The error is cached

I tried to create my own behavior in CakePHP today. Using ideas from the attachments solution, my first step was just to try to read in data that I've already manually keyed into the database. Associate them with each other. This is what I have done so far (goes into models/behaviors/comment.php):


<?php
class CommentBehavior extends ModelBehavior
{
var $settings=array();
var $runtime=array();
var $Comment;

function __construct(){
$this->Comment=ClassRegistry::init('Comment','model');
}

function setup(&$model,$config = array()){
$this->settings[$model->alias]['assocAlias'] = $model->alias.'Comment';
return true;
}

public function beforeFind(&$model, $query){
$commentcond['Comment.model']=$model->alias;
if(!empty($query['conditions']) && is_array($query['conditions'])) {
foreach($query['conditions'] as $fieldName => $constraint) {
if(!strstr($fieldName,'Comment.')) {
continue;
}

$commentcond[$fieldName] = $constraint;
unset($query['conditions'][$fieldName]);
}
}

$this->runtime[$model->alias]['query']['conditions'] = $commentcond;
return $query;
}

public function afterFind(&$model,$results,$primary){
extract($this->settings[$model->alias]);
foreach($results as &$result){
if(!isset($result[$model->alias][$model->primaryKey])){
continue(1);
}
$commentcond=$this->runtime[$model->alias]['query']['conditions'];
$commentcond['Comment.foreign_key']=$result[$model->alias][$model->primaryKey];
$comment=$this->Comment->findAll($commentcond);
if(empty($comment)){
continue(1);
}
$result[$assocAlias]=$comment;
}
return $results;
}
}

class Comment extends AppModel
{
var $name="Comment";
var $useTable="comments";
}
?>


Quite straight forward I think. But earlier on I made a mistake and cakephp spit out a request address not found error. I cut down all the additional code until I got to the very basic which you see above and it still gives out that error. I was stumped. Well, a quick search into the cakephp google groups shows that there is caching for the models and so I deleted the content of tmp/cache/models. It works after that. Alhamdullillah.. :)

0 comments:

About Me

My Photo
Abdullah Zainul Abidin
I am what I am because of what I am...
View my complete profile