I have a main view called Dashboard.php in which I want to display a logged in employee's cell phone (from the employee_phone table which stores all employee phone numbers and are differentiated by 'phone_type' (ie cell, home, main)) in an arbitrary field labeled as 'phone 1:' on the view, then display either their 'home' or 'main' number as 'phone 2:'. Please note, for clarity I left out the 'home' phone provider as I'm sure I can figure it out if someone can help with the 'cell' phone type. I cannot get any phone number to display and I've tried several configurations. Any help is appreciated and I apologize in advance for my newness. I've read this page: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#querying-data and it seems like it fits my criteria but I just can't make it work. I've also read the related questions here on SO but they seem related to working with data in an array and displaying it in a ListView or Gridview. I also don't seem to be able to get the provider to be able to access the method getEmployeeCellPhone() with the magic method of employeeCellPhone.
I have two tables:
employee Table:
id
user_id
employee_phone Table:
id
employee_id
phone_number
phone_type
Employee.php Model:
public function getEmployeePhones()
{
return $this->hasMany(\frontend\models\EmployeePhone::className(), ['employee_id' => 'id']);
}
public function getEmployeeCellPhone()
{
return $this->hasOne(\frontend\models\EmployeePhone::className(), ['employee_id' => 'id'])
->where(['=', 'phone_type', 'Cell'])
->all();
}
EmployeeController:
public function actionDashboard($id)
{
$model = $this->findModel($id);
$providerCellPhone = $model->employeeCellPhone;
return $this->render('dashboard', [
'model' => $this->findModel($id),
'providerCellPhone' => $providerCellPhone,
]);
}
Dashboard.php View:
<div class="col-lg-3">
Phone 1: <?= $model->$providerCellPhone ?><br>
Phone 2: <?= $model->$providerHomePhone ?>
</div>