سؤال

I got the following result after using Paginate in Cake:

  array(
        (int) 0 => array(
            'DestinationPhoto' => array(
                'id' => '1',
                'name' => 'picture.jpg',
                'destination_id' => '2'
            ),
            'Destinations' => array(
                'id' => '2',
                'country_id' => '2'
            ),
            'DestinationIdiom' => array(
                'id' => '3',
                'name' => 'TestName-1',
                'description' => 'TestDescription',
                'idiom' => 'English',
                'destination_id' => '2'
            )
        ),
        (int) 1 => array(
            'DestinationPhoto' => array(
                'id' => '1',
                'name' => 'picture.jpg',
                'destination_id' => '2'
            ),
            'Destinations' => array(
                'id' => '2',
                'country_id' => '2'
            ),
            'DestinationIdiom' => array(
                'id' => '4',
                'name' => 'TestName-2',
                'description' => 'TestDescription-2',
                'idiom' => 'Spanish',
                'destination_id' => '2'
            )
        )
)

As it seems i got multiple instances of "DestinationPhoto", "Destinations" caused by differents "DestinationIdiom" in the same result, it's possible to join both "DestinationIdiom" to be displayed as the same result of the paginator? like

 array(
        (int) 0 => array(
            'DestinationPhoto' => array(
                'id' => '1',
                'name' => 'huehuehuehuh.jpg',
                'destination_id' => '2'
            ),
            'Destinations' => array(
                'id' => '2',
                'country_id' => '2'
            ),
            'DestinationIdiom' => array(
                (int) 0 => array( 
                                    'id' => '3',
                        'name' => 'TestName-1',
                        'description' => 'TestDescription',
                        'idiom' => 'English',
                        'destination_id' => '2'
                                    )
                (int) 1 => array( 
                                    'id' => '3',
                        'name' => 'TestName-2',
                        'description' => 'TestDescription',
                        'idiom' => 'Spanish',
                        'destination_id' => '2'
                                    )
            )
        )

)

In the DestinationPhoto Controller i have the following:

        $this->paginate = array(
        'fields'=>array('DestinationPhoto.*','Destinations.*','DestinationIdiom.*'),
        'joins' => array(
            array(
                'table' => 'destinations',
                'alias'=>'Destinations',
                'type' => 'LEFT',
                'conditions' => '`DestinationPhoto`.`destination_id` = `Destinations`.`id`'
            ),
            array(
                'table' => 'destination_idioms',
                'alias'=>'DestinationIdiom',
                'type' => 'LEFT',
                'conditions' => '`DestinationIdiom`.`destination_id` = `Destinations`.`id`'
            )
        ),
        'limit' => 20
    );
هل كانت مفيدة؟

المحلول

Use the Model naming convention. Make it Destination instead of Destinations.

Bind the model association at run time like given below:

$this->paginate = array(
    'joins' => array(
        array(
            'table' => 'destinations',
            'alias'=>'Destination',
            'type' => 'LEFT',
            'conditions' => '`DestinationPhoto`.`destination_id` = `Destinations`.`id`'
        )),
    'limit' => 20,
    'recursive' => '2'
    );

    $this->DestinationPhoto->Destination->bindModel(array('hasMany' => array('DestinationIdiom' => array('className' => 'DestinationIdiom',
                                                                                                         'foreignKey' => 'destination_id'))), false);
    $result = $this->paginate('Destination');
    pr($result);
    die;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top