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.