【问题标题】:How to get my ID on ARRAY from model on controller如何从控制器上的模型获取我在 ARRAY 上的 ID
【发布时间】:2017-12-05 00:44:50
【问题描述】:

我有一个案例,这个案例真的让我很困惑,因为我尝试了很多解决方案,但我仍然没有从我的模型中得到我的 ID。

这是我的情况:

  1. 在我的模型上,我从数据库中收集了一些数据

  2. 然后我将它传递给我的控制器

  3. 当然,这些数据将是一个数组(对吗?)

  4. 然后在我的控制器上,我只想获取我的 ID 用于其他目的(我将再次将它传递给我的模型,只有我的 ID)

  5. 但我对如何在控制器上提取数据感到困惑

这里按照我的方法使用我的$data['query'] 获取 ID:

$data['query']   = $this->mkomentar->get_all_($params);

$id = $data['query'];
$id_new = $id->hits->hits->_source->id;

下面是我的var_dump $id_new

下面是我的var_dump $data['query']

object(stdClass)#17 (4) { ["took"]=> int(4) ["timed_out"]=> bool(false) ["_shards"]=> object(stdClass)#18 (3) { ["total"]=> int(5) ["successful"]=> int(5) ["failed"]=> int(0) } ["hits"]=> object(stdClass)#19 (3) { ["total"]=> int(1) ["max_score"]=> float(1.6931472) ["hits"]=> array(1) { [0]=> object(stdClass)#20 (5) { ["_index"]=> string(11) "tryelastic2" ["_type"]=> string(11) "tryelastic2" ["_id"]=> string(20) "AVz4fiU3Gx0i_9jPP49l" ["_score"]=> float(1.6931472) ["_source"]=> object(stdClass)#21 (6) { ["id"]=> int(2) ["nama"]=> string(7) "Jhordan" ["url"]=> string(11) "jhordan.com" ["pesan"]=> string(5) "aiueo" ["code"]=> string(3) "247" ["status"]=> int(2) } } } } } 

【问题讨论】:

  • 用您的代码而不是您的代码图像更新您的问题。 meta.stackexchange.com/questions/22186/…
  • 好的,对不起,已经修复了@wolfgang1983
  • 用你的模型函数更新问题是这个,还有$id->hits->hits->_source->id;是一个库函数还是另一个模型函数?
  • 我的模型代码是查询 elasticsearch 先生 @wolfgang1983 的函数
  • 使用查询添加您的型号代码

标签: php mysql codeigniter elasticsearch


【解决方案1】:

通过替换它来检查您的控制器代码并检查您是否获得了 id 您可以将此 id 从控制器传递给模型

 $data['query']   = $this->mkomentar->get_all_($params);
foreach( $data['query'] as $loop) {
                 $id=$loop->id;//the id after loop-> is the column name in database
                 echo $id; //check whether id is getting
             }

【讨论】:

  • ok,谢谢@reshma,您的回答有助于解决我的问题。 ^^,
猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多