我在数据库中有 2 个表城市和州。城市表具有外键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 Rumunder 的 My_Model 类按州名顺序获取所有城市和它们所属的州。 因此,我尝试使用控制器类中的以下代码段来实现这一点:
$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子句的正确方法,我将不胜感激。
谢谢!
public function _order_by_state() {
$this->db->order_by('state_name', 'desc');
return $this;
}
并像这样做
$data['cities'] = $this->city_model->_order_by_state()->get_all();
/*
- 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);