0

プロジェクトのビューの 1 つで、2 つのテーブルのデータを使用するテーブルを表示する必要がありますが、そのうちの 1 つはジャンクション テーブルです。

UserSkillの 2 つのテーブルがあります。この 2 つの関係は多対多であるため、ジャンクション テーブルStudentSkillsを作成しました。

このジャンクション テーブルには、通常のようにid_userid_skillだけでなく、特定の id のペアに固有のパーセンテージ値もあります。

ビューの上記のテーブルでは、SkillテーブルのNameと、Certion ユーザーのStudentSkillsジャンクション テーブルのパーセンテージを含む行を表示する必要があります。

これは私の見解です:

<?php 
    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'skills-grid',
        'dataProvider'=>$model->with('student_skills')->searchWithStudentId($id),
        'template' => '{items}{pager}',
        'cssFile'=>Yii::app()->request->baseUrl. '/themes/'. Yii::app()->theme->name.'/css/table.css',
        'htmlOptions'=>array('class'=>'datagrid', 'style'=>'width:550px;'),
        'columns'=>array(
            array(
                'name'=>Yii::t('MainTrans', 'Name'),
                'value' => '$data->name',
                                        ),
            array(
                'name'=>'percentage',
                'header' => Yii::t('MainTrans', 'Success rate %'),
                'value' => '$data->student_skills->percentage',
                 ),
            ),
    ));

?>

そして、これは私の Skill $model での私の検索と関係です:

public function relations()
{
    return array(
        //the first two relations were generated by gii
        'problems' => array(self::MANY_MANY, 'Problems', 'problem_skill(id_skill, id_problem)'),
        'users' => array(self::MANY_MANY, 'User', 'student_skills(skill_id, student_id)'),

        //this is what tired to do
        'student_skills' => array(self::HAS_MANY, 'StudentSkills', 'skill_id'),
    );
}

public function searchWithStudentId($studentId)
{
    // my custom search to find all records with certain user_id

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('name',$this->name,true);

    $criteria->with = array('student_skills');
    $criteria->together = true;

    $criteria->compare('student_skills.student_id',$studentId);

    return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'pagination'=>array(
                    'pageSize'=>25,
            ),
            'sort'=>array(
                    'defaultOrder'=>array(
                            'title'=>CSort::SORT_ASC
                    )
            ),
    ));
}

ページをレンダリングしようとすると、エラーが発生します: Trying to get property of non-object

私のコードに何か問題があることはわかっています。おそらくリレーションに関係しているのですが、何も見つかりません。

新しいテーブルを作成することなく、誰かがこの問題を解決できますか?

ありがとう

4

1 に答える 1

0

問題は、すべてのモデルに複数の1HAS_MANYがあることを意味する関係を使用していることです。の配列を取得しているので、それを使って何かをする必要があります。たとえば、配列の最初のパーセンテージを取得しますSkill StudentSkillstudent_skills

$data->student_skills[0]->percentage

または、特定のスキルの平均パーセンテージを計算します

array_sum($data->student_skills)/count($data->student_skills)
于 2014-09-19T09:45:00.953 に答える