0

URL '/localhost/topp/events/view/14' の現在のイベント ID '14' をコメント テーブルの event_id フィールドに保存しようとしています。コメント コントローラーで追加機能を使用し、イベント view.ctp ページで [コメントを追加] ボタンを使用しています。何か案は?非表示の id フィールドと $this->params['pass']['0'] を使用するようにアドバイスされましたが、これについてどうすればよいかわかりません。どんなアドバイスでも大歓迎です。前もって感謝します。

私の現在のコメントコントローラーはアクションコードを追加します:

public function add() {
        //$this->loadModel('Event');
        if ($this->request->is('post')) {
            $this->Comment->create();
            $this->request->data['Comment']['user_id'] = $this->Auth->user('id');
                    //Commented below is the logic Iam trying to achieve but is wrong
            //$this->request->data['Comment']['event_id'] = $this->request->data['Event']['id'];
            if ($this->Comment->save($this->request->data)) {
                $this->Session->setFlash(__('The comment has been saved'));
                $this->redirect(array('controller' => 'events', 'action' => 'myevents'));
            } else {
                $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
            }
        }
    }

////////////////////////////////////// マイイベント コメントリンクを追加してコードを表示// ////////////////////////

<div class="events view">
    <?php echo $this->element('maintitle'); ?>
    <?php echo $this->element('profile_area'); ?>
<h2><?php  echo __('Event'); ?></h2>
    <dl>        
        <td><?php echo $this->Html->image('/files/event/photo/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <td><?php echo $this->Html->image('/files/event/photo2/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo2'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <td><?php echo $this->Html->image('/files/event/photo3/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo3'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <td><?php echo $this->Html->image('/files/event/photo4/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo4'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <dt></dt>
        <dd>
        <br>
        </dd>
        <dt><?php echo __('Name'); ?></dt>
        <dd>
            <?php echo h($event['Event']['name']); ?>
            &nbsp;
        </dd>
        <dt><?php echo __('Date'); ?></dt>
        <dd>
            <?php echo h($event['Event']['date']); ?>
            &nbsp;
        </dd>


    </dl>
</div>
<div class="related">
    <h3><?php echo __('Related Comments'); ?></h3>
    <?php if (!empty($event['Comment'])): ?>

    <?php echo ('Add a comment for this event') ?>
    <?php
        $i = 0;
        foreach ($event['Comment'] as $comment): ?>

        <tr>
            <td><?php echo $comment['comment']; ?></td>

            <td class="actions">

            </td>
        </tr>
    <?php endforeach; ?>

<?php endif; ?>

    <div class="actions">
        <ul>
            <li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
        </ul>
    </div>
</div>
4

1 に答える 1

1

これが正しいかどうか見てみましょう。

  1. 投稿したフォームはイベントビューにあります
  2. リンク「新しいコメント」は、コメントコントローラーの別のビュー「追加」にリダイレクトします
  3. この追加ビューには、実際にコメントを書き込むためのフォームがあります (この部分を推測しています。そのコードをここに入れていません)。
  4. add 関数に、これがどこから来たのかからの event_id を持たせたいとします。

右?

次に、イベントの ID を event_view.ctp (ここで名前を考えています) から comment_add.ctp に渡す必要があります。

だからそれを行う2つの方法

最初の方法:リンクでイベントの ID を渡し、このようなアクションでそれを受け取ります

//event_view.ctp
//I'm going to assume you know the event id here
$this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add', $event_id)); ?>

これにより、たとえばcomments/add/14のようなリンクが作成されます。ここで、14 はイベント ID です。アクションでは、IDを受け取ります

//pass it like parameter of the action
public function add($event_id = null) {
    if ($this->request->is('post')) {
        $this->Comment->create();
        $this->request->data['Comment']['user_id'] = $this->Auth->user('id');

        //check that the event_id isn't null and that the event actually exists first
        $this->request->data['Comment']['event_id'] = $event_id;
        /*etc*/
    }
}

event_id を comment_add ビューに渡す必要はありません。ユーザーが処理する必要がない場合は、フォームに追加する必要はありません。

2 番目の方法: URL 内で event_id を渡したくない場合は、POST で渡す必要があります。そのためには、新しいコメントを追加する必要があるリンクを、event_id が非表示のフォームに変更する必要があります。

echo $this->Form->create('Comment', array('url'=>array('controller' => 'comments', 'action' => 'add')));
echo $this->Form->input('event_id', array('type'=>'hidden', 'value'=>$event_id);
echo $this->Form->end('New comment');

//instead of <?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?>

コメントのアクションで、たとえば、受信した投稿に event_id が含まれているが user_id が含まれていないかどうかを確認します。そのアクションに対して受け取るすべてのリクエストが投稿になるため、この方法は少し面倒だと思います。そのため、入力するフォームをいつ表示したいのか、いつ保存したいのかを区別するのが難しくなります。

私は最初の方法の方が好きで、それをお勧めします。しかし、それを行う他の方法があることを指摘する必要があると感じました. あなたが望むものに依存します。

于 2013-06-20T14:27:04.940 に答える