0

I have a table

CREATE TABLE IF NOT EXISTS `register` (
  `id` int(9) NOT NULL AUTO_INCREMENT,
  `student_id` varchar(15) NOT NULL,
  `title` varchar(100) NOT NULL,
  `name` varchar(250) NOT NULL,
  `email` varchar(50) NOT NULL,
  `gsm` varchar(15) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `birth_date` date NOT NULL,
  `department_id` int(2) NOT NULL,
  `specialization` varchar(150) NOT NULL,
  `level_id` int(2) NOT NULL,
  `reason` varchar(2000) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

which have relation with two tables department and level

CREATE TABLE IF NOT EXISTS `level` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `level` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

and

CREATE TABLE IF NOT EXISTS `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `department` varchar(300) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

I have made relation in Register model as

public function getDepartments() {
        return $this->hasOne(app\models\Department::className(), ['id' => 'department_id']);
    }

But in view.php (view file )when I use

<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'student_id',
            'name',
            'email:email',
            'gsm',
            'gender',
            'birth_date',

 'department' ,
            'specialization',
            'level',
            'reason',
        ],
    ]) ?> ,

the department is shown as 'not set'. Also print_r($model);?> shows [department] =>(i.e, blank) . What is the issue here ?

6
  • Use ['attribute' => 'department_id', 'value' => $model->departments->department]. if not then show your query. Commented Jan 7, 2016 at 9:20
  • It shows Class 'app\models\app\models\Department' not found Commented Jan 7, 2016 at 9:33
  • is this view file of register class? Commented Jan 7, 2016 at 9:35
  • Yes , the error is pointing to public function getDepartments() { return $this->hasOne(app\models\Department::className(), ['id' => 'department_id']); } Commented Jan 7, 2016 at 9:36
  • add path to department model in register model. Commented Jan 7, 2016 at 9:39

2 Answers 2

2

Use $model->relationName->field_name:

[
'attribute' => 'department_id', 
'value' => $model->departments->department
],

And add department model in register model.

Sign up to request clarification or add additional context in comments.

Comments

0

better change getDepartments to getDepartment as this is a hasOne relation.

[
  'attribute' => 'department_id', 
  'value' => isset($model->department) ? $model->department->department : ''
]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.