Friday, April 3, 2009

retrieve specific data

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: