0

I use yii framework but I have a php problem here. I struggle to get datas from a loop which begins in my controller, continues in my model and ends in my controller. Here is my code :

adminController.php

public function actionAdmin()
{
////code generated by gii////
    $model=new vbnm('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['vbnm'])){
        $model->attributes=$_GET['vbnm'];
    }

////my code////
    $groupid=array();

    $dataArray = vbnm::model()->search()->getData();

    foreach($dataArray as $data){
        $groupid[] = $data->groupvb['joinvbgroup_group_id'];
    }
    $size = count($groupid);

    $values = array();

    for ($i=0; $i<$size; $i++){
        $model->get_user_name($groupid[$i]);
    }

    $this->render('admin',array(
        'model'=>$model,
    ));
}

adminModel.php

public function get_user_name($groupId)
{           
    $userid = array();

    $user = Yii::app()->db->createCommand()
    ->select('joinusergroup_user_id')
    ->from('JoinUserGroup')
    ->where('joinusergroup_group_id=:id', array(':id'=>$groupId))
    ->query();

    foreach($user as $key=>$value){
        $userid[] = $value['joinusergroup_user_id'];
    }

    $size=count($userid);

    $username = array();

    for($i=0; $i<$size; $i++){

        $name = Yii::app()->db->createCommand()
        ->select('username')
        ->from('tbl_users')
        ->where('id=:id', array(':id'=>$userid[$i]))
        ->query();

        foreach($name as $key=>$value){
            $username[] = $value['username'];
        }

        $size_n = count($username);

        $uname=array();

        for($j=$i; $j<$size_n; $j++){
            $uname[]=$username[$j];
        }
    }   
}

The code works, I get what I want when I log my $userid[$i] and my $username[$j].

My problem is : How can I return the values of $username[$j] in my controller (when my loop ends) to pass it in my view (I want to display it in a CGridView)? If I use a return it will stop my loop, right? May I have a bad use of MVC?

Thanks!

2 Answers 2

0

Yes, actually, that doesn't look like very clean MVC :-) What I would suggest you do is wrap all your logic into a Yii FormModel. You'll find it simplifies how you retrieve info for display on forms and will make saving easier (all your save logic being in one place).

FormModels can in essence handle multiple AR models and saving between them easily.

Edit: To help with improving your code, I would suggest beginning to do unit testing / thinking about how you would test this logic. You want to have relatively little logic in your controllers so that most of your apps logic is wrapped in models that you can test.

Suggested reading: http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices

http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ - Ruby on Rails, but it has good advice that can be applied to Yii

3
  • Thanks @acorncom, I will try this way. I also think it's a bad MVC practise, not yet really familiar with it^^
    – smarc
    Nov 14, 2013 at 5:55
  • @smarc updated with some thoughts on how to get better here. Would also appreciate an accept check too! :-)
    – acorncom
    Nov 14, 2013 at 11:13
  • Yes the best thing to do is probably learn more about MVC and try another approach. Thanks for the docs @acorncom!
    – smarc
    Nov 15, 2013 at 2:08
0

To pass the values to the 'admin' view, store the usernames in the $values array.

Then pass it via the render method to the view.

$this->render('admin',array(
        'model'=>$model,
        'usernames' => $values
));

// admin view
// and in the 'admin' view file you can access it like this.
print_r($usernames);
1
  • Thanks for your help @TamilSh but I have already try that. It returns me an empty array, $model->get_user_name($groupid[$i]) doesn't return anything... As if my function get_user_name() doesn't return any value to the controller.
    – smarc
    Nov 14, 2013 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.