我正在通过尝试创建一个笔记应用程序来学习CakePHP。但是,我有两个烦人的错误阻止我进一步完成我的应用程序。
编辑:您可以在以下位置看到演示:http://jeeremie.com(你的 用户名 / p: demo123 - 如果需要,您还可以创建一个新帐户)。
我添加了一个选项,可以将图片上传到我的笔记中。上传工作正常,但是如果我不上传图片,CakePHP告诉我帖子已经成功添加,虽然我没有看到我的新笔记,无论是在网站还是数据库。 ADD
视图的代码是:
<!-- File: /app/views/notes/add.ctp -->
<h2>Add Note</h2>
<hr />
<?php
echo $form->create('Note',array('type'=>'file'));
echo $form->input('title', array( 'label' => 'Enter Title:' ));
echo $form->input('body', array( 'label' => 'Enter your note:' ), array('rows' => '10'));
echo $form->input('file',array('type' => 'file'), array( 'label' => 'Attach a file:' ));
echo $form->input('id', array('type'=>'hidden'));
echo '<hr />';
echo $form->end('Save Note');
?>
其次,我不能删除任何帖子,但它再次告诉我已被删除成功但帖子仍然存在。在控制器的代码下面:
<!-- File: /app/controllers/notes_controller.php -->
<?php
class NotesController extends AppController {
var $name = 'Notes';
var $components = array('Auth', 'Session');
var $paginate = array(
'limit' => 3,
'order' => array('Note.modified' => 'desc')
);
function index() {
$condition = array('user_id'=>$this->Auth->user('id'));
$this->set('notes', $this->paginate('Note', $condition));
$this->helpers['Paginator'] = array('ajax' => 'Ajax');
//$this->set('notes', $this->Note->find('all'));
//$this->Note->recursive = 0;
}
function note($id = null) {
$this->Note->id = $id;
$this->set('note', $this->Note->read());
}
function add() {
$this->layout = 'page';
if (!empty($this->data)) {
$this->data['Note']['user_id'] = $this->Auth->user('id');
if ($this->Note->save($this->data['Note']) == true) {
$this->Session->setFlash('The note has been saved', 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Oops! The note could not be saved. Please, try again.', 'flash_error');
}
}
}
function delete($id) {
$this->Note->delete($id);
$this->Session->setFlash('The note with id: '.$id.' has been deleted.', 'flash_success');
$this->redirect(array('action'=>'index'));
}
function edit($id = null) {
$this->Note->id = $id;
if (empty($this->data)) {
$this->data = $this->Note->read();
} else {
if ($this->Note->save($this->data)) {
$this->Session->setFlash('Your note has been updated.', 'flash_success');
$this->redirect(array('action' => 'index'));
}
}
}
}
?>
最后,笔记视图:
<!-- File: /app/views/notes/index.ctp -->
<?php echo $html->link('Add Post',array('controller' => 'notes', 'action' => 'add'), array('class' => 'add'))?>
<ul id="myNotes">
<?php foreach ($notes as $note): ?>
<li>
<div class="thb">
<?php
//Build the img url
$base_url = $this->base;
$imgUrl = $base_url . '/app/webroot/media/' . $note['Note']['dirname'] . '/' . $note['Note']['basename'];
//display the thumb
if(!empty ($note['Note']['file'])) {
echo '<img class="thb" src="' . $imgUrl . '" alt="' . $note['Note']['title'] .'" />';
}
?>
</div>
<h2><?php echo $html->link($text->truncate($note['Note']['title'], 40), array('controller' => 'notes', 'action' => 'note', $note['Note']['id'])); ?>
<span><?php echo $time->relativeTime($note['Note']['modified']); ?></span></h2>
<?php /* Edit/Delete Button */ echo $html->link('Edit', array('action'=>'edit', $note['Note']['id'])) . ' | ' . $html->link('Delete', array('action' => 'delete', $note['Note']['id']), null, 'Are you sure?' ); ?>
<p>
<?php
$note = $note['Note']['body'];
$noteUrls = $text->autoLink($note);
echo $text->truncate($note, 120);
?>
</p>
</li>
<?php endforeach; ?>
</ul>
<!-- Shows the page numbers -->
<?php echo $paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $paginator->numbers(); ?>
<?php echo $paginator->counter(); ?>
<?php echo $paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
有谁知道这里的问题是什么?
答案 0 :(得分:0)
好像你没有保存正确。 添加功能设置flash消息,如果:
$this->Note->save($this->data['Note']) == true
我不明白你为什么要这样保存......
为什么不保存用户在表格中输入的所有信息,如下所示:
if($this->Note->save($this->data)){
if success code goes here...
}
这将获取输入的所有信息并保存。 此外,你似乎错过了这个:
$this->Note->create();
您是否烘焙过您的应用程序,或者您只是自己编写代码?
答案 1 :(得分:0)
首先,这里有一个链接,向您展示如何将cakePHP(蛋糕shell命令)添加到您的Windows路径,这将允许您从任何地方使用它。 首先在某处放置一个新鲜的cakePHP安装,这样你就可以在一个固定的地方放置它。 比到这里并在Windows路径中设置蛋糕: Setting Windows path
除此之外,这就是Basic add方法应该是这样的:
function add() {
if (!empty($this->data)) {
$this->Article->create();
if ($this->Article->save($this->data)) {
$this->Session->setFlash(__('The article has been saved ', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.', true));
}
这是基本删除方法的样子:
function admin_delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for article', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Article->delete($id)) {
$this->Session->setFlash(__('Article deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Article was not deleted', true));
$this->redirect(array('action' => 'index'));
}
所以我的建议: 1.学会用贝壳中的蛋糕 - 它会改变你的生活!