如何使用fuelphp (php)从mongodb(object)中提取数据
我正在使用 FulePHP 和 MongoDB,而且我对它们都是新手。由于某种原因,我无法在集合中提取多个 MongoDB 数据对象。当我尝试提取数据时,我的集合“人员”中有两项“人员”,但我只得到一项“人员”。我试图理解为什么。当我从模型中执行 var_dump() 时,我看到出现了两个项目。当我从控件中执行 print_r() 时,我只看到一个项目(第一个)出现。当我在模型中执行 sizeof($personnel) 时,我看到“1”,这是有道理的,因为我只有一个对象。因此,我假设我的 foreach 循环不在模型内部工作,并且只进行一次迭代。我花了两天时间试图弄清楚为什么会发生这种情况,并且我花了很多时间在 foreach 循环上,以至于我不再能够理解它们,这就是我来到这里的原因。 这是我的模型:
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;
}
}
}
这是我的控制器:
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);
}
}
不要介意 css 部分,它只是引入 css。 这是模型中的 var_dump:
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"
}
}
}
这是来自控制器的 print_r():
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也是 PHP 新手吗?
这仅返回第一项。只需
return $personnel;
即可。Are you new to PHP too?
This is returning only the first item. Just
return $personnel;
instead.