⚠ 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

set_relation and table where



HumbleMonk
  • profile picture
  • Member

Posted 09 May 2012 - 17:54 PM

I am trying to use a table where and set_relation on the same crud table and am having difficulties.

I have two tables, table_a and table_b. Both have a 'type' field. I am filtering table_a by the type column and then setting a relation to a field in table_b.

Here is the code:


$crud = new grocery_CRUD();
$crud->where('type', 'food');
$crud->set_theme('datatables');

$crud->set_table('table_a');

$crud->set_relation('contact', 'table_b', '{firstName} {lastName}');

$output = $crud->render();
$output = (array)$output;
$this->output($output);


The problem is that this returns a database error saying that 'type' on the where() is ambiguous. OK, no problem, I've had this error before on callback_column and followed this solution to get a unique field name. The problem is that I tried to use that on the where clause:

$crud->where($this->unique_field_name('type'), 'food');


And that returns a database error of 'unknown column' for the where().

Is there any solution besides changing the name of one of the fields?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 09 May 2012 - 18:30 PM

This should work:


$crud->where('table_a.type', 'food');

HumbleMonk
  • profile picture
  • Member

Posted 10 May 2012 - 03:12 AM

Perfect - that did the trick.