Saturday, February 9, 2013

Re: CakeDC Categories with Associated Models

That works super-great for something like view.ctp where you're dealing with a single entity/record. That's exactly what I've done for those situations. The issue becomes much, much more complicated for something like index.ctp where you have to apply it to every record with a foreach loop. It's much easier to do that in the view file than the cumbersome route of creating the array in the model, calling it into the controller and passing it on to the view. (I know it's Cake heresy to say that, but it is totally complicated the Cake way.) I suppose I'll have to do it that way.

On Saturday, February 9, 2013 5:28:10 PM UTC-6, cricket wrote:
I think you're mixing up Models and Helpers. Your Collection model has
an association with Categories.Category, using the alias Category, so
you can do $this->Collection->Category->whatever()

But, you can't call model methods in the view. You can, but you
shouldn't. From the view's perspective, $this->SomeClass is a call to
a helper. And to do that you need to include it in the controller's
$helpers array.

The simplest solution would be to just make the call to the model from
the controller action and set the returned string as a view var:

$this->set('path', $this->Collection->Category->catFullPath(...));

Better yet, create an afterFind() method in the model and add the full
path to your data array.

On Sat, Feb 9, 2013 at 11:42 AM, Larry Lutz <lut...@swbell.net> wrote:
> 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+u...@googlegroups.com.
> To post to this group, send email to cake...@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.
>
>

--
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: