【发布时间】:2014-11-12 12:02:20
【问题描述】:
我是 cakephp 新手,无法理解我在烘焙时做错了什么。我使用 cakephp/app 路径来烘焙我的博客,它有两个表:
帖子 Id-integer 设置为自动递增和主键 标题 身体 已创建 修改
cmets id-integer 设置为自动递增和主键 post_id 姓名 评论 创建 修改
我打算拥有的关联是帖子 hasMany cmets 和 cmets 属于帖子 所有模型都通过关联和验证成功烘焙。我的 cmets add.ctp 使用下拉列表并要求用户选择他想要评论的帖子。我希望在不询问用户的情况下自动设置帖子。下面是我在 cmetscontroller.php 中添加操作的 sn-p
添加操作
public function add() {
if ($this->request->is('post')) {
$this->Comment->create();
if ($this->Comment->save($this->request->data)) {
$this->Session->setFlash(__('The comment has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
}
}
$posts = $this->Comment->Post->find('list');
$this->set(compact('posts'));
}
add.ctp(cmets)
<div class="comments form">
<?php echo $this->Form->create('Comment'); ?>
<fieldset>
<legend><?php echo __('Add Comment'); ?></legend>
<?php
echo $this->Form->input('post_id');
echo $this->Form->input('name');
echo $this->Form->input('comment');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Comments'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Posts'), array('controller' => 'posts', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Post'), array('controller' => 'posts', 'action' => 'add')); ?> </li>
</ul>
【问题讨论】:
标签: php cakephp cakephp-bake