The secret is getting the data right in the controller before you pass it down 
to the view. I'd recommend the Containable behaviour (see this post I just 
replied to) and then do this in your controller function:

$contact = $this->Contact->find(
        'first',
        array(
                'conditions' => array(
                        'Contact.id' => $id,
                ),
                'contain' => array(
                        'Capital' => array(
                                'Capitalclass'
                        )
                )
        )
);

$this->set('contact', $contact);

Then in your view you can do:

<td><?php echo $contact['Capital']['Capitalclass']['name'];?></td>

To see what the full array looks like, do this in your view:
die(debug($contact));

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 5 Oct 2010, at 09:39, AvE wrote:

> Hi all,
> 
> I'm kind of new to CakePHP and currently learning the system. So far
> I've been able to find answers to my questions via internet and in
> this group, but this time I have a problem where I can't find the
> answer to, also because it's a bit hard to describe.
> 
> I'm building an application to store information about companies. Each
> company can have several share capitals and each share capital belongs
> to a class of shares (ie: ordinary, preferent, etc).
> 
> For this I made some tables:
> 
> The table 'contacts', which has a field capital_id to link it to the
> table capitals
> The table 'capitals', which has a field capitalclass_id to link to the
> table capitalclasses
> The table 'capitalclasses', which just has two fields 'id' and 'name'.
> 
> A contact (company) can have several capitals. Each capital only
> belongs to one capitalclass.
> 
> The relationship between tables in the code works very well, I used
> cake bake to make the controller and views. When I view a contact all
> data is displayed well.
> 
> Also on the bottom the 'baked' code will display the capital details,
> now it's being displayed as:
> 
> Capitalclass_id - Amount - Value
> 1 - 100 - 1
> 
> Of course I don't want the capitalclass_id to be displayed, I want the
> name of the capitalclass to be displayed (in this case 'ordinary' for
> ordinary shares).
> 
> As this is the view of a contact and contact has no direct link with
> capitalclass, but only via capital, how can I accomplish this?
> 
> The code of contacts/view.ctp to display the capital data:
> 
> <?php if (!empty($contact['Contact']['companyname']) &&
>                 $contact['Status']['status'] == 'ACTIVE'):?>
>           <div class="related">
>           <h3><?php __('Capital');?></h3>
>           <?php if (!empty($contact['Capital'])):?>
>           <table cellpadding = "0" cellspacing = "0">
>          <tr>
>                   <th><?php __('Capitalclass Id'); ?></th>
>                   <th><?php __('Currency Id'); ?></th>
>                   <th><?php __('Amount'); ?></th>
>                   <th><?php __('Value'); ?></th>
>                   <th class="actions"><?php __('Actions');?></th>
>           </tr>
>           <?php
>                   $i = 0;
>                   foreach ($contact['Capital'] as $capital):
>                           $class = null;
>                           if ($i++ % 2 == 0) {
>                                   $class = ' class="altrow"';
>                           }
>                   ?>
>                   <tr<?php echo $class;?>>
>                           <td><?php echo $capital['capitalclass_id'];?></td> 
> // Here
> should capitalclass.name be displayed instead.
>                           <td><?php echo $capital['currency_id'];?></td>
>                           <td><?php echo $capital['amount'];?></td>
>                           <td><?php echo $capital['value'];?></td>
>                           <td class="actions">
>                                   <?php echo $html->link(__('View', true), 
> array('controller' =>
> 'capitals', 'action' => 'view', $capital['id'])); ?>
>                                   <?php echo $html->link(__('Edit', true), 
> array('controller' =>
> 'capitals', 'action' => 'edit', $capital['id'])); ?>
>                                   <?php echo $html->link(__('Delete', true), 
> array('controller'
> => 'capitals', 'action' => 'delete', $capital['id']), null,
> sprintf(__('Are you sure you want to delete # %s?', true),
> $capital['id'])); ?>
>                           </td>
>                   </tr>
>           <?php endforeach; ?>
>           </table>
>    <?php endif; ?>
> 
>           <div class="actions">
>                   <ul>
>                           <li><?php echo $html->link(__('New Capital', true),
> array('controller' => 'capitals', 'action' => 'add'));?> </li>
>                   </ul>
>           </div>
>    </div>
> 
> I hope it's clear what I mean as it's a bit hard for me to describe
> the problem.
> 
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
> 
> 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+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to