CDetailView Error

hi…

I get problem when using CDetailView

here is my model


Class SuperAdmins extends CActiveRecord {

    ....


    public function findUsername($userId){

        if(!isset(self::$_model)){

            self::loadModel();

        }

        $Criteria = new CDbCriteria();

        $Criteria->condition = "id = '$userId'";

        $result = self::$_model->find($Criteria);

        return $result->username;

    }

}

when I try to display with CDetailView the result is still ID user not username. but when I try to set manually like this:


Class SuperAdmins extends CActiveRecord {

    ....


    public function findUsername($userId){

        if(!isset(self::$_model)){

            self::loadModel();

        }

        $Criteria = new CDbCriteria();

        $Criteria->condition = "id = '1'"; // I sett manuall id = '1' and it works

        $result = self::$_model->find($Criteria);

        return $result->username;

    }

}

here is the view




    $this->widget('zii.widgets.CDetailView', array(

        'data' => $model,

        'attributes'=>array(

                'id',

                'display_name',

                'email',

                'username',

                'created_time:datetime',

                array(

                    'name' => 'created_by',

                    'value' => SuperAdmins::findUsername($data->created_by) //  it doesn't work

                ),

                'edited_time:datetime',

                array(

                    'name' => 'edited_by',

                    'value' => SuperAdmins::findUsername($data->edited_by) //  it doesn't work

                ),

                ....

        ),

    ));



how to resolve this problem…

thanks…

function getUsername() {} ??

this is function to get username




    public function findUsername($userId){

        if(!isset(self::$_model)){

            self::loadModel();

        }

        $Criteria = new CDbCriteria();

        $Criteria->condition = "id = '$userId'"; 

        $result = self::$_model->find($Criteria);

        return $result->username;

    }



thanks for your respon sir… I have try to display this function in CDetailView, but appear CDetailView is cannot displaying other function not like CGridView. :unsure:

Dan Din Dun, try basic troubleshooting, such as a var_dump($userId); in your method, as well as var_dump($result);

Post back with your results please

hi sir, I have try to var_dump($result) it is outputing NULL

so I try to var_dump($userId); and still outputting NULL

I guess the problem is in CDetailView is don’t store the data in $data variable.

but I try to access $model->id not $data->id (it is works). confused :blink:

so


    $this->widget('zii.widgets.CDetailView', array(

        'data' => $model,

        'attributes'=>array(

                'id',

                'display_name',

                'email',

                'username',

                'created_time:datetime',

                array(

                    'name' => 'created_by',

                    'value' => SuperAdmins::findUsername($data->created_by) //  it doesn't work so I change into $model->created_by and works

                ),

                'edited_time:datetime',

                array(

                    'name' => 'edited_by',

                    'value' => SuperAdmins::findUsername($data->edited_by) //  it doesn't work so I change into $model->edited_by and works

                ),

                ....

        ),

    ));

can you explain about this sir??

thanks

public function findUsername($userId) {}

must change to:

public static function findUsername($userId) {}

ok sir, thanks… now it works :slight_smile: GREAT