Saturday, February 9, 2013

CakeDC Categories with Associated Models

I'm attempting to use the CakeDC Categories plugin in CakePHP 2.3.0. Unfortunately, CakeDC's documentation is notoriously vague and incomplete, and my knowledge of Cake is very elementary. The problem is that, when dealing with hierarchical categories, Category.name is very unhelpful. For instance, in the cases of CakePHP > Views > Forms and HTML > Forms, Category.name would be "Form" in both instances. To deal with that, I have the following function in my Categories.Category model:

public function catFullPath($id = NULL) {
$models = $this->getPath($id);
$path   = '';
foreach ($models as $model) {
$path .= $model['Category']['name'] . ' » ';
}
$path = substr($path, 0, strlen($path) - 3);

return $path;
}
(That function came from a very helpful person on this group.) That works great when I'm dealing with a Categories view. Where it breaks down is when I attempt to use it in an associated model. To start with, I have this in my Category model:

public $hasMany = array(
'ChildCategory' => array(
'className'  => 'Categories.Category',
'foreignKey' => 'category_id',
'dependent'  => FALSE
),
'Collection'    => array(
'className'  => 'Collection',
'foreignKey' => 'category_id',
'dependent'  => FALSE
)
);
 In my Collection model, I've got:
 
  public $belongsTo = array(
'Type'     => array(
'className'  => 'Type',
'foreignKey' => 'type_id',
'order'      => 'Type.name',
),
'User'     => array(
'className'  => 'User',
'foreignKey' => 'user_id',
'order'      => 'User.username',
),
'Category' => array(
'className' => 'Categories.Category',
'order'     => 'Category.name',
)
);

However, when I try this in a Collections view, it throws a "missing helper" error:

echo $this->Collection->Category->catFullPath($collection['Category']['id']);
I've tried several variations, including:

echo $this->Collection->Categories.Category->catFullPath($collection['Category']['id']);
echo $this->Collection->Categories/Category->catFullPath($collection['Category']['id']);
echo $this->Category->catFullPath($collection['Category']['id']);
Of course, none of those worked. (And they shouldn't be needed since the path to the Category class is defined in the blongsTo association.) I even tried adding app::import('Categories.Category'); in the view which didn't work either.

I'm stumped. The "missing CollectionHelper" error message seems to imply that Cake can't find the catFullPath() function. I can't seem to find anything on using functions from associated plugin models. 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: