Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Order_by doesnt work
  • No matter how I put it the order_by on my ORM model doesn't work... I always have the same ordering? What am I doing wrong here?
    $data['articles'] = Model_Article::find('all', array('order_by' => array('created_at' => 'desc')));
    
  • I'm having the same issue with the ORM. Given the following code:
      $location = Model_Location::find('first', array(
          'related' => array(
              'items' => array(
                  'order_by' => array('name' => 'asc')
              ),
          ),
      ));
    

    The results are returned as expected, however, the order is id ascending. Did you find a resolve to your problem?
  • Neither this will work, I always get the same ordering??
    $data['articles'] = Model_Article::find()->order_by('created_at', 'desc')->get();
    
  • Could you try echo Model_Article::find()->order_by('created_at', 'desc')->get_query(); and show me the resulting query.
  • Jelmer Schreuder wrote on Sunday 26th of February 2012:
    Could you try echo Model_Article::find()->order_by('created_at', 'desc')->get_query(); and show me the resulting query.

    SELECT `t0`.`id` AS `t0_c0`, `t0`.`category_id` AS `t0_c1`, `t0`.`user_id` AS `t0_c2`, `t0`.`author` AS `t0_c3`, `t0`.`status` AS `t0_c4`, `t0`.`allow_comments` AS `t0_c5`, `t0`.`uri` AS `t0_c6`, `t0`.`title` AS `t0_c7`, `t0`.`summary` AS `t0_c8`, `t0`.`body` AS `t0_c9`, `t0`.`tags` AS `t0_c10`, `t0`.`view_count` AS `t0_c11`, `t0`.`videos` AS `t0_c12`, `t0`.`gallery_id` AS `t0_c13`, `t0`.`published_at` AS `t0_c14`, `t0`.`created_at` AS `t0_c15`, `t0`.`updated_at` AS `t0_c16` FROM `articles` AS `t0` ORDER BY `t0`.`created_at` DESC, `t0`.`created_at` DESC SQL statement seems OK. But when looping trough returning record set with foreach ($articles as $article) the order is not ok ...
  • Aren't you doing something of your own after this and before the foreach that may change the order? The ORM doesn't do any reordering, it just works through the returned data in the order that it's been given.
  • Jelmer Schreuder wrote on Sunday 26th of February 2012:
    Aren't you doing something of your own after this and before the foreach that may change the order? The ORM doesn't do any reordering, it just works through the returned data in the order that it's been given.

    Nothing special... Here is my controller code:
     public function action_index()
     {
      $data['articles'] = Model_Article::find()->order_by('created_at', 'desc')->get();
      $this->template->content = View::forge('admin/articles/index', $data);
    
     }
    

    and my view
    <?php if ($articles): ?>
    <table id="table-articles" class="striped">
     <thead>
      <tr>
       <th class="header">#</th>
       <th class="yellow header">Title</th>
       <th class="orange header">Category</th>
       <th class="blue header">Status</th>
       <th class="green header">Created at</th>
       <th class="green header">Published at</th>
       <th class="header">Actions</th>
      </tr>
     </thead>
     <tbody>
    <?php foreach ($articles as $article): ?>  
      <tr>
       <td><?php echo $article->id; ?></td>
       <td><?php echo $article->title; ?></td>
       <td><span class="<?php echo $article->category->fontclass ?>"><?php echo $article->category->name; ?></span></td>
       <td>
        <?php 
        echo $article->status->name;
        ?>
       </td>
       <td><?php echo date('d.m.Y', $article->created_at) ?></td>
       <td><?php echo date('d.m.Y', $article->published_at) ?></td>
       <td nowrap>
        <?php echo Html::anchor('articles/preview/'.$article->id, 'View', array('target'=>'_blank')); ?> |
        <?php echo Html::anchor('admin/articles/edit/'.$article->id, 'Edit'); ?> |
        <?php echo Html::anchor('admin/articles/delete/'.$article->id, 'Delete', array('onclick' => "return confirm('Are you sure?')")); ?>
    
       </td>
      </tr>
    <?php endforeach; ?> 
     </tbody>
    </table>
    
    <?php else: ?>
    <p>No articles.</p>
    <?php endif; ?>
    
  • My issue was actually javascript related and me being stupid :). I needed two days to figure out. I was testing some JS-based table sorter way before that and totally forgot I had it linked to this view :S. After removing the order worked as expected for me. In your case... can you echo out your query to see the actual SQL produced?
    echo Model_Location::find('first', 
                    array( 'related' => array( 
                          'items' => array( 'order_by' => array('name' => 'asc') ), ), ))->get_query()
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion