在員工網格視圖的columns
屬性補充一點:
// this is in place of just 'departmentId',
array(
'name'=>'departmentId', // name of the foreign key attribute
'value'=>'$data->department->name', // access the 'name' attribute of the related record through relation named 'department', the current record is represented by '$data'
'type'=>'raw' // data is of raw type
),
上CDataColumn閱讀就知道如何修改上面的陣列。
注:由於您只要求顯示,上面的代碼將工作,如果你想爲這列的篩選工作,你就必須要麼
- 修改員工模型的搜索功能(或任何你正在進行搜索的地方)將輸入字符串映射到departmentId或
- 編寫一些JavaScript以將相關部門的ID發送到搜索,而不是字符串。
眼下的過濾器將只與DepartmentID的(即整數)沒有名字(串)工作。
你也可以做到這一點,而不是上面的代碼:
//again in place of departmentId
'department.name' // using the 'department' relation, accessing its 'name'
但隨後得到過濾器將是艱難的(你將不得不再次使用數組),和列標題也將成爲屬性標籤部門模型的名稱屬性。可以使用以下格式更改標題:'name:type:header'
,因此您可以執行以下操作:'department.name:Department'
大多數這些詳細信息都可以在我所包含的文檔鏈接中找到。
您是否已將它們連接到數據庫關係中? – Oliver
我必須再次生成模型,如果我改變我的數據庫中的任何東西? –
所以你有你的模型中的關係自動生成? –