its not a big deal, but on the first view you querying the database 3 times..

you could save on that by doing something like:

public function view($id = null) {
                $product = $this->Product->find('first', array(
                        'recursive' => -1,
                        'conditions' => array('Product.id' => (integer) $id)
                ));

                if (empty($product)) {
                        throw new NotFoundException(__('Invalid product'));
                }

                if ($product['Product][''published] == 0) {
                        $this->Session->setFlash(__('That product is not 
published.'));
                        $this->redirect(array('action' => 'index'));
                }

                $this->set(compact('product'));
}


Andras Kende


On Oct 20, 2011, at 4:21 PM, 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