Domanda

The Relation:

public function relations() {
    return array(
        'submissionStatus' => array(self::HAS_ONE, 'SubmissionStatus', 'submission_status_id'),
    );
}

Column value in GridView:

'value' => '$data->submissionStatus->submission_status_name',

SubmissionStatus Model Attributes:

public function attributeLabels() {
    return array(
        'submission_status_id'   => 'Status ID',
        'submission_status_name' => 'Status Name',
    );
}

SubmissionStatus Relations:

public function relations() {
    return array(
        'submissionStatus' => array( self::BELONGS_TO, 'Submission', 'submission_status_id' ),
    );
}

Yet I get an error:

Trying to get property of non-object

Why can I not retrieve submission_status_name by this relation?

EDIT:

This on the other hand works perfectly:

'submissionStatus' => array(self::BELONGS_TO, 'SubmissionStatus', 'submission_status')

DATABASE:

| tbl_submission                  |
-----------------------------------
| submission_id (int) PK          |
| submission_name (varchar)       |
| submission_status_id (int) FK   | 


| tbl_submission_status            |
------------------------------------
| submission_status_id (int) PK    |
| submission_status_name (varchar) |
È stato utile?

Soluzione

There to 2 questions here.

  1. if your relations really works
  2. hot to get the data in gridview

I think you have an error in the realtion. The difference between BELONGS_TO and HAS_ONE is, that it tries to create the realtion in a different way. Could you post your Database tables, what fields they have, we need to see what is the key and foreign jey fields. i guess it is id in one table and submission_status_id in the other one. try to change you relation in one of these two (try if one of them works):

1.

'submissionStatus' => array(self::HAS_ONE, 'SubmissionStatus', array('submission_status_id'=>'id'),

'submissionStatus' => array(self::HAS_ONE, 'SubmissionStatus', array('id'=>''),

2.

in your grid the code you use is correct but as stated in the comment from @Manquer, if the relation returns null it will not work. But you can try the more Yii style in the GridViiew like this: do not give any value only tne name

'name'=>'>submissionStatus.submission_status_name'

Altri suggerimenti

If the value of relation field is null then you relation will not return a object of the Foreign model; therefore calling the attribute in it will give the error try changing

'value' => '$data->submissionStatus->submission_status_name',

to

'value' => '(isset($data->submissionStatus))?
            $data->submissionStatus->submission_status_name:""',
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top