1

I'm trying to get my EE add-on (using Dashee) to get entries from the DB.

So far I have

    $this->_EE->db->select('ct.channel_id, ct.title, ct.entry_date, ct.status, cd.field_id_52, cd.field_id_67, cd.field_id_73');
    $this->_EE->db->from('channel_titles AS ct, channel_data AS cd');
    $this->_EE->db->where('ct.channel_id', $settings->channel_id);
    $this->_EE->db->order_by('entry_date DESC');
    $this->_EE->db->limit($settings->limit);
    $entries = $this->_EE->db->get();

Which works, but when it outputs I get 4 entries, when really there should only be 1. But the overall total of entries on my site is 4.

What I think is happening is

$this->_EE->db->where('ct.channel_id', $settings->channel_id);

Is not selecting the correct channel, but not sure why.

Settings are set like this:

        $this->settings = array(
            'title' => 'Recent Orders',
            'channel_id' => '6',
            'limit' => '10',
            'field_firstname' => 'orders_firstname',
            'field_surname' => 'orders_surname',
            'field_total' => 'orders_total',
            'custom_url' => '10',
            'status' => 'Processing'
            );

2 Answers 2

0

I managed to fix this using 'join'

        $entries = $this->_EE->db->select('ct.entry_id AS entry_id,
                                           ct.channel_id AS channel_id, 
                                           ct.title AS title, 
                                           ct.entry_date AS entry_date, 
                                           ct.status AS status, 
                                           cd.field_id_52 AS total, 
                                           cd.field_id_67 AS first_name, 
                                           cd.field_id_73 AS surname')
                                 ->from('channel_titles ct')
                                 ->join('channel_data cd','ct.entry_id = cd.entry_id', 'inner')
                                 ->where('ct.channel_id', $channel_id)
                                 ->order_by('ct.entry_date DESC')
                                 ->limit($settings->limit)
                                 ->get();
0

Another option would be to use a proper API for this. Take a look at Channel Data. I made it so that it would solve this exact problem, as I grew tired of writing hard to maintain code, and one with hardcoded id's.

In Channel Data, you would do the following:

$entries = $this->EE->channel_data->get_channel_entries($channel_id, array(
    'where' => array(
        'channel_titles.channel_id' => $this->settings->channel_id
    ),
    'order_by' => 'entry_date'
));

This would return an Active Record object the same as the $this->EE->db->get() method.

https://github.com/objectivehtml/Channel-Data

While there is nothing wrong with other solution, Channel Data can provide a more abstract approach that can be easier to maintain in the long run if the fields ever were to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.