⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

setRelation relational table field column display



irshadjan

irshadjan
  • profile picture
  • Member

Posted 31 December 2020 - 07:12 AM

Hello,

I am a kinda confused with setRelation function. Tables are joined but i can't display a field from relational table field in columns i can only display basic table fields as column. How can i display the fields from "Relational table" in column as well? Although joining is working fine.


Sorin Sfirlogea

Sorin Sfirlogea
  • profile picture
  • Member

Posted 18 January 2021 - 15:25 PM

Not 100% sure, but I don't think that this is the purpose of setRelation function. It seems to be designed only to replace foreign keys with a proper name from the related table, but the other fields are not available as columns in the datagrid. The only workaround that I see is to use the multiple fields option from setRelation function. Instead of returning one field, it can concatenate several fields and display them, but in the same column of the related foreign key

Example: 
Table CUSTOMER with fields: id, name, city_id 
Table CITY with fields: id, name, country
 
$crud->setTable('CUSTOMER'); 
$crud->setRelation('city_id', 'CITY', 'name'); 

will result in 
--------------------------------- 
| id | name       | city_id     | 
--------------------------------- 
| 1  | John Smith | Madrid      | 

and: 
$crud->setTable('CUSTOMER'); 
$crud->setRelation('city_id', 'CITY', '{name}, {country}'); 

will result in 
----------------------------------- 
| id | name       | city_id       | 
----------------------------------- 
| 1  | John Smith | Madrid, Spain |

Hope that helps.