【问题标题】:Can't delete posts and can't add a post without an image无法删除帖子,也无法添加没有图片的帖子
【发布时间】:2011-07-02 06:35:25
【问题描述】:

我正在通过尝试创建一个笔记应用程序来学习 CakePHP。但是,我有两个烦人的错误阻止了我继续前进并无法完成我的应用程序。

编辑:您可以在以下位置查看演示:http://jeeremie.com (u: 用户名 / 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')); ?>

有谁知道这里的问题是什么?

【问题讨论】:

    标签: cakephp-1.3


    【解决方案1】:

    看来你没有保存权利。 您添加功能设置闪烁消息,如果:

    $this->Note->save($this->data['Note']) == true
    

    我不明白你为什么要那样保存......

    为什么不将用户输入的所有信息保存在这样的表单中:

    if($this->Note->save($this->data)){
    if success code goes here...
    }
    

    这将获取输入的所有信息并保存。 另外,你似乎错过了这个:

    $this->Note->create();
    

    您是自己烘焙应用程序还是自己编写代码?

    【讨论】:

    • 非常感谢您的帮助。正如我所说,我正在学习。我自己编码。在 Windows 上使用控制台不是很好。我可以切换到 Linux,但不能使用 Adob​​e 产品。此外,我现在不确定完全理解这个烘焙的东西是如何工作的,所以我现在更愿意跳过它。无论如何,您给我的代码解决了删除按钮的问题,但当我保存没有文件的帖子时却没有。 “$this->Note->create();”去哪儿了?在 IF 语句中?
    • 其实我的笔记还是删不掉。它仅适用于 2-3 cmets,而不适用于所有人。我将尝试将文件放在服务器上,以便您自己尝试。坚持,稍等。这需要一段时间。
    • 好的,我已经把文件移到服务器了:jeeremie.com(u:用户名/p:demo123——如果你愿意,你也可以创建一个新帐户)。
    • 现在测试一下...顺便说一句,windows上的CakePHP控制台非常好用,我也是在windows环境下开发的,原因和你说的一样,而且控制台工作得很好为您完成 90% 的工作......
    • 是的,我必须阅读更多相关信息。听起来很有趣。基本上,当使用($ cake bake)时,它会为你生成代码(添加,查看,编辑,删除......),对吧?
    【解决方案2】:

    首先,这里有一个链接向您展示如何将 cakePHP(cake shell 命令)添加到您的 Windows 路径,这将让您可以在任何地方使用它。 首先将一个新的 cakePHP 安装在某个地方,这样您就可以始终在一个固定的地方安装它。 比去这里在 Windows 路径中设置蛋糕: Setting Windows path

    除此之外,基本的 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. 学会用贝壳做蛋糕——它会改变你的生活!

    1. 试试我上面列出的内容。

    【讨论】:

    • 谢谢。我已经在 Windows 上设置了 cakephp 控制台,虽然有一些错误,但我稍后会修复。我现在手动做所有事情,所以我可以更多地学习 CakePHP 和 PHP。我是 UI 设计师,而不是后端开发人员 :) 无论如何,上面的代码并没有改变任何东西。感谢您的帮助
    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多