請幫助我的CI4。
這是 CI4-> ItemsCatModel.php 中的模型
public function get_items_cat() {
$builder = $this->table('items_category');
$query = $this->table('items_category')->orderBy('items_cat_id', 'asc')->getWhere($this->table, array(['items_flag' => 1], ['items_cat_id !=' => 6]));
return $query->getResultArray();
}
public function get_items_cat_bi($id) {
$query = $this->query("SELECT * FROM items_category WHERE items_cat_flag = 1 AND items_cat_id <> ' . $id . ' ORDER BY items_cat_id ASC");
return $query->getResultArray();
}
當我使用 get_items_cat 這是指向array()的錯誤 傳遞給 CodeIgniter\Database\BaseBuilder::getWhere() 的引數 2 必須是 int 或 null 型別,給定陣列
我也嘗試過這個查詢..問題->與上述查詢相同嗎?還是不同?
$query = $this->query("SELECT * FROM items_category WHERE items_cat_flag = 1 AND items_cat_id <> 6 ORDER BY items_cat_id ASC");
如果答案是肯定的,那么上面的查詢運行良好。
但是如果我使用上面的查詢,當我使用我的控制器運行時它會失敗。
這是控制器-> Home.php
public function __construct() {
$this->bannersModel = new BannersModel();
$this->itemsModel = new ItemsModel();
$this->itemsCatModel = new ItemsCatModel();
}
public function index() {
$data['items'] = $this->itemsModel->get_items();
$data['items_lain'] = $this->itemsModel->get_items_lain();
$i = 0;
foreach ($data['item'] as $key => $value) {
$category = $this->itemsCatModel->get_items_cat_bi($value['items_cat_id']);
$data['items'][$i]['category'] = $category;
}
$i ;
}
并且錯誤就像這樣
不能使用stdClass型別的物件作為陣列
,這是錯誤開始
$category = $this->itemsCatModel->get_items_cat_bi($value['items_cat_id']);
請幫助我解決問題。提前謝謝你。
uj5u.com熱心網友回復:
解釋:
第1部分:
\CodeIgniter\Model所有用戶空間模型擴展的類沒有內置table(...)方法。因此,您的自定義get_items_cat()方法中的以下代碼行應該會引發錯誤。
$builder = $this->table('items_category');
我想,你打算$builder = $this->db->table('items_category');改用。
第2部分:
$builder->getWhere()
- 與該
get()方法相同,只是它允許您在第一個引數中添加“where”子句,而不是使用該$builder->where()方法:
getWhere
- 允許直接添加 where 子句、limit 和 offset。
public function getWhere(
$where = null,
?int $limit = null,
?int $offset = 0,
bool $reset = true
)
根據上面的參考資料,正在討論的方法期望 a $limitwhich is an intor null。然而你卻輸入了一個陣列(array(['items_flag' => 1], ['items_cat_id !=' => 6]))。
解決方案:
不要在第二個引數中傳遞WHERE條件,而是在第一個引數中這樣做。IE:
// ...
$query = $this->db
->table('items_category')
->orderBy('items_cat_id', 'ASC')
->getWhere(
[
['items_cat_flag =' => 1],
['items_cat_id !=' => 6]
]
);
// ...
附錄
看起來您的自定義方法中的foreach回圈也是錯誤的。index()
public function index() {
$data['items'] = $this->itemsModel->get_items();
// ...
$i = 0;
foreach ($data['item'] as $key => $value) {
// ...
}
$i ;
}
我非常懷疑您打算將代碼行$i ;放在foreach回圈內。否則,$i將始終設定為0。
其次,您傾向于回圈,$data['item']但您的初始宣告指的是$data['items']. 注意“item”這個詞的復數形式的不同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487739.html
