Reputation: 3199
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.
Upvotes: 0
Views: 612
Reputation: 30766
Are you new to PHP too?
foreach($personnel as $key => $val){
return $val;
}
This is returning only the first item. Just return $personnel;
instead.
Upvotes: 2