dongzhun8449 2011-12-26 22:01
浏览 45
已采纳

如何使用fuelphp(php)从mongodb(对象)中提取数据

I am using FulePHP and MongoDB and I am new to both of them. For some reason I cannot pull out more than one MongoDB data object inside a collection. I have two items 'person' in my collection 'personnel' when I try to pull out data I only get one item 'person'. I am trying to understand why. When I do var_dump() from within model I see two items showing up. When I do print_r() from control I see only one item (first one) show up. When I did sizeof($personnel) in the model I saw '1', which makes sense because I have only one object. So I assume that my foreach loop is not working inside the model and only does one iteration. I spent last tow days trying to figure out why this happens, and I spent so much time with foreach loops that I no longer can understand them, that is why I came here. Here is my model:

class Model_Home extends Model {

static function get_results(){

    $mongodb = Mongo_Db::instance();
    $personnel = $mongodb->get('personnel');

    var_dump($personnel);

    foreach($personnel as $key => $val){
        return $val;
    }
  }
}

Here is my Controller:

class Controller_Home extends Controller {


public function action_index()
{
    $data['css'] = Asset::css(array('reset.css','main.css'));

    $results = Model_Home::get_results();


    print_r($results);

    foreach ($results as $key => $val){
        $data[$key] = $results[$key];
    }

    $this->response->body = View::factory('home/index', $data);
}
}

Do not mind the css part, it's just pulling in css. Here is my var_dump from within the model:

    object(stdClass)#10 (2) {
      [0]=>
      array(5) {
        ["_id"]=>
        object(MongoId)#13 (1) {
          ["$id"]=>
          string(24) "4ef82a27b238f02ed9000000"
        }
        ["cms"]=>
        array(1) {
          [0]=>
          string(8) "Druapl_1"
        }
        ["first_name"]=>
        string(6) "Name_1"
        ["last_name"]=>
        string(10) "Lst_Name_1"
        ["skills"]=>
        array(3) {
          [0]=>
          string(6) "html_1"
          [1]=>
          string(5) "css_1"
          [2]=>
          string(8) "jQuery_1"
        }
      }
      [1]=>
      array(5) {
        ["_id"]=>
        object(MongoId)#14 (1) {
          ["$id"]=>
          string(24) "4ef81a0dcf163c7da3e5c964"
        }
        ["cms"]=>
        array(1) {
          [0]=>
          string(8) "Druapl_2"
        }
        ["first_name"]=>
        string(6) "Name_2"
        ["last_name"]=>
        string(10) "Lst_Name_2"
        ["skills"]=>
        array(3) {
          [0]=>
          string(6) "html_2"
          [1]=>
          string(5) "css_2"
          [2]=>
          string(8) "jQuery_2"
        }
      }
    }

And here is my print_r() from the controller:

        Array
    (
        [_id] => MongoId Object
            (
                [$id] => 4ef82a27b238f02ed9000000
            )

        [cms] => Array
            (
                [0] => Druapl_1
            )

        [first_name] => Name_1
        [last_name] => Lst_Name_1
        [skills] => Array
            (
                [0] => html_1
                [1] => css_1
                [2] => jQuery_1
            )

    )

I do not think I need the view too, because data gets lost before it gets to the controller it seems. But let me know if I do need the view. Please help. I simply do not know what else to do... Thanks.

  • 写回答

1条回答 默认 最新

  • dougan0529 2011-12-27 14:58
    关注

    Are you new to PHP too?

    foreach($personnel as $key => $val){
        return $val;
    }
    

    This is returning only the first item. Just return $personnel; instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题