Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of objects
    primarykey
    data
    text
    <p>I'm using Mongo DB and Slim PHP in my app.</p> <p>In mongo I have this array of objects </p> <pre><code>"dashboards" : [ { "_id" : ObjectId("52b96365481b9c02091d8698"), "title" : "Default title", "subtitle" : "Default subtitle", "metric_views" : [ ] }, { "_id" : ObjectId("52b9665f481b9c06091d8698"), "title" : "asdasdasd", "subtitle" : "asdasdasdas", "metric_views" : [ ] }, { "_id" : ObjectId("52b96666481b9c04091d8697"), "title" : "asdasdas", "subtitle" : "dasdasdas", "metric_views" : [ ] } ] </code></pre> <p>and for delete I wrote this function </p> <pre><code>function removeDashboard ($user,$dashboardMongoId) { foreach ($user['dashboards'] as $key =&gt; $dashboard) { if($user['dashboards'][$key]['_id']==$dashboardMongoId) { unset($user['dashboards'][$key]); break; } } return $user['dashboards']; } </code></pre> <p>after deleting one object my dashboards looks like this </p> <pre><code>"dashboards" : { "1" : { "_id" : ObjectId("52b9665f481b9c06091d8698"), "title" : "asdasdasd", "subtitle" : "asdasdasdas", "metric_views" : [ ] }, "2" : { "_id" : ObjectId("52b96666481b9c04091d8697"), "title" : "asdasdas", "subtitle" : "dasdasdas", "metric_views" : [ ] } } </code></pre> <p>[] become {} and I get number for objects "1" , "2" , but the whole app is forking fine with this . Does anyone knows why is this happening and should I fix it or its ok to stay like this ?</p> <p>Thanks !!</p> <p>UPDATE :</p> <p>Im creating default dashboard if there is no dashboards in Array maybe thats the problem , here is the code for that </p> <pre><code>$app-&gt;get('/getDashboards', function () { // Connect to database and select collection for loged in user $collection = selectMongoCollection('ctstats', 'users'); $user = findLoggedUser($collection); // Get all dashboards for user $data = $user['dashboards']; if(sizeof($user['dashboards']) == 0) { // Creating default dashboard $dashboard = array( "_id" =&gt; new MongoId(), "title" =&gt;"Default title", "subtitle" =&gt;"Default subtitle", "metric_views" =&gt; array() ); // Saving dashboard to collection array_push($user['dashboards'],$dashboard); // Updating mongo with new data $collection-&gt;update($user,$user['dashboards']); $collection-&gt;save($user); } echo json_encode($user['dashboards']); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload