2014-12-02 42 views
-1

表單元(id,unitNo,summary)。在find()中排序cakephp

id | unitNo
1 | 23
2 | 25
3 | 22

在UnitsController:

public function index() { 
    $this->set('units', $this->Unit->find('all'), array(
     'order' => array('Unit.unitNo ASC'), 
     'fields' => array('Unit.id', 'Unit.unitNo')) 
    ); 
} 

index.ctp

<table> 
    <tr> 
     <th>Unit</th> 
     <th>Summary</th> 
    </tr> 
    <!-- Here is where we loop through our $posts array, printing out post info --> 
    <?php foreach ($units as $unit): ?> 
     <tr> 
      <td> 
       <?php echo $this->Html->link($unit['Unit']['unitNo'], array('controller' => 'units', 'action' => 'view', $unit['Unit']['id'])); 
       ?> 
      </td> 
      <td><?php echo $unit['Unit']['summary']; ?></td> 
     </tr> 
    <?php endforeach; ?> 
    <?php unset($unit); ?> 
</table> 

我想使用順序unitNo但結果仍是按id訂購。 http://s1104.photobucket.com/user/thai92hp/media/Untitled.png.html

任何人都可以幫助我解決這個問題嗎?

回答

1

請這給一試:

public function index() { 
    $this->set('units', $this->Unit->find('all', 
     array(
      'order' => array('Unit.unitNo ASC'), 
      'fields' => array('Unit.id', 'Unit.unitNo') 
     ) 
    )); 
} 

在我看來,你已經給您的訂單設置$this->set()作爲第三個參數,但你可能想要做的就是給它$this->Unit->find()作爲第二個參數。

+0

哇!你是對的!非常感謝! <3 – 2014-12-02 06:32:25

+0

很高興我能幫助,歡呼! – kyo 2014-12-02 06:43:43