开发者

CakePHP: find neighbors, order on 'name' or 'order'

开发者 https://www.devze.com 2022-12-23 06:48 出处:网络
I have a list of ordered items, ordered according to the int field order. I\'m creating a gallery in CakePHP 1.2 that has a prev and next button and those should link to the previous and next item acc

I have a list of ordered items, ordered according to the int field order. I'm creating a gallery in CakePHP 1.2 that has a prev and next button and those should link to the previous and next item according to their ordering, not according to their id.

In order to get this result I've included the 'order' parameter to the find function, and populated it with 'Item.order'=>'DESC'. Still the result is an id ordered list.

My question is: what do I do wrong? My controller:

$this->Item->id = 16;

$neighbours = $this->Item->find('neighbors', array(
    'order' => array('Item.order'=>'DESC'),
    'fields' => array('id','name')
));

My Solution

I've tried a different approach. My code now does the job and looks as follows:

$order = $this->Item->findById(6);

$neighbours = $this->Item->find('neighbors', array(
    'fiel开发者_开发技巧d' => 'order',
    'value' => $order['Item']['order']
));

By setting the parameter 'field' to the field will be the ordering field, and set the 'value' parameter to the order value of you current Item you'll get the prev and next.


Yeah the problem was that you weren't including the order field in your fields array.

$neighbours = $this->Item->find('neighbors', array(
    'order' => 'order DESC',
    'fields' => array('id', 'name', 'order')
));

Unless you have related models with conflicting field names you don't need to include the Item. model prefix (though I usually do anyway to avoid such errors.) You're original syntax would work if you had included [Item.]order in "fields"

Finally, your solution is not optimal, you're making two SQL queries when you don't need to. and 'field' is not a query option as far as I'm aware which actually means you're returning all of the fields in the table.


I was having problems with this. Basically I have a list of questions that need to be randomly ordered (once only) per user session.

I was setting the order of the model to something like this:

'FIELD(TestQuestion.id, 3, 1, 5)';

This worked fine for normal queries, but finding neighbors is stymied by line 2897 in Model.php:

$query['order'] = $field . ' DESC';

So to get around it, I did the following:

  • Add a virtual field called 'examination_order', and set the order to that:

    $this->TestQuestion->virtualFields['examination_order'] = 'FIELD(TestQuestion.id, 3, 1, 5)';
    
    $this->TestQuestion->order = array(
        $this->TestQuestion->alias . '.examination_order'
    );
    
  • In the find neighbors call, set the 'field' to 'examination_order', with the value set to the index as found previously:

    $neighbors = $this->TestQuestion->find(
        'neighbors',
        array(
            'field' => 'examination_order',
            'value' => $testQuestion[$this->TestQuestion->alias]['examination_order'],
            'conditions' => $conditions
        )
    );
    
0

精彩评论

暂无评论...
验证码 换一张
取 消