Я изучаю CakePHP, пытаясь создать приложение для заметок.Тем не менее, у меня есть две досадные ошибки, которые мешали мне идти дальше и иметь возможность завершить свое приложение.
РЕДАКТИРОВАТЬ: вы можете увидеть демо на: u: username / 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')); ?>
Кто-нибудь знает, в чем здесь проблема?