Firstly I'd change the published field definition to tinyint(1) unsigned as 
Cake will better be able to recognise it as a boolean and you'll save some db 
space.

Secondly I'd start using the Containable behaviour for everything - its the way 
to go as it gives you this sort of control.

Thirdly you can alter your Category model so that it hasMany PublishedProduct 
that has a condition of 'published' => 1 (or true). Then you can do the query 
through the Category model and you'll only get published products:

var $hasMany = array(
        ...,
        'PublishedProduct' => array(
                'className' => 'Product',
                'foreignKey' => 'category_id',
                'conditions' => array(
                        'PublishedProduct.published' => 1
                )
        ),
        ...
);

Then you can query your Category using the Containable behaviour and instead of 
containing 'Product' you contain 'PublishedProduct'.

$category = $this->Category->find(
        'first',
        array(
                'conditions' => array('Category.id' => $id),
                'contain' => array('PublishedProduct')
                )
        )
);

Alternatively, you can (still using the Containable behaviour) contain Product 
but with a condition:

$category = $this->Category->find(
        'first',
        array(
                'conditions' => array('Category.id' => $id),
                'contain' => array(
                        'Product' => array(
                                'conditions' => array('Product.published' => 1)
                        )
                )
        )
);


Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 20 Oct 2011, at 22:21, vaughany wrote:

> Hi all. I'm a Cake newbie so this may be quite obvious to many, but I'm 
> learning on the job, and I know what I want to achieve, just not too sure how 
> to achieve it.
> 
> In my ProductsController, originally 'cake baked' in 2.0 RC3, I have the 
> following:
> 
> public function view($id = null) {
>     $this->Product->id = $id;
>     if (!$this->Product->exists()) {
>         throw new NotFoundException(__('Invalid product'));
>     }
>     if ($this->Product->field('published') == true) {
>         $this->set('product', $this->Product->read(null, $id));
>     } else {
>         // TODO: consider throw new NotFoundException too
>         $this->Session->setFlash(__('That product is not published.'));
>         $this->redirect(array('action' => 'index'));
>     }
> }
> 
> The purpose of the if/else structure is to see if a given product has been 
> published or not, which is achieved through a 'published INT (1)' column in 
> the products table. I believe I am doing this the 'right' way, and it works 
> fine within /products, but when I come to my CategoriesController's view 
> action (below, passed /categories/view/1 and so on) it shows all items in a 
> given category whether published or not:
> 
> public function view($id = null) {
>     $this->Category->id = $id;
>     if (!$this->Category->exists()) {
>         throw new NotFoundException(__('Invalid category'));
>     }
>     // some logic which checks for products in this category which are not 
> published.
>     $this->set('category', $this->Category->read(null, $id));
> }
> 
> Note: Product belongsTo Category, Category hasMany Product.
> 
> I hope this is clear enough. 
> 
> Cheers.
> 
> -- 
> 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

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

Reply via email to