I often set up my images table with model & foreign_key columns. The
Image model looks like:
public $hasOne = array(
'Thumbnail' => array(
'ClassName' => 'Portfolio',
'foreignKey' => 'image_id',
'dependent' => true
)
);
public $belongsTo = array(
'Book' => array(
'className' => 'Book',
'foreignKey' => false,
'conditions' => array(
'Image.model' => 'Book',
'Image.foreign_key = Book.id'
),
'dependent' => true
)
// etc.
Book model, for example:
public $hasMany = array( // could also be $hasOne
'Image' => array(
'className' => 'Image',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Image.model' => 'Book'
),
'dependent' => true
)
);
With this schema, we could create an action in ImagesController:
public function foo($model, $fk, $order, $limit) {
// let's keep the controller lean
return $this->Image->foo($model, $fk, $order, $limit);
}
Image:
public function foo($model, $fk, $order, $limit)
{
return $this->find(
'all',
array(
'conditions' => array(
$this->alias.'.model' => $model,
$this->alias.'.foreign_key' => $fk
),
'order' => $order,
'limit' => $limit
)
);
}
So your carousel element could simply call requestAction() on a single
controller action. (Just remember to cache the element!)
One issue I see right off the bat is that you want to order your User
pics by rating. I presume that's User.rating -- that would add some
complexity which I'm not sure how to address.
Another issue might be if you want to link to the User or Article
itself, rather than just pop up an enlarged image. That is, it'd be a
problem if you're using non-standard routes. If you get to a given
User with '/users/view/:id' and an Article with '/articles/view/:id'
then you're golden, because the element has all the info it needs to
construct the links. But if you're using something like '/users/:slug'
you've got more problems.
On Sun, Dec 9, 2012 at 11:51 AM, marcus33cz <marcus33cz@gmail.com> wrote:
> Hi all,
>
> first of all I must say cakePHP is amazing how everything works
> automagically. :-) (I'm still learning but once I've learned enough, I'll be
> happy to help others.)
>
> Now I'm wondering about best practice for a CAROUSEL - basically a piece of
> website with rotating images
>
> . Let's say I want to:
> --- a carousel that can display different models and entries from the
> database (e.g. "latest 3 articles", "top 5 players", "top 3 most active
> teams")
> --- this carousel should be reusable and universal
> (I don't care about the javascript, I already have that)
>
> How would you do it? My idea is:
> --- create an element (carousel.ctp in View/Elements)
> --- allow to pass some parameters to the carousel element
> --- include this element in different views with different parameters
> --- the element would then loop through the parameters and display something
> according to them
>
> So for example:
>
> In one view:
>>
>> $this->element('carousel',
>> array(
>> 'model' => "articles",
>> 'limit' => 3,
>> 'order' => "created DESC"
>> )
>> );
>
>
> In another view:
>>
>> $this->element('carousel',
>> array(
>> 'model' => "users",
>> 'limit' => 5,
>> 'order' => "rating DESC"
>> )
>> );
>
>
> My question is --- should the carousel element really do the work (e.g.
> getting last 3 articles)?
> Shouldn't that be in some controller --- and if yes what one (I want to
> avoid any duplicities)
>
> Thanks a lot!
>
> --
> 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 post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>
--
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 post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
Sunday, December 9, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment