⚠ 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

How to get two field at table relation ?



Nguyễn Hải Nam
  • profile picture
  • Member

Posted 20 August 2013 - 11:58 AM

I have 2 table:

Table_A: a_id, a_name, a_unsigned_name

Table_B: b_id, b_name, a_id

 

When i using crud, how i can get my column like this: b_id, b_name, a_name, a_unsigned_name

 

Below is my example code:

$crud = new grocery_CRUD();
$crud->set_table('Table_B');
$crud->set_subject('Table B');
$crud->columns('b_id', 'b_name', 'a_id', 'a_unsigned_name');
$crud->set_relation('a_id', 'Table_A', 'a_name');

Now my result: a_unsigned_name column is null.

 

I try with below code but it's been overid a_name:

$crud = new grocery_CRUD();
$crud->set_table('Table_B');
$crud->set_subject('Table B');
$crud->columns('b_id', 'b_name', 'a_name', 'a_unsigned_name');
$crud->set_relation('a_id', 'Table_A', 'a_name');
$crud->set_relation('a_id', 'Table_A', 'a_unsigned_name');

I have a question for everybody: How i can get value for a_unsigned_name column at Table_A.a_unsigned_name ?

 

I try find more way to resolve it but unsuccessful :(. Can you help resolve it?

 

 

       

 

 

 


Amit Shah
  • profile picture
  • Member

Posted 20 August 2013 - 14:18 PM

Well there surely is .. an indirect way. Directly wont be possible as the you trying to display the column of other table and grocerycrud by default deals in with single table.

Well if you want .. you can ignore showing a_id and there u can set_relation to the table_a and there u display the required field

other way around what you can do is ..a column callback to a_unsigned_name field name...

there retrieve the row from table_a based on a_id

and then return the a_unsigned_name from table_a

 

third way is much complex and not needed ... write your own model


Nguyễn Hải Nam
  • profile picture
  • Member

Posted 20 August 2013 - 16:32 PM

I'm very happy for your supporting. I have each a way to resolve it but it's not good for me.

I can show for you:

 

Table_A: a_id, a_name, a_unsigned_name

Table_B: b_id, b_name, a_id, a_id1

and my reference:

a_id => a_name,

a_id1 => a_unsigned_name

a_id = a_id1(will be updated when add/edit) .

 

Code for this option: 

$crud = new grocery_CRUD();
$crud->set_table('Table_B');
$crud->set_subject('Table B');
$crud->columns('b_id', 'b_name', 'a_id', 'a_id1');
$crud->set_relation('a_id', 'Table_A', 'a_name');
$crud->set_relation('a_id1', 'Table_A', 'a_unsigned_name');

I think this way, it'll support for my advanced search which be modify for flexigrid theme.


chavamm
  • profile picture
  • Member

Posted 20 August 2013 - 17:14 PM

hi,
If your purpose is to only show a_unsigned_name field value, you can do the following:

$crud->set_relation('a_id', 'Table_A', '{a_name} - {a_unsigned_name}');

I hope help you.

 

Thanks, Regards.


Nguyễn Hải Nam
  • profile picture
  • Member

Posted 21 August 2013 - 02:09 AM

Hi chavamm,

 

I don't want a_name column and  a_unsigned_name in a column like that. It must be separated two column.
 

Thanks, Regards.


Amit Shah
  • profile picture
  • Member

Posted 21 August 2013 - 08:22 AM

well.. than thats the way i suggested earlier - define the fields and use callbacks. That should suffice your need.