Hi all,
i got what I need, but only after some pretty ugly code.
would appreciate if someone tells me a better way of doing it.
$this->paginate = array(
'fields'=>array('Product.*', 'ProductImage.id',
'ProductImage.filename', 'ProductImage.dir'),
'conditions' => array('OR' =>
array (
array('ProductImage.cover'=>true),
array('ProductImage.cover'=>null),
),
),
'link'=>array('ProductImage'),
'order'=> array('Product.created DESC')
);
$this->paginate['conditions']['AND'] = array('Product.shop_id'
=>
Shop::get('Shop.id'));
$products = $this->paginate('Product');
/* here starts the ugly code */
$images = Set::combine($products, '{n}.ProductImage.product_id',
'{n}.ProductImage.filename');
$products = Set::extract('{n}.Product', $products);
foreach($realProducts as $key=>$product) {
foreach($images as $product_id => $image) {
if ($product['id'] == $product_id) {
$realProducts[$key]['image'] = $image;
unset($images[$product_id]);
break;
}
}
}
/* $realProducts is the result I want */
On Mar 21, 6:44 am, Zeu5 <[email protected]> wrote:
> Hi all,
>
> my pagination code in controller looks like this
>
> $this->paginate = array(
> 'conditions' => array('OR' =>
> array (
> array('ProductImage.cover'=>true),
> array('ProductImage.cover'=>null),
> ),
> ),
> 'link'=>array('ProductImage'),
> 'fields'=>array('Product.*', 'ProductImage.id',
> 'ProductImage.filename', 'ProductImage.dir'),
> 'order'=> array('Product.created DESC')
> );
>
> $this->paginate['conditions']['AND'] = array('Product.shop_id'
> => Shop::get('Shop.id'));
>
> I do not wish to change the conditions whatsoever. I have the
> following as the result of the pagination.
>
> Array
> (
> [0] => Array
> (
> [Product] => Array
> (
> [id] => 8
> [shop_id] => 2
> [title] => test
> [code] =>
> [description] => <p>test</p>
> [price] => 1.0000
> [created] => 2011-03-03 09:51:41
> [modified] => 2011-03-03 09:51:41
> [status] => 1
> [weight] => 1.0000
> [currency] => SGD
> [weight_unit] => kg
> [shipping_required] => 1
> )
>
> [ProductImage] => Array
> (
> [id] => 5
> [filename] => wal_mart_t_shirt_for_web-1.jpg
> [dir] => uploads/products
> [product_id] => 8
> [cover] => 1
> [created] => 2011-03-03 09:51:41
> [modified] => 2011-03-03 09:51:41
> [mimetype] => image/jpeg
> [filesize] => 14479
> )
>
> )
>
> [1] => Array
> (
> [Product] => Array
> (
> [id] => 7
> [shop_id] => 2
> [title] => test
> [code] =>
> [description] => <p>test</p>
> [price] => 0.0000
> [created] => 2011-03-03 08:42:16
> [modified] => 2011-03-03 08:42:17
> [status] => 1
> [weight] => 0.0000
> [currency] => SGD
> [weight_unit] => kg
> [shipping_required] => 1
> )
>
> [ProductImage] => Array
> (
> [id] => 4
> [filename] => wal_mart_t_shirt_for_web-0.jpg
> [dir] => uploads/products
> [product_id] => 7
> [cover] => 1
> [created] => 2011-03-03 08:42:17
> [modified] => 2011-03-03 08:42:17
> [mimetype] => image/jpeg
> [filesize] => 14479
> )
>
> )
>
> I want this instead where the image value is taken from
> ProductImage.filename
>
> Array
> (
> [0] => Array
> (
>
> [id] => 8
> [shop_id] => 2
> [title] => test
> [description] => <p>test</p>
> [price] => 1.0000
> [created] => 2011-03-03 09:51:41
> [modified] => 2011-03-03 09:51:41
> [status] => 1
> [weight] => 1.0000
> [currency] => SGD
> [weight_unit] => kg
> [shipping_required] => 1
> [image] => wal_mart_t_shirt_for_web-1.jpg
> )
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php