Riffaz Starr
Riffaz Starr

Reputation: 611

How to call a model function in view Yii?

I have a public function in model Yii:

public function test() {
    echo 'I am working';
}

Now I want to call this function on view Yii. I wolud like to go through via controller instead of directly calling that function on view Yii.

So how can I call the function on view Yii? and what I have to do with controller before I call it on view Yii?

Upvotes: 2

Views: 8969

Answers (2)

Prabowo Murti
Prabowo Murti

Reputation: 1341

I prefer to put it on your controller, like this

$model_result = MyModel::model()->test();
$this->render('view', array('model_result' => $model_result));

On your view.php

<div class="my_class" >
    <?php echo $model_result;?>
</div>

Upvotes: 5

LeleDumbo
LeleDumbo

Reputation: 9340

View should not call any functions (unless perhaps formatting helper functions), controller should pass to the view whatever values it needs, including but not limited to a result of function/method call.

Upvotes: 0

Related Questions