Yii Framework Forum: How to pass a variable from view file to controller? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to pass a variable from view file to controller? --> how to make comments work in index view (blog tutorial) Rate Topic: -----

#1 User is offline   Majestic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 138
  • Joined: 28-March 11
  • Location:Germany, Stuttgart

Posted 14 April 2011 - 08:31 AM

Hi everybody,

I just have done the Yii Blog tutorial and now i want to show all the comments in the post/index.php (where you are able to see all posts in a list) which worked fine so far, but the thing that doesn't work is commenting. Because the form doesn't know the post id.
So my question is, how can I pass the post id to the postController? Or is there an even better solution?


Thanks a lot,
Mayjestic
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 14 April 2011 - 08:43 AM

If you are displaying the post page, you should have a $model variable with the model.

Here you have all data of the post. Use a form with an action like:

'action'=>array('post/comment', 'id'=>$model->id)


And you will got in the action comment a $_GET variable with the id of the post.
0

#3 User is offline   Majestic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 138
  • Joined: 28-March 11
  • Location:Germany, Stuttgart

Posted 16 April 2011 - 08:01 AM

Thanks for your help so far. But I couldn't solve the problem yet.
The thing is, that i got a list of "posts" (I called them "entrys") shown on the page and each can be commented.

So my _view (entry) looks as follows:
<div class="view">

    <?php echo CHtml::link(CHtml::encode($data->translations[0]->title), array('view', 'id' => $data->entry_id)); ?>, 

    <?php echo Yii::app()->locale->dateFormatter->formatDateTime($data->update_time, 'long', 'medium') ?>, von

    <?php echo $data->user->username; ?>,

    <?php if ($data->commentCount >= 1): ?>
    <?php echo $data->commentCount . ' Kommentare'; ?>
    <?php endif; ?>

    <?php echo $data->translations[0]->content; ?>
        <br />

        <div id="comments">
        <?php if (Yii::app()->user->hasFlash('commentSubmitted')): ?>
            <div class="flash-success">
            <?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
        </div>
        <?php else: ?>
        <?php
                $this->renderPartial('/comment/_form', array(
                    'model' => $data->comments,
                    'entry' => $data,
                ));
        ?>
        <?php endif; ?>

        <?php if ($data->commentCount >= 1): ?>
        <?php
                    $this->renderPartial('_comments', array(
                        'entry' => $data,
                        'comments' => $data->comments,
                        'translation' => $data->comments->translations[0],
                    )); ?>
        <?php endif; ?>
    </div><!-- comments -->

</div>


The called _form (entry) looks like that:
<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
                'id' => 'comment-form',
                'enableAjaxValidation' => true,
            ));
    ?>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo '<p style="font-size:18px; color:#000;">Wie sehen Sie das? </p>' ?>
        <?php echo $form->textArea(Translation::model(), 'content', array('rows' => 1, 'cols' => 50)); ?>
        <?php echo $form->error(Translation::model(), 'content'); ?>
    </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>


    <?php $this->endWidget(); ?>

</div><!-- form -->


And the _comments (entry) looks like that:
<?php foreach ($comments as $comment): ?>
    <div class="comment" id="c<?php echo $comment->comment_id; ?>">

    <div class="content">
        <span>
        <?php echo nl2br(CHtml::encode($comment->translations[0]->content)); ?>
        </span>
    </div>

    <div class="author">
        von <?php
        if ($comment->user->username == null) {
            echo $comment->authorLink;
        } else {
            echo $comment->user->username;
        }
        ?>,
        <?php echo Yii::app()->locale->dateFormatter->formatDateTime($comment->create_time, 'long', 'medium') ?>
    </div>

    <div class="time">
    </div>
        <br/>

</div><!-- comment -->
<?php endforeach; ?>


In the EntryController I thought of making the following call in the actionIndex Method:
$comment = $this->newComment($this->loadModel());


The newComment function needs an entry model object...

And now the newComment function in EntryController should receive the correct entry_id (belonging to the posted comment):
public function loadModel()
    {
        if ($this->_model === null) {
            if (isset($_GET['id'])) {
                if (Yii::app()->user->isGuest)
                    $condition = 'status=' . Entry::STATUS_PUBLISHED
                            . ' OR status=' . Entry::STATUS_ARCHIVED;
                else
                    $condition='';
                $this->_model = Entry::model()->with('translations')->findByPk($_GET['id'], $condition);
            }
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }



Just cant figure out how to solve this ... there must be an easy solution but i've now tried so much, that Im even more confused :-D. still learning Yii ;-)

Thanks in advance,
Mayjestic
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users