1

I am going through lithum's quick start guide and I am having an issue displaying all the posts saved to my Mongo database.

I have setup my PostsController like so:

<?php

 namespace app\controllers;

 use app\models\Posts;

class PostsController extends \lithium\action\Controller {

    public function index(){
            $posts = Posts::all();
            return compact('posts');
    }

    public function add(){
            $post = Posts::create();
            $success = false;

            if($this->request->data && $post->save($this->request->data)){
                    $success = true;
            }
            return compact('post','success');
    }
}
?>

My add view is setup like this:

  <?=$this->form->create($post); ?>
    <?=$this->form->field('title');?>
    <?=$this->form->field('body', array('type' => 'textarea'));?>
    <?=$this->form->submit('save'); ?>
 <?=$this->form->end(); ?>


 <?php if ($success): ?>
    <p>Post Successfully Saved</p>
 <?php endif; ?>

And finally my index view is setup like this:

 <?php foreach ($posts as $post): ?>
     <article>
       <h1><?= $post->title ?></h1>
       <p><?= $post->body ?></p>
     </article>
 <?php endforeach; ?>

I have 4 test posts in my post collection as you can see from this output:

 db.posts.find();
 { "_id" : ObjectId("54dd379902a6ce7b088b4567"), "title" : "First    Post", "body" : "Test First Post" }
 { "_id" : ObjectId("54dd3b7e02a6ce7b088b4568"), "title" : "Test Post 2", "body" : "Test Post 2" }
 { "_id" : ObjectId("54dd3cfe02a6ceea0b8b4567"), "title" : "Test Post 3", "body" : "Test Post 3" }
 { "_id" : ObjectId("54dd3d1e02a6cee60b8b4567"), "title" : "Test Post 4", "body" : "Test Post 4" }

However when i view my index in a web browser it only displays the first 3 and not the final post. Also when a I add new post using the add view and then view my index again I will see everything but the post that was just added.

I am fairly new to Lithum, hence the quickstart guide, so I am not sure where I should start looking for my problem. It seems to me that the Posts::all() call is not returning everything it should. Any help or suggestions would be great.

DMcP89
  • 705
  • 2
  • 10
  • 26
  • This is strange. I'm not seeing why the last post would be missing from the view. Have you tried viewing source in the html to make sure the post is not there and it's not an issue with styling or malformed html? – rmarscher Feb 16 '15 at 16:29

1 Answers1

1

I posted this as an issue to lithium's github and one of the dev's helped me out. The issue is not with lithium or with the code but with the PHP-MongoDB driver I was using.

Versions 1.6.0 and 1.6.1 are buggy version from what the dev told me, once I updated my driver the code started working as expected to check what version you drivers are run this command on your box.

  php --ri mongo

To update my drivers i ran this command:

 sudo pecl install mongo

Please note that pecl requires pear to be installed on your machine.

DMcP89
  • 705
  • 2
  • 10
  • 26