⚠ 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

multiple set_relation = strange bug



luizzz
  • profile picture
  • Member

Posted 08 March 2012 - 22:55 PM

Hi!
I have a table "cars" that has the foreign fields "idbrand" and "idmodel" which references the tables "brands" and "models". Ok? Ok.


Just found a strange bug when i try the following:


$crud->set_relation('idbrand','brands','{name}')
->set_relation('idmodel','models','{name}');


If i left just one "set_relation", it works...
If i put many "set_relations", gives me the following error:


[i]Error Number: 1054[/i]
[i]Column 'jed4f12e7.name' unknown in 'field list'[/i]
[i]SELECT cars.*, CONCAT('', COALESCE(jed4f12e7.name, ''), '') as sed4f12e7, CONCAT('', COALESCE(je1f0af3d.name, ''), '') as se1f0af3d FROM (`cars`) LEFT JOIN `models` as je1f0af3d ON `je1f0af3d`.`id` = `cars`.`idmodel`[/i]
[i]Filename: .\system\database\DB_driver.php[/i]
[i]Line Number: 330[/i]


Note that it's considered only the last "LEFT JOIN" command (from "idmodel"). Where's the "LEFT JOIN" for "idbrands"?

My poor solution was to create a new protected array called "$join_cache" that flushes the left join string parameters (separated with |) when the system calls "get_list()", before the "get" command... like this:


$this->db->select($select, false);
foreach ($this->join_cache as $item)
{
$jointmp = explode("|",$item);
$this->db->join($jointmp[0],$jointmp[1],$jointmp[2]);
}
$results = $this->db->get($this->table_name)->result();


(in [i]grocery_model.php[/i])

Obviously, not a good solution and not recommended. :)
So... what's happening? CodeIgniter bug?

Sorry, my english is bad, very bad. :/

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

Posted 08 March 2012 - 23:56 PM

It is a strange bug as it works fine for me and for everyone. Try this patch: https://github.com/s...9340caae1cde0d5 I just remove the from grocery_model.php

if($this->db_table_exists($related_table))


it was useless.

If this doesn't work for you can you please tell me: PHP version, Codeigniter version and grocery CRUD version?

luizzz
  • profile picture
  • Member

Posted 09 March 2012 - 00:42 AM

Doesn't work. Still got the error 1054.
My PHP version is 5.3.0, CodeIgniter 2.1.0, grocery CRUD 1.1.8, mySQL 5.1.37, Apache 2.2.13, Windows 7 SP1 x64. Very bizarre. ;)

I've changed the following:


protected $join_cache = array();
(...)
function get_list()
{
if($this->table_name === null)
return false;

$select = "{$this->table_name}.*";

if(!empty($this->relation))
foreach($this->relation as $relation)
{
list($field_name , $related_table , $related_field_title) = $relation;
$unique_join_name = $this->_unique_join_name($field_name);
$unique_field_name = $this->_unique_field_name($field_name);

if(strstr($related_field_title,'{'))
$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
else
$select .= ", $unique_join_name.$related_field_title as $unique_field_name";

if($this->field_exists($related_field_title))
$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
}

$this->db->select($select, false);

foreach ($this->join_cache as $item)
{
$jointmp = explode("|",$item);
$this->db->join($jointmp[0],$jointmp[1],$jointmp[2]);
}
$results = $this->db->get($this->table_name)->result();

return $results;
}
(...)
function join_relation($field_name , $related_table , $related_field_title)
{
if($this->db_table_exists($related_table))
{

$related_primary_key = $this->get_primary_key($related_table);
if($related_primary_key !== false)
{
$unique_name = $this->_unique_join_name($field_name);
$this->join_cache[] = $related_table.' as '.$unique_name ."|". "$unique_name.$related_primary_key = {$this->table_name}.$field_name" ."|". 'left';
//$this->db->join( $related_table.' as '.$unique_name , "$unique_name.$related_primary_key = {$this->table_name}.$field_name",'left');
$this->relation[$field_name] = array($field_name , $related_table , $related_field_title);
return true;
}
}

return false;
}

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

Posted 09 March 2012 - 07:26 AM

That's weird. I will check it this weekend deeper and I will have an answer for you.

luizzz
  • profile picture
  • Member

Posted 09 March 2012 - 18:02 PM

Hey Johnny! Problem solved! :D
I don't know exactly why but, when i downloaded the Grocery CRUD version in GitHub, the problem has gone.
Maybe a cache problem in my server?
And you are right about the useless "db_table_exisits" condition inside join_relation function. When i put it, the problem returns.
Thank you!