I just started to play with the PHP framework lithium (v 0.10).
I followed the quick start manual which uses MongoDB as database.
To learn a bit more about lithium, I wanted to switch the DBMS from MonogoDB to MySQL.
The problem I'm having that when I open /posts/
in the browser lithium only shows a blank page with no error messages. Also, when I go to /posts/add/
, the correct form is displayed, but after submitting the data (which is correctly written to the DB), lithium also just displays a blank page. What's going wrong?
Also, after reading the lithium docs on models in lithium, I'm still not quite sure what logic (in this case) belongs to the model.
UPDATE 1:
I looks like it there is a problem with the APC caching. After installing APC and renaming the folder containing the lithium, the application worked without an error. When leaving the name of the folder containing lithium unchanged, I got an cache error:
Warning: include(/var/www/web/frameworks/lithium/app/resources/tmp/cache/templates/template_views_layouts_default.html_886_1308416958_798.php) [function.include]: failed to open stream: No such file or directory in /var/www/web/frameworks/lithium/libraries/lithium/template/view/adapter/File.php on line 111
Warning: include() [function.include]: Failed opening '/var/www/web/frameworks/lithium/app/resources/tmp/cache/templates/template_views_layouts_default.html_886_1308416958_798.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/web/frameworks/lithium/libraries/lithium/template/view/adapter/File.php on line 111
END UPDATE 1
I manually set a up a MySQL table posts
with the rows id
, title
and body
.
My Posts.php
model in /app/models
:
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
}
?>
My PostsController.php
controller in /app/controllers
:
<?php
namespace app\controllers;
use app\models\Posts;
class PostsController extends \lithium\action\Controller {
public function index() {
$posts = Posts::all();
return compact('posts');
var_dump($posts);
}
public function add() {
if($this->request->data) {
$post = Posts::create($this->request->data);
$success = $post->save();
}
return compact('success');
}
}
?>
And finally my views index.html.php
in /app/views/posts/
:
<?php foreach($posts as $post): ?>
<article>
<h1><?=$post->title ?></h1>
<p><?=$post->body ?></p>
</article>
<?php endforeach; ?>
And also add.html.php
in /app/views/posts/
:
<?=$this->form->create(); ?>
<?=$this->form->field('title');?>
<?=$this->form->field('body', array('type' => 'textarea'));?>
<?=$this->form->submit('Add Post'); ?>
<?=$this->form->end(); ?>
<?php if ($success): ?>
<p>Post Successfully Saved</p>
<?php endif; ?>
A couple of tips ...
1) Are you sure Lithium is running correctly in general? I'm confused as to what your actual question / problem is.
2) There is no need to make the change to your PostsController, Posts::all();
is just shorthand for Posts::find('all');
3) You may need to change your Model for Posts, Lithium (with MySQL) will expect an id column in your table to use as a key, if you don't have a coloum named id you might need to add one to your model.
For example, if you have a table with columns like postid, title, body, date ... add this to your model
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
public $_meta = array('key' => 'postid');
}
?>
Note the 'key' => 'postid' that will let lithium know to use postid as the key for the table instead of looking for id
4) Here's how to actually construct a MySQL query in your controller ...
public function locations($companyid,$state=null) {
/* removes null or false values with array_filter() */
$conditions = array_filter(array('companyid' => $companyid, 'state' => $state));
/* pass $conditions array to the Locations model, find all, order by city */
$locations = Locations::find('all', array(
'conditions' => $conditions,
'order' => array('city' => 'ASC')
));
return compact('locations');
}
Check out the Lithium Manual for more help: http://li3.me/docs/manual
Models
http://li3.me/docs/manual/working-with-data/using-models.md
Controllers
http://li3.me/docs/manual/handling-http-requests/controllers.md
Views