Friday, April 3, 2009

Re: retrieve specific data

No, I dont have any afterFind code.

On Apr 3, 9:29 pm, justclint <justcl...@gmail.com> wrote:
> Thanks Brian, I in regards to the category and other fields, they have
> the same issue but I've just deleted them so just to simplify and to
> work with one item and then I can repeat whatever the solution is.
>
> Yes, you are correct about the foreach. It is as follows on the index
> view:
> -------------------------
> <?php
> echo $paginator->counter(array(
> 'format' => __('Page %page% of %pages%, showing %current% records out
> of %count% total, starting on record %start%, ending on %end%', true)
> ));
> ?></p>
> <? //debug($products, $showHTML = false, $showFrom = true); ?>
> <table cellpadding="0" cellspacing="0">
> <tr>
>         <th><?php echo $paginator->sort('id');?></th>
>         <th><?php echo $paginator->sort('name');?></th>
>         <th><?php echo $paginator->sort('color_id');?></th>
>         <th class="actions"><?php __('Actions');?></th>
> </tr>
> <?php
> $i = 0;
> foreach ($products as $product):
>         $class = null;
>         if ($i++ % 2 == 0) {
>                 $class = ' class="altrow"';
>         }
> ?>
>         <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>
>                 <td class="actions">
>                         <?php echo $html->link(__('View', true), array('action'=>'view',
> $product['Product']['id'])); ?>
>                         <?php echo $html->link(__('Edit', true), array('action'=>'edit',
> $product['Product']['id'])); ?>
>                         <?php echo $html->link(__('Delete', true), array
> ('action'=>'delete', $product['Product']['id']), null, sprintf(__('Are
> you sure you want to delete # %s?', true), $product['Product']
> ['id'])); ?>
>                 </td>
>         </tr>
> <?php endforeach; ?>
> </table>
> ----------------------
>
> Thanks!
>
> On Apr 3, 9:20 pm, brian <bally.z...@gmail.com> wrote:
>
> > Wait a sec--you also don't have any Category, Sport, or Material data
> > in that array, although they are clearly part of the query.
>
> > But, then again, you *do* have Color in there. Weird.
>
> > On Sat, Apr 4, 2009 at 12:17 AM, brian <bally.z...@gmail.com> wrote:
> > > I just looked closer and see that you have:
>
> > > $this->set('products', $this->paginate());
>
> > > So, your view var should be $products. So, is this line inside of a loop?
>
> > > <?php echo $product['Product']['id']; ?>
>
> > > IOW, you're doing something like:
>
> > > foreach($products as $product)
>
> > > Anyway, as you can see from the debug output, the Color array is
> > > present alongside each Product. So this seems a bit baffling. Can you
> > > post the entire loop?
>
> > > BTW, you don't need the query to be:
>
> > > `colors` AS `Color` ON (`Product`.`color_id` = `Color`.`name`)
>
> > > ... as it's Color.id that Product.color_id is the foreign key for.
> > > What you want (and what you seem to be getting, if not able to access)
> > > is Color.name.
>
> > > I'm not sure what to make of this. Do you have any afterFind code?
>
> > > On Fri, Apr 3, 2009 at 11:58 PM, justclint <justcl...@gmail.com> wrote:
>
> > >> I forgot to mention, this is the dubug I ran: debug($products,
> > >> $showHTML = false, $showFrom = true);
>
> > >> On Apr 3, 8:56 pm, justclint <justcl...@gmail.com> wrote:
> > >>> 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
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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: