Quantcast

Model associations

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Model associations

André Luis
Hi people, it´s me again!

I am using a habtm relationship in my application, it works 100% fine... But the Product menu is created dinamically with it´s Categories... so i have Product model and the Category model with habtm relationship between them.

In AppController i have to set at beforeFilter the variable "products_categories", as i dont need the products of each category right now i use $this->set('products_categories",$this->Product->CatProduct->find('all')); and it works fine... BUT, latter when i need to read the product category with it´s products, i would use $this->CatProduct->read('*',$id); right? BUT it´s returning ONLY the category, not the products relateds, but if i comment the line $this->Product->CatProduct->find('all') it works fine, and returns me the category and all related products.

What i am doing wrong? Isnt there anyway to get only the categories them get one category with it´s relateds?

Thanks!

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Model associations

André Luis
Now when i try to use the find all in appcontroller it seems to remove my behaviors for latter uses, what i am doing wrong??

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Model associations

André Luis
In reply to this post by André Luis
I´ve noticed that if i try do use the CatProduct in AppModel, the Product model becomes a AppModel object, and not a Product object anymore
=(

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Model associations

Mark Wratten
In reply to this post by André Luis
Do you have var $belongsTo = array('Category', 'Product'); in your CatProduct model?

On Thursday, August 9, 2012 10:43:43 AM UTC-4, André Luis wrote:
Hi people, it´s me again!

I am using a habtm relationship in my application, it works 100% fine... But the Product menu is created dinamically with it´s Categories... so i have Product model and the Category model with habtm relationship between them.

In AppController i have to set at beforeFilter the variable "products_categories", as i dont need the products of each category right now i use $this->set('products_categories",$this->Product->CatProduct->find('all')); and it works fine... BUT, latter when i need to read the product category with it´s products, i would use $this->CatProduct->read('*',$id); right? BUT it´s returning ONLY the category, not the products relateds, but if i comment the line $this->Product->CatProduct->find('all') it works fine, and returns me the category and all related products.

What i am doing wrong? Isnt there anyway to get only the categories them get one category with it´s relateds?

Thanks!

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Model associations

lowpass
In reply to this post by André Luis
I'm not sure I follow all of that but I think what you want is a list
of all Categories for the main page, and all Products for a given
Category. If that's the case, I would set the main page as
CategoryController::index(). This is from one of my projects:

Router::connect(
        '/catalog',
        array(
                'controller' => Categories',
                'action' => 'index'
        )
);
Router::connect(
        '/catalog/:slug',
        array(
                'controller' => Categories',
                'action' => 'view'
        ),
        array(
                'slug' => '[-a-z0-9]+',
                'pass' => array('slug')
        )
);

Next, fetch the Category list directly from that model:

public function index()
{
        $this->set(
                'data',
                $this->Category->find(
                        'all',
                        array(
                                'order' => array(
                                        'Category.name' => 'ASC'
                                ),
                                'recursive' => -1
                        )
                )
        );
}

public function view($slug = null)
{
        if (empty($slug))
        {
                ...
        }
       
        $this->set(
                'data'
                $this->Category->fetch($slug)
        );
}


Category model:

public function fetch($slug)
{
        return $this->find(
                'first',
                array(
                        'conditions' => array(
                                $this->alias.'.slug' => $slug
                        ),
                        'contain' => array(
                                'Product'
                        )
                )
        );
}


On Thu, Aug 9, 2012 at 10:43 AM, André Luis <[hidden email]> wrote:

> Hi people, it´s me again!
>
> I am using a habtm relationship in my application, it works 100% fine... But
> the Product menu is created dinamically with it´s Categories... so i have
> Product model and the Category model with habtm relationship between them.
>
> In AppController i have to set at beforeFilter the variable
> "products_categories", as i dont need the products of each category right
> now i use
> $this->set('products_categories",$this->Product->CatProduct->find('all'));
> and it works fine... BUT, latter when i need to read the product category
> with it´s products, i would use $this->CatProduct->read('*',$id); right? BUT
> it´s returning ONLY the category, not the products relateds, but if i
> comment the line $this->Product->CatProduct->find('all') it works fine, and
> returns me the category and all related products.
>
> What i am doing wrong? Isnt there anyway to get only the categories them get
> one category with it´s relateds?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to
> [hidden email].
> Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
>
>

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Model associations

André Luis
In reply to this post by Mark Wratten
Yes i do... I resolved the problem using requestAction, but i know that´s not correct.

Em quinta-feira, 9 de agosto de 2012 17h28min29s UTC-3, Mark Wratten escreveu:
Do you have var $belongsTo = array('Category', 'Product'); in your CatProduct model?

On Thursday, August 9, 2012 10:43:43 AM UTC-4, André Luis wrote:
Hi people, it´s me again!

I am using a habtm relationship in my application, it works 100% fine... But the Product menu is created dinamically with it´s Categories... so i have Product model and the Category model with habtm relationship between them.

In AppController i have to set at beforeFilter the variable "products_categories", as i dont need the products of each category right now i use $this->set('products_categories",$this->Product->CatProduct->find('all')); and it works fine... BUT, latter when i need to read the product category with it´s products, i would use $this->CatProduct->read('*',$id); right? BUT it´s returning ONLY the category, not the products relateds, but if i comment the line $this->Product->CatProduct->find('all') it works fine, and returns me the category and all related products.

What i am doing wrong? Isnt there anyway to get only the categories them get one category with it´s relateds?

Thanks!

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Model associations

André Luis
In reply to this post by lowpass
cricket, thanks for the answear, but it´s not exactly this... I have a menu wich is generated dinamically in ALL pages of the site, so it´s a part of the layout and the categories information needs to be in all pages.

Em sexta-feira, 10 de agosto de 2012 00h23min04s UTC-3, cricket escreveu:
I'm not sure I follow all of that but I think what you want is a list
of all Categories for the main page, and all Products for a given
Category. If that's the case, I would set the main page as
CategoryController::index(). This is from one of my projects:

Router::connect(
        '/catalog',
        array(
                'controller' => Categories',
                'action' => 'index'
        )
);
Router::connect(
        '/catalog/:slug',
        array(
                'controller' => Categories',
                'action' => 'view'
        ),
        array(
                'slug' => '[-a-z0-9]+',
                'pass' => array('slug')
        )
);

Next, fetch the Category list directly from that model:

public function index()
{
        $this->set(
                'data',
                $this->Category->find(
                        'all',
                        array(
                                'order' => array(
                                        'Category.name' => 'ASC'
                                ),
                                'recursive' => -1
                        )
                )
        );
}

public function view($slug = null)
{
        if (empty($slug))
        {
                ...
        }
        
        $this->set(
                'data'
                $this->Category->fetch($slug)
        );
}


Category model:

public function fetch($slug)
{
        return $this->find(
                'first',
                array(
                        'conditions' => array(
                                $this->alias.'.slug' => $slug
                        ),
                        'contain' => array(
                                'Product'
                        )
                )
        );
}


On Thu, Aug 9, 2012 at 10:43 AM, André Luis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="deg7nl4D8ikJ">cava...@...> wrote:

> Hi people, it´s me again!
>
> I am using a habtm relationship in my application, it works 100% fine... But
> the Product menu is created dinamically with it´s Categories... so i have
> Product model and the Category model with habtm relationship between them.
>
> In AppController i have to set at beforeFilter the variable
> "products_categories", as i dont need the products of each category right
> now i use
> $this->set('products_categories",$this->Product->CatProduct->find('all'));
> and it works fine... BUT, latter when i need to read the product category
> with it´s products, i would use $this->CatProduct->read('*',$id); right? BUT
> it´s returning ONLY the category, not the products relateds, but if i
> comment the line $this->Product->CatProduct->find('all') it works fine, and
> returns me the category and all related products.
>
> What i am doing wrong? Isnt there anyway to get only the categories them get
> one category with it´s relateds?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="deg7nl4D8ikJ">cake...@....
> To unsubscribe from this group, send email to
> <a href="javascript:" target="_blank" gdf-obfuscated-mailto="deg7nl4D8ikJ">cake-php+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
>
>

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 
Loading...