Re: association - foreach
On 7/4/06, Ryan Ginstrom <[EMAIL PROTECTED]> wrote: > > > [mailto:[EMAIL PROTECTED] On Behalf Of ShepherdWeb > > That makes sense. My current implementation is the other way around > > though: > > > > (my url looks like this: /products/color/) > > So... > > In colors_controller: > > function getById( $id ) > { >$this->Color->id = $id ; >return $this->Color->read() ; > } > > In products_controller: > > function color( $id ) > { >$this->set( 'data', $this->requestAction( "/colors/getById/$id" ) ) ; > } if you're after the colour, you could also get it like so: // in products_controller.php function color ($name) { // set id for color $this->Product->Color->id = $id; // set recursive var for Color model so it pulls proper product details in $this->Product->Color->recursive = 2; // find color details (and associated models $color = $this->Product->Color->find(); } this should output something along the lines of: [Color] => Array ( [id] => 2 [name] => Lime [hex] => 3beb63 [Product][0] => Array ( [id] => 2 [title] => Another Test Product [description] => some more description and stuff yeah yeah yay [inventory] => 15 [price] => 3.49 [color_id] => 2 ) [Product] => Array ( [id] => 5 [title] => Green Headband [description] => this is a nice green headband [inventory] => 3 [price] => 2.54 [color_id] => 2 ) ) hth jon -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
Re: association - foreach
I did have that class...and the model. I'll try adding "var $scaffold = true;" and see what happens. Thanks for the tip. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
RE: association - foreach
> [mailto:[EMAIL PROTECTED] On Behalf Of ShepherdWeb > class ColorsController extends AppController > { > var $name = 'Colors'; > } > ?> > > in file : app/controllers/colors_controller.php Do you have this class? You need it. And put that getById function inside it. You will also need to create a Color model, if you don't have one. You could start with this: Color->findById( $id ) ; } } ?> -- Regards, Ryan Ginstrom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
Re: association - foreach
do I need to make some sort of adjustment to Routes.php? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
Re: association - foreach
Good point. I'm getting this error using your code though: Missing controller You are seeing this error because controller ColorsController could not be found. Notice: this error is being rendered by the app/views/errors/missing_controller.thtml view file, a user-customizable error page for handling invalid controller dispatches. Fatal: Unable to load controller ColorsController Fatal: Create Class: in file : app/controllers/colors_controller.php --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
RE: association - foreach
> [mailto:[EMAIL PROTECTED] On Behalf Of ShepherdWeb > Thanks . No problem. Just thought I'd mention that in addition to being prettier, getting the color data from the color controller has another advantage. What happens if there are no products of that color? Keying off the product model will get you an empty set, and now you don't know what color you were looking for. If you go from the color model, you will get a color entry, and an empty set of products, so you will be able to tell the user, "Sorry, no mauve products." -- Regards, Ryan Ginstrom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
Re: association - foreach
Thanks . --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
RE: association - foreach
> [mailto:[EMAIL PROTECTED] On Behalf Of ShepherdWeb > That makes sense. My current implementation is the other way around > though: > > (my url looks like this: /products/color/) So... In colors_controller: function getById( $id ) { $this->Color->id = $id ; return $this->Color->read() ; } In products_controller: function color( $id ) { $this->set( 'data', $this->requestAction( "/colors/getById/$id" ) ) ; } // view same... And you're good to go -- Regards, Ryan Ginstrom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
Re: association - foreach
That makes sense. My current implementation is the other way around though: (my url looks like this: /products/color/) in product_controller function color($color_id = null) { $this->set('products', $this->Product->findAllByColor_id($color_id); } I found this solution, but it doesn't seem very elegant to me. In the view: ... Output data ... ... etc ... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---
RE: association - foreach
> From: cake-php@googlegroups.com > Color hasMany Product, Product belongsTo Color. I'm trying to return > all the products in a specific color category...and display the > color.name at the top of the page. So if you retrieve the color instead, you should have all its associated products. In color_controller: function products( $id ) { $this-Color->id = $id ; $this->set( 'data', $this->Color->read() ) ; } In view: Products ... ... Output data ... -- Regards, Ryan Ginstrom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" 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 -~--~~~~--~~--~--~---