⚠ In case you've missed it we are migrating to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. Very soon this forum will be read-only and archived. ⚠


setRelation relational table field column display

joining tables setRelation

  • Please log in to reply
1 reply to this topic

#1 irshadjan

irshadjan

    Newbie

  • Members
  • Pip
  • 3 posts

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.



#2 Sorin Sfirlogea

Sorin Sfirlogea

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 18 January 2021 - 03: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.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users