On Sat, Apr 11, 2009 at 12:57 PM, wilfredo
<[email protected]> wrote:
>
> I have a problem when I try to select distinct fields in an associated
> model with find. The models "Address" und "Expertise" have a HABTM
> relation.
>
> I have the following code in my controller. (There is no table named
> mitglieder).
>
> <?php
> class MitgliederController extends AppController {
>
>        var $name = 'Mitglieder';
>        var $uses = array('Address','Expertise');
>
>        function de_index() {
>                $fachgebiete = $this->Expertise->find('list');
>                $this->set('fachgebiete',$fachgebiete);
>                $berufe = $this->Presentation->query('SELECT DISTINCT
> beruf FROM `addresses` WHERE `mitglied` = "J" ORDER BY beruf ASC');
>                $this->set('berufe',$berufe);
>                $mitglieder = $this->Address->find('all',array
> ('fields'=>array
> ('Address.name','Expertise.fachgebiet'),'conditions'=>'`Address.mitglied`
> = "J"','order'=>'`created` ASC','recursive'=>'1'));
>                 $this->set('mitglieder',$mitglieder);
>           }
>
> }
> ?>
>
> I get the following error for the last find: "SQL Error: 1054: Unknown
> column 'Expertise.fachgebiet' in 'field list'"
>
> Query: SELECT `Address`.`name`, `Expertise`.`fachgebiet` FROM
> `addresses` AS `Address`   WHERE `Address`.`mitglied` = "J"   ORDER BY
> `created` ASC
>

You seem to have some other problem here. Does the "fachgebiet" column
exist? And it looks like you'll need to use Containable.

$mitglieder = $this->Address->find(
        'all',
        array(
                'fields' => array('Address.name'),
                'conditions' => array(
                        'Address.mitglied' => 'J'
                ),
                'order' => array('Address.created' => 'ASC'),
                'contain' => array(
                        'Expertise' => array(
                                'fields' => array(
                                        'Expertise.fachgebiet'
                                )
                        )
                )
        )
);


For DISTINCT, you can do:

$berufe = $this->Presentation->find(
        'all',
        array(
                'fields' => array('DISTINCT Address.beruf'),
                'order' => array('Address.beruf' => 'ASC')
        )
);

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to