Hello Guys,
Sorry for the long post, but I’m very new to Yii, let alone Yii2. I would like to understand how I should handle table relationship, so I set up a very simple scenario with two tables, one called employee and another one called team. An employee can belong to one team and one team can have multiple employees. Here is the migration that I created for this scenario:
class m131125_221834_create_basic_schema extends \yii\db\Migration
{
public function up()
{
$this->createTable('employee', [
'id' => Schema::TYPE_PK.' NOT NULL',
'first_name' => Schema::TYPE_STRING.'(32) NOT NULL',
'last_name' => Schema::TYPE_STRING.'(32) NOT NULL',
'account_type' => Schema::TYPE_STRING."(1) NOT NULL DEFAULT 'E'",
'team_id' => Schema::TYPE_INTEGER.' NOT NULL'
]);
$this->createTable('team', [
'id' => Schema::TYPE_PK.' NOT NULL',
'short_name' => Schema::TYPE_STRING.'(64) NOT NULL',
'description' => Schema::TYPE_TEXT
]);
$this->addForeignKey('employee_to_team','employee','team_id','team','id');
}
public function down()
{
$this->dropForeignKey('employee_to_team','employee');
$this->dropTable('employee');
$this->dropTable('team');
}
}
As you can see there is a foreign key in the employee table referencing a team_id in the team table. I’m using the yii2-advanced app for starting point. Created models (\common\models\team and \common\models\employee respectively) for both tables using the gii utility. Also created a CRUD for the employee table using gii.
When I go to the employee index page, team_id will be listed as opposed to the team’s name.
So, opened the file called \common\models\search\EmployeeSearch and located the search() function. It defines the query in the first line, which originally reads:
$query = Employee::find();
Replaced the first line with the following so that the corresponding team records will be provided:
$query = Employee::find()->leftJoin('team','employee.team_id=team.id')->with('Team');
Also replaced the ‘team_id’ column of the GridView widget with ‘Team’ like so (\backend\views\employee\index.php):
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'first_name',
'last_name',
'account_type',
'Team',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I got an error Getting unknown property: common\models\Employee::Team
so added the __toString() method to \common\models\Team:
public function __toString()
{
return $this->short_name;
}
It did the trick. Team ID is gone and friendly name is displayed in the GridView widget. However, the filter field remains empty and I cannot figure it out how I can make GridView to provide a dropdown box with all used teams or at least an input box where the user can input the name of the team.
I tried using the filter property in GridView with no luck.
Can you guys please give me a pointer how should I implement the filter functionality in case table relationship is used?
Thank you in advance.
PS.: In the meantime I figured out sorting but still no luck with filters.