Friday, April 3, 2009

Re: retrieve specific data

I'd never used the debugger before so I had to do a little reading
first but I ran it and here's the results:
-------------------------------
Array
(
[0] => Array
(
[Product] => Array
(
[id] => 1
[name] => Item 1
[color_id] => 2
)

[Color] => Array
(
[id] => 2
[name] => White
)

)

[1] => Array
(
[Product] => Array
(
[id] => 2
[name] => Item 2
[color_id] => 1
)

[Color] => Array
(
[id] => 1
[name] => Black
)

)

)
--------------------------------


So in the following under the products->color_id, I know the field is
getting populated in the table as an id but how do I get it to show
the color name that corresponds to that id?

Thanks Brian!

On Apr 3, 8:37 pm, brian <bally.z...@gmail.com> wrote:
> Odd. The query shows that it should be in there. Did you try the
> debug() i mentioned? ;-)
>
> On Fri, Apr 3, 2009 at 10:12 PM, justclint <justcl...@gmail.com> wrote:
>
> > Thanks for the quick response Brian!
>
> > When I add:
>
> > <?php echo $product['Color']['name']; ?>
>
> > I get error:
>
> > Notice (8): Undefined index:  Color [APP\views\products\index.ctp,
> > line 48]
>
> > Any suggestions?
>
> > Thanks!
>
> > On Apr 3, 6:56 pm, brian <bally.z...@gmail.com> wrote:
> >> The SQL query is fetching Color.name. You're looking in the Product
> >> array. The Color name will be in the Color array.
>
> >> <?php echo $product['Color']['name']; ?>
>
> >> When in doubt, do a debug($this->data) in your view.
>
> >> On Fri, Apr 3, 2009 at 9:48 PM, justclint <justcl...@gmail.com> wrote:
>
> >> > My app is working but a couple of my fields are not pulling the data I
> >> > want.
>
> >> > I have a table called products and another table called colors. My
> >> > colors table has 2 fields, id and name.
>
> >> > When adding the product, you select the color (from drop down menu)
> >> > and it will insert the color id into the color_id field of the
> >> > products table.
>
> >> > Once I add a product, instead of displaying the actual color it shows
> >> > the colors id. (ie: 2 instead of blue)
>
> >> > How do I get my page to pull the actual color name not the color id?
>
> >> > When launching the page it does the following query:
>
> >> > SELECT `Product`.`id`, `Product`.`name`, `Product`.`category_id`,
> >> > `Product`.`material_id`, `Product`.`color_id`, `Product`.`sport_id`,
> >> > `Product`.`price`, `Product`.`image_link`, `Product`.`buy_link`,
> >> > `Category`.`id`, `Category`.`name`, `Material`.`id`,
> >> > `Material`.`name`, `Color`.`id`, `Color`.`name`, `Sport`.`id`,
> >> > `Sport`.`name` FROM `products` AS `Product` LEFT JOIN `categories` AS
> >> > `Category` ON (`Product`.`category_id` = `Category`.`id`) LEFT JOIN
> >> > `materials` AS `Material` ON (`Product`.`material_id` =
> >> > `Material`.`id`) LEFT JOIN `colors` AS `Color` ON
> >> > (`Product`.`color_id` = `Color`.`id`) LEFT JOIN `sports` AS `Sport` ON
> >> > (`Product`.`sport_id` = `Sport`.`id`) WHERE 1 = 1 LIMIT 20
>
> >> > I think I need the following part of the query to be changed from:
>
> >> > `colors` AS `Color` ON (`Product`.`color_id` = `Color`.`id`)
>
> >> > to
>
> >> > `colors` AS `Color` ON (`Product`.`color_id` = `Color`.`name`)
>
> >> > My controller is just the default one after creating through the
> >> > console so Products->index looks like:
>
> >> >        function index() {
> >> >                $this->Product->recursive = 0;
> >> >                $this->set('products', $this->paginate());
> >> >        }
>
> >> > My Product model looks like:
>
> >> > var $belongsTo = array(
> >> >                        'Color' => array(
> >> >                        'className' => 'Color',
> >> >                        'foreignKey' => 'color_id',
> >> >                        'conditions' => '',
> >> >                        'fields' => '',
> >> >                        'order' => ''
> >> >                )
> >> > );
>
> >> > My Color model looks like this:
>
> >> >        var $hasMany = array(
> >> >                'Product' => array(
> >> >                        'className' => 'Product',
> >> >                        'foreignKey' => 'color_id',
> >> >                        'dependent' => false,
> >> >                        'conditions' => '',
> >> >                        'fields' => '',
> >> >                        'order' => '',
> >> >                        'limit' => '',
> >> >                        'offset' => '',
> >> >                        'exclusive' => '',
> >> >                        'finderQuery' => '',
> >> >                        'counterQuery' => ''
> >> >                )
> >> >        );
>
> >> > And Products->index view looks like this:
>
> >> > <tr<?php echo $class;?>>
> >> >                <td>
> >> >                        <?php echo $product['Product']['id']; ?>
> >> >                </td>
> >> >                <td>
> >> >                        <?php echo $product['Product']['name']; ?>
> >> >                </td>
>
> >> >                <td>
> >> >                        <?php echo $product['Product']['color_id']; ?>
> >> >                </td>
>
> >> > Your help is greatly appreciated.
>
> >> > 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 cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: