【问题标题】:Facing troubles baking associations in cakephp 2.5.5.12在 cakephp 2.5.5.12 中面临烘焙关联的麻烦
【发布时间】: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


    【解决方案1】:

    我假设您想从帖子视图导航以向帖子添加评论。在这种情况下,您的按钮将具有以下形式:

     <?php echo $this->Form->create('comment', array(
            'controller' => 'comment',
            'action' => 'add',
            'type' => 'get'
        ));
        echo $this->Form->input('id', array(
            'type' => 'hidden',
            'id' => 'id',
            'value' => $this->request->data['Post']['id']
        ));
        echo $this->Form->button('New Comment', array(
            'type' => 'submit',
            'class' => 'actionButton'
        ));
        echo $this->Form->end();
    
        ?>
    

    这将通过 URL 中的 GET 传递帖子 ID,然后您可以在视图中拥有以下内容:

    <?php
                if (isset($this->request->query['id'])) {
                    echo $this->Form->input('post_id', array(
                        'default' => $this->request->query['id']
                    ));
                } else echo $this->Form->input('post_id', array(
                    'empty' => '[select]'
                ));?>
    

    这会将帖子的选项默认设置为通过 GET 发送的选项

    【讨论】:

    • 非常感谢!有效 。虽然我不得不改变 :- 'value' => $this->request->data['Post']['id'] 到 'value' => $post['Post']['id'] 。我不知道为什么它在第一个中显示了一个错误。你也能解释一下“$this->request->allowMethod('post', 'delete');”命令?
    • 那是因为您使用的是$post 变量字段而不是$this-&gt;request-&gt;data,但无论哪个有效。如果有效,请将问题标记为已回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多