开发者

How to submit form for one model in another's controller?

开发者 https://www.devze.com 2023-03-05 17:28 出处:网络
Let\'s say I have a Posts table and a Comments table. I want my /posts/view/ page to have a form on the same page for submitting a comment, much like any typical blog. I\'m not sure where I\'m going w

Let's say I have a Posts table and a Comments table. I want my /posts/view/ page to have a form on the same page for submitting a comment, much like any typical blog. I'm not sure where I'm going wrong here, but this is what I've tried:

class PostsController extends AppController {
var $name = 'Posts';
var $uses = array('Post', 'Cmt'); 

function view($id = null) {
    ...
    if (!empty($this->data)) {
        $this->Cmt->create();
        if ($this->Cmt->save($this->data)) {
            $this->Session->setFlash(__('The cmt has been saved', true));
        } 
    }

    $this->set('post', $this->Post->read(null, $id));
}

and in the view

<?php echo $this->Form->create('Cmt');?>
<fieldset>
<?php
    echo $this->Form->input('name');
    echo $this->Form->input('email');
    echo $this->Form->input('title');
    echo $this->Form->input('content');
?>
    <div class="input select required"><label for="CmtStpageId">Post</label>
        <select id="CmtPostId" name="data[Cmt][post_id]">
            <option value="1">postname</option>
        </select>
    </div>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>

What's wrong here that won't allow a record to be posted开发者_运维技巧 to the cmts table?

Also, I have the post id hardcoded into that form, as you can see, because the select box doesn't populate with post id's for whatever reason. Any help with that would be appreciated also.


When you create a form, you can explicitly set what the form action is using the url parameter:

$this->Form->create('Cmt', array('url'=>$this->Html->url(array('controller'=>'cmts', 'action'=>'add'))));

As for the Post ID, I assume that you have a 1-to-Many relationship between posts and comments. If that's the case, you should just be able to do the following in your view: echo $this->Form->input('post_id', array('type'=>'hidden')); Then, in your view function, set $this->data['Cmt']['post_id'] = $post['Post']['id']; to get it to automatically populate.


I think is better to do it like this...

$this->Form->create('Cmt', array('url'=>array('controller'=>'cmts', 'action'=>'add')));

the option['url'] already handles arrays with the controller and action.

You'll find it here: http://book.cakephp.org/view/1384/Creating-Forms#options-url-1387


Can also use

$this->Form->create('Cmt', array( 'action' => 'add' ));

as this would take you to cmts controller and action add will be called.


If you want to submit data for Comments, it is better to handle that in the CmtsController.

Your view file in PostsController is ok

<?php echo $this->Form->create('Cmt');?>

will generate the form action "/autogenerated_cake_base_url/cmts/view" and you handle this at your CmtsController, not in your PostsController.

0

精彩评论

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

关注公众号