0

我刚开始使用 PHP 框架锂 (v 0.10)。
我遵循了使用 MongoDB 作为数据库的快速入门手册。
为了进一步了解锂,我想将 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

我手动设置了一个 MySQL 表,posts其中包含和。idtitlebody

我的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; ?>
4

1 回答 1

3

几个提示...

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

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

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

例如,如果您有一个包含postidtitlebodydate等列的表......将其添加到您的模型中

<?php
 namespace app\models;

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

?>

注意 'key' => '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');

}

查看锂手册以获得更多帮助: 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

于 2011-08-12T08:00:07.890 回答