带锂电池的CRUD MySQL的

时间:2011-08-09 11:21:18

标签: php mysql frameworks lithium

我刚刚开始使用PHP框架锂(v 0.10) 我跟着使用MongoDB作为数据库的the quick start manual 为了更多地了解锂,我想将DBMS从MonogoDB切换到MySQL。

当我在浏览器中打开/posts/时,我遇到的问题只显示一个没有错误消息的空白页面。此外,当我转到/posts/add/时,会显示正确的表单,但在提交数据(正确写入数据库)后,锂也只显示一个空白页。出了什么问题?

此外,在阅读了有关锂模型的锂文档之后,我仍然不太确定什么逻辑(在这种情况下)属于该模型。

UPDATE 1:

我看起来APC缓存存在问题。安装APC并重命名包含锂的文件夹后,应用程序正常运行而没有错误。当保留包含锂的文件夹名称不变时,我收到了一个缓存错误:

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

我手动设置了一个包含行postsidtitle的MySQL表body

Posts.php中的/app/models模型:

<?php
namespace app\models;

class Posts extends \lithium\data\Model {

}
?>

PostsController.php中的/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');
    }
}
?>

最后我在index.html.php中的观点/app/views/posts/

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

add.html.php中的/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; ?>

1 个答案:

答案 0 :(得分:3)

一些提示......

1)您确定锂电池一般正常运行吗?我对你的实际问题/问题感到困惑。

2)无需对PostsController进行更改,Posts::all();只是Posts::find('all');

的简写

3)您可能需要更改您的帖子模型,如果您没有名为coloum的名称,Lithium(使用MySQL)会希望表中的 id 列用作键 id 您可能需要在模型中添加一个。

例如,如果您的表格包含 postid 标题正文日期等列。 ..将此添加到您的模型

<?php
 namespace app\models;

 class Posts extends \lithium\data\Model { 
   public $_meta = array('key' => 'postid');        
 }

?>

注意'key'=&gt; 'postid'让锂知道使用 postid 作为表格的关键,而不是寻找 id

4)以下是如何在控制器中实际构建MySQL查询...

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');

}

查看Lithium手册以获取更多帮助:http://li3.me/docs/manual

<强>模型

http://li3.me/docs/manual/working-with-data/using-models.md

<强>控制器

http://li3.me/docs/manual/handling-http-requests/controllers.md

<强>视图

http://li3.me/docs/manual/handling-http-requests/views.md