0

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>

1 Answer 1

1

Please try the following to check if your issue is solved -

//EmployeeController
public function actionDashboard($id)
{
    $model = $this->findModel($id);

    return $this->render('dashboard', [
        'model' => $model,
    ]);
}

And the view file -

//Dashboard.php View
<div class="col-lg-3">
    Phone 1: <?= $model->employeeCellPhone[0]->phone_number  ?><br>
    Phone 2: <?= $model->employeeHomePhone[0]->phone_number  ?>
</div>

To properly output the value $model->employeeCellPhone[0] is required because in your getEmployeeCellPhone() function, you have used the ->all() function. The Dashboard code is written assuming that in DB only one phone number will exist per user per phone_type. If that is not the case, you need to loop over $model->employeeCellPhone in the view and get the desired output.

Also, your Model code pasted above, doesn't show the getEmployeeHomePhone() function, I am assuming you have it there.

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

10 Comments

Correct. But as mentioned in my answer you can simply do a foreach loop and print the output
Bad mistake - I will update the answer. I just printed employeeCellPhone[0] which is the object. We need to print employeeCellPhone[0]->phone_number. This should get rid of the error
I get the error, 'Object of class frontend\models\EmployeePhone could not be converted to string' when I do it that way. Any other suggestions?
Thanks again for helping, but isn't there a way without looping to simply pick the phone numbers of the logged in user with the phone_type of 'cell'?
I converted getEmployeeCellPhone() into hasMany but that doesn't seem to help either.
|

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.