开发者

Can't delete posts and can't add a post without an image

I am learning CakePHP by trying to create a note application. However, I have two annoying bugs which have stopped me from going further and being able to complete my app.

EDIT: you can see a demo at: http://jeeremie.com (u: username / p: demo123 - you can also create a new account if you want).

I added an option to upload a picture to my notes. The uploader works just fine however if I don't upload a picture, CakePHP tells me the post has been added successfully though I don't see my new note, neither on the site or database. The code for the ADD view is:

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

Second, I can't delete any post though, again, it tells me is has been deleted successfully but the post still remains. Below the code for the controller:

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


}
?>

And last, the notes view:

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

Does anyone know what the problem is here?


It seems like your not saving right. You Add function set the flash message if:

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

I don't understand why your trying to save that way...

Why not save all the information the user input in the form like this:

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

This will take all the information that was input and save it. Also, you seem to be missing this:

$this->Note->create();

Did you bake your application or did you just code it all yourself?


First off, here is a link that shows you how to add cakePHP (the cake shell command) to you windows path, this will let you use it from anywhere. First place a fresh cakePHP installation somewhere so that you can always have this in a set place. Than go here and set cake in windows path: Setting Windows path

Aside from that, this is what the Basic add method is supposed to look like:

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

This is what the basic delete method looks like:

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

So my recommendation: 1. learn to use cake from the shell - it will change you life!

  1. try out what I listed above.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜