【问题标题】:CodeIgniter - Jamie Rumbelow My Model - Order By ClauseCodeIgniter - Jamie Rumbelow 我的模型 - 按条款排序
【发布时间】:2013-10-28 11:18:43
【问题描述】:

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

谢谢!

【问题讨论】:

  • 这是 MY_Model.php 上的第 200 行,请...指定
  • 我从 My_Model.php 添加了 get_all 方法。请参阅代码中标记的第 200 行。
  • 我认为费城在宾夕法尼亚
  • 是的,我已经更正了。
  • 您是否为错误打开了堆栈跟踪?我在 erors:error_db 中放了一个 debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)。发现它有很大帮助。见pastiebin.com/526e55fc41807

标签: php mysql codeigniter


【解决方案1】:
public function _order_by_state() {
        $this->db->order_by('state_name', 'desc');
        return $this;
    }

然后这样做

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

【讨论】:

    【解决方案2】:
    /*  
    - 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2013-02-17
      • 1970-01-01
      • 2020-05-06
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多