0

我在数据库 City 和 State 中有 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 子句,我将不胜感激。

谢谢!

4

2 回答 2

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();
于 2014-09-09T10:03:18.533 回答
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);
于 2015-03-23T00:03:44.763 回答