views:

27

answers:

1

Hi there!

I have a CI Model set up with this function and ActiveRecord:

function get_open_competitions()
{                                       
    $this->db->select('*, TO_DAYS(closingdate) - TO_DAYS(CURDATE()) AS days')
                ->from('challenges')
                ->where('closingdate >','CURDATE()')
                ->order_by('days','asc');

    $query = $this->db->get();          
    return $query;
}

I'm 99.9% confident that would run this query:

SELECT *, TO_DAYS(closingdate) - TO_DAYS(CURDATE()) AS days
FROM challenges
WHERE closingdate > CURDATE()
ORDER BY days ASC

When I put that plain query through phpMyAdmin or Sequel Pro, it returns 5 rows - as expected - from the database. However when I call in the challenges controller the following code:

    function index()
{
    // Fetch the Open for Entry competitions
    $data['open'] = $this->cm->get_open_competitions(); 
    // Fetch the Open for Voting competitions
    $data['voting'] = $this->cm->get_voting_competitions();
    // Fetch the Ended Competitions
    $data['ended'] = $this->cm->get_ended_competitions();   

    $data['colwide'] = 'challenges/challengeshome';
    $this->load->view('templatewide',$data);
}

... And then in the view file call it up like this...

<h2>Open for Entry</h2>

<hr/>

<?php foreach ($open->result() as $row) { ?>
    <h3>
        <?php echo anchor('challenges/view/'.$row->id, $row->title);?> - 
        <i>Challenge ends and voting begins in <?php echo $row->days;?> days</i>
    </h3>
    <h4> <?php echo $row->description;?> </h4>
<?php } ?>

... nothing is output!

This is confusing me, because I'm very certain I've got a valid query, and also I have two other Model functions - get_ended_competitions and get_voting_competitions - both of which I'm using alongside this which work fine. The code is definitely no different.

What am I doing wrong?! :S

Thanks!

Jack

Edit: Nothing is written to either the CodeIgniter log, or to PHP's error log.

+1  A: 

Before anything else, enable the profiler in your controller constructor:

$this->output->enable_profiler(true);

this way you'll see exactly what queries are generated.

LE: also, don't forget about $this->db->last_query(); ;)

Bogdan
Thanks for reminding me about the Profiler. Just ran it's queries through the DB - problem is, that ActiveRecord is putting 'quotes' around CURDATE() so it's treated as text instead of a MySQL function! Do you know any way to get around this?
Jack Webb-Heller
Got it. `$this->db->where()`'s third parameter, when set to FALSE, won't add backticks. Thanks Bogdan!
Jack Webb-Heller
Yeah, CI's Active Record escape mechanism is really a PIA sometimes :)
Bogdan