Вопрос

I have three tables whose structure is is similar to below :

employees    offices    postings
________   ________  ___________
id           id       employees_id  
name         name     offices_id

So I want to know how can I get the office name from Employee model. Putting office within $hasOne array shows Unknown column 'office.employees_id' in 'on clause'. What should I do to get the office name in the results ?

Это было полезно?

Решение

If you want to stick to your database, you might use has and belongs to many relation. In employee.php:

   public $hasAndBelongsToMany = [
       'Office'=> [
            'foreign_key' => 'employees_id',
            'joinTable' => 'postings',
            'associationForeignKey' => 'offices_id',
       ]
   ];

Then you can get office from employee model.

What I really want to suggest is that put a column office_id in employees table, and put

$public $belongsTo = ['office'];

in your employee model.

PS: Brackets [] is supported in php 5.4 or newer , if you are using php 5.3 or lower, you may want to replace it with array().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top