CodeIgniter - Jamie Rumbelow我的模型 - 按条款命令

时间:2013-10-28 11:18:43

标签: php mysql codeigniter

我在数据库城市和州有2个表。 City表有一个外键state_id。

数据库看起来像这样:

City
-----
id    city_name        state_id
--    ---------        -------- 
1     Jersey City      1
2     Philadelphia     2

State
-----
id    state_name    
--    ----------
1     New Jersey
2     Pennsylvania

这就是我的城市模型的样子:

class City_Model extends MY_Model {
    public $_table = 'city';

    public $belongs_to = array( 'state' );

    public _order_by_state() {
        $this->db->order_by('state_name', 'desc');
        return $this;
    }
}

我正在尝试使用Jamie Rumbelow的My_Model类获取所有城市和他们所属的州以州名命令。因此,我试图使用Controller类中的以下代码实现此目的:

$data['cities'] = $this->city_model->_order_by_state()->with('state')->get_all();

上面的代码给出了以下错误:

Fatal error: Call to a member function result() on a non-object in C:\xampp\htdocs\start\application\core\MY_Model.php on line 200

这是My_Model.php中的get_all方法:

public function get_all()
{
    $this->trigger('before_get');

    if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE)
    {
        $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted);
    }

    $result = $this->_database->get($this->_table)
                       ->{$this->_return_type(1)}(); //Line 200 <- Error Here
    $this->_temporary_return_type = $this->return_type;

    foreach ($result as $key => &$row)
    {
        $row = $this->trigger('after_get', $row, ($key == count($result) - 1));
    }

    $this->_with = array();
    return $result;
}

如果有人能指导我在这里使用order_by子句的正确方法,我将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:0)

public function _order_by_state() {
        $this->db->order_by('state_name', 'desc');
        return $this;
    }

并且喜欢这个

$data['cities'] = $this->city_model->_order_by_state()->get_all();

答案 1 :(得分:0)

/*  
- Controller  
- There'll be a table called states and i just want to select id (that comes auto) and the state_name, that's why i'm using dropdown()  
*/  

$st = $this->state_model->order_by('state_name')->dropdown('state_name');  
var_dump($st);