Friday, July 1, 2011

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 stops me from going further.

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



  2. 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? Thanks in advance.

Jeremie


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: