2015-09-25 206 views
1

我需要列表名称的字母顺序。当我占用寻呼机CakePHP时我需要做的事情,但我不能。我认为按字段顺序排列(在这个例子中是名字),所以DESC和ASC在任何情况下都不起作用。按字母顺序排列

在控制器:

$conditions = array('active !=' => -1, 'NOT' => array('role_id' => 2)); 
$users = $this->paginate('User', $conditions); 
$this->set(compact('users','filter')); 

在View:

<div class="row"> 
<table class="table table-striped table-hover table-condensed col-lg-12"> 
<thead> 
    <tr> 
     <th>#</th> 
     <th><?=$this->Paginator->sort('username', h(__('User')));?></th> 
     <th><?=$this->Paginator->sort('name');?></th> 
     <th><?=$this->Paginator->sort('email');?></th> 
     <th><?=$this->Paginator->sort('created', h(__('Registration date')));?></th> 
     <th><?=$this->Paginator->sort('active', h(__('Status')));?></th> 
    </tr> 
</thead> 
<tbody> 
    <?php 
    $offset = $this->Paginator->counter(array('format' => '{:start}')); 
    foreach($users as $user) { 
     echo '<tr onclick="window.location.href=\''.$this->Html->url(array('controller'=>'users','action'=>'view', 
       $user['User']['id'])).'\'">'. 
       '<td>'.($offset++).'</td>'. 
       '<td>'.h($user['User']['username']).'</td>'. 
       '<td>'.h($user['User']['name'].' '.$user['User']['father_surname'].' '.$user['User']['mother_surname']).'</td>'. 
       '<td>'.str_replace('@','[at]',h($user['User']['email'])).'</td>'. 
       '<td>'.$this->Time->format('d-m-Y H:i',strtotime($user['User']['created'])).'</td>'. 
       '<td>'; 
       if ($user['User']['active'] == 1) 
        echo '<span class="label label-success">'.h(__('Active')).'</span>'; 
       else 
        echo '<span class="label label-warning">'.h(__('Blocked')).'</span>'; 
       echo '</td>'. 
      '</tr>'; 
    } 
    ?> 
</tbody> 
</table> 
</div> 

回答

0

添加您的条件之后,下面

$this->User->order(
        array('User.username ASC') //use whatever field you want to sort 
        );