⚠ 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

Concatenate fields after retrieving them from different tables



h4kl0rd
  • profile picture
  • Member

Posted 30 September 2013 - 16:43 PM

I'm facing difficulties with concatenating two fields after joining tables. I went through a post about concatenating two fields into one (/topic/479-concatenate-two-or-more-fields-into-one-field/), it wasn't a great help.

 

Scenario:

Table items has fields item_name and item_type_id, which stores the id of item_type.

Table item_type has field item_type, which stores the item_type

 

I need to display a dropdown list that loads item_name + item_type

 

Example:

1. item_name = BEER, item_type = BOTTLE

2. item_name = BEER, item_type = CAN

 

So, dropdown should display:

BEER - BOTTLE

BEER - CAN


davidoster
  • profile picture
  • Member

Posted 30 September 2013 - 17:40 PM

Hello and welcome to the forums [member=h4kl0rd].

Please tell us where would you like this concatenation to take place because there are a few different ways to do this and at least one of them is with one line of code and you're done!

When I say where I mean:

- while inserting or editing a record?

- while viewing a single record?

- while viewing the table list?

 

Each of the above has a different implementation method for such a concatenation.

Remark: If I am not mistaken concatenation (for the example you describe) can only happen when viewing either a record or the list where you get two or more fields and you display them as one.


h4kl0rd
  • profile picture
  • Member

Posted 01 October 2013 - 09:11 AM

Thanks @davidoster for the welcome,

I want to view the record list in the dropdown.


davidoster
  • profile picture
  • Member

Posted 01 October 2013 - 09:22 AM

So while inserting or editing.

Then it's as simple as to use the set_relation function with such a format:

$crud->set_relation('user_id','users','{username} - {last_name} {first_name}');

 

Then in order to display, wherever you want the BEER - BOTTLE / BEER - CAN

you need to use the columns and create a fake column e.g. $crud->columns('myfake_column');

And then you can use the callback_column which gives you access to the specific column (field) and mask it with the values you want.


h4kl0rd
  • profile picture
  • Member

Posted 01 October 2013 - 09:46 AM

The set_relation code: 

$crud->set_relation('user_id','users','{username} - {last_name} {first_name}');

works only when both fields that are to be retrieved are in the same table.

 

So according to the scenario from my early post, I explained that both values come from different tables.


davidoster
  • profile picture
  • Member

Posted 01 October 2013 - 17:51 PM

Excuse me but you mention just two tables and you never referred to a third one.

If you have another controller that has nothing to do with the two above tables you mentioned, items and item_type then you need to use a custom model to get the data and build something similar to the set_relation function yourself.


h4kl0rd
  • profile picture
  • Member

Posted 02 October 2013 - 07:40 AM

Thanks,

 

I'm working with only two tables (items and item_type).

items has fields item_name and item_type_id.

item_type has field item_type, which stores the name of the item type.

 

I want to retrieve the item_name (from items table) and item_type (from item_type table using the FK item_type_id).