[英]CRUD with Lithium & MySQL
我剛剛開始使用PHP框架鋰(v 0.10)。
我遵循了使用MongoDB作為數據庫的快速入門手冊 。
要了解有關鋰的更多信息,我想將DBMS從MonogoDB切換到MySQL。
我遇到的問題是,當我在瀏覽器中打開/posts/
時,鋰只顯示一個空白頁面,沒有錯誤消息。 另外,當我轉到/posts/add/
,將顯示正確的格式,但是在提交數據(已正確寫入DB)之后,鋰電池也僅顯示空白頁。 怎么了
同樣,在閱讀了鋰電池模型的鋰電池文檔之后,我仍然不太確定該模型屬於哪種邏輯(在這種情況下)。
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
與行id
, title
和body
。
我在/app/models
Posts.php
模型:
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
}
?>
我在/app/controllers
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');
}
}
?>
最后是我在/app/views/posts/
視圖index.html.php
:
<?php foreach($posts as $post): ?>
<article>
<h1><?=$post->title ?></h1>
<p><?=$post->body ?></p>
</article>
<?php endforeach; ?>
還有/app/views/posts/
add.html.php
:
<?=$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)您確定鋰電池總體上運行正常嗎? 我對您的實際問題是什么感到困惑。
2)無需更改您的PostsController, Posts::all();
只是Posts::find('all');
簡寫Posts::find('all');
3)您可能需要更改您的帖子模型,Lithium(使用MySQL)將期望表中的id列用作鍵,如果您沒有名為id的coloum,則可能需要在模型中添加一個。
例如,如果您有一個表,其中包含postid , title , body , date等列,請將其添加到模型中
<?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
觀看次數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.