Re: help with get product by user, function in products controller
Hi Marcus I have done the following, but it returns ALL the products NOT just the user's is it possible to point out my error? Thanks My model (user) var $hasMany = array( 'Product' => array('className' => 'Product', 'foreignKey' => 'product_id', product controller function viewbyuser() { $products = $this->User->Product->find('all' , array('conditions' => array('user_id' => $this->Session->read('Auth.User.id') ))); $this->set('products', $this->paginate()); } On Tue, Oct 14, 2008 at 10:33 PM, Marcus Silva <[EMAIL PROTECTED]> wrote: > > Ok, > > You might be able to do this: > > > Add a relationship in your user model; example: User hasMany Product > etc... > > Then in you controller you can do the following: > > > $this->User->id = $id; //Passed in the function > > $userProducts = $this->User->Product->find('all',$params = > array( 'conditions' => add any extra condition in here , it can be > an array as well. )); > > > //then set the data to be available int he view. > > Better yet, you could write a custom function in the User model to get > products for the user, that would save typying etc.. > > in user.php //model > > function getProduct(){ > //The model must be associated before trying to do this. > > return $this->Product->find(); > > } > > in the controller you do: > > $userProducts = $this->User->getProduct(); //SImple > > Hope it helps... > > Cheers > > > > gabriel wrote: >> Hi, i hope someone can help me here please, I need help with my >> controller function, I want to get products by user_id, unfortunately >> I have no idea where to start, help will be greatfully received. >> >> Here is my code so far... >> >> >> class ProductsController extends AppController { >> var $name = 'Products'; >> var $uses = array('Product', 'Category', 'User'); >> var $components = array('Search','Files'); >> >> function view() { >> $this->Product->recursive = 0; >> $this->set('products', $this->paginate()); >> $this->set('users', $this->User->read(null, $id)); >> >> } >> >> Thanks > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: help with get product by user, function in products controller
Ok, You might be able to do this: Add a relationship in your user model; example: User hasMany Product etc... Then in you controller you can do the following: $this->User->id = $id; //Passed in the function $userProducts = $this->User->Product->find('all',$params = array( 'conditions' => add any extra condition in here , it can be an array as well. )); //then set the data to be available int he view. Better yet, you could write a custom function in the User model to get products for the user, that would save typying etc.. in user.php //model function getProduct(){ //The model must be associated before trying to do this. return $this->Product->find(); } in the controller you do: $userProducts = $this->User->getProduct(); //SImple Hope it helps... Cheers gabriel wrote: > Hi, i hope someone can help me here please, I need help with my > controller function, I want to get products by user_id, unfortunately > I have no idea where to start, help will be greatfully received. > > Here is my code so far... > > > class ProductsController extends AppController { > var $name = 'Products'; > var $uses = array('Product', 'Category', 'User'); > var $components = array('Search','Files'); > > function view() { > $this->Product->recursive = 0; > $this->set('products', $this->paginate()); > $this->set('users', $this->User->read(null, $id)); > > } > > Thanks --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: help with get product by user, function in products controller
Thanks Marcus, I will try it, you are a star. On Tue, Oct 14, 2008 at 10:33 PM, Marcus Silva <[EMAIL PROTECTED]> wrote: > > Ok, > > You might be able to do this: > > > Add a relationship in your user model; example: User hasMany Product > etc... > > Then in you controller you can do the following: > > > $this->User->id = $id; //Passed in the function > > $userProducts = $this->User->Product->find('all',$params = > array( 'conditions' => add any extra condition in here , it can be > an array as well. )); > > > //then set the data to be available int he view. > > Better yet, you could write a custom function in the User model to get > products for the user, that would save typying etc.. > > in user.php //model > > function getProduct(){ > //The model must be associated before trying to do this. > > return $this->Product->find(); > > } > > in the controller you do: > > $userProducts = $this->User->getProduct(); //SImple > > Hope it helps... > > Cheers > > > > gabriel wrote: >> Hi, i hope someone can help me here please, I need help with my >> controller function, I want to get products by user_id, unfortunately >> I have no idea where to start, help will be greatfully received. >> >> Here is my code so far... >> >> >> class ProductsController extends AppController { >> var $name = 'Products'; >> var $uses = array('Product', 'Category', 'User'); >> var $components = array('Search','Files'); >> >> function view() { >> $this->Product->recursive = 0; >> $this->set('products', $this->paginate()); >> $this->set('users', $this->User->read(null, $id)); >> >> } >> >> Thanks > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---