07-31-2008, 04:31 PM
[eluser]Paul T[/eluser]
OK, after spending some time dissecting the code, I came up with a decent solution. I'm not sure how scalable it is, but it gets the job done.
First, in codexcontroller, around line 280:
I did this because there may or may not be a join statement, but if there is, this will prevent ambiguity.
In my controller, I added a couple new arrays to the config array. One maps the many-to-many field name from 'display_fields' to the proper SQL syntax. The other array contains whatever joins you will need. For example:
Then, in the codexcontroller, make sure to declare your table_joins and field_to_lookup variables, and also populate those variables in the setConfig() function.
In the search() function, I replaced this code:
with this code:
Did I write nice PHP? Nope. Did I put all this modified code in the right places? Probably not. Does it work? So far!
I'm sure many of you will find a much more elegant way to approach this. If you do have a better way, please share it so I can use well formed code, and not my sloppy PHP. :-)
~Paul
OK, after spending some time dissecting the code, I came up with a decent solution. I'm not sure how scalable it is, but it gets the job done.
First, in codexcontroller, around line 280:
Code:
foreach($field_names as $k=>$v)
{
$field_names[$k] = $this->table . '.' . $v;
}
I did this because there may or may not be a join statement, but if there is, this will prevent ambiguity.
In my controller, I added a couple new arrays to the config array. One maps the many-to-many field name from 'display_fields' to the proper SQL syntax. The other array contains whatever joins you will need. For example:
Code:
$config = array(
'db_table' => 'yourTable', //The name of the table associated with this controller
'form_setup' => $this->spyc->YAMLLOAD($this->codexadmin->getDefinitionFileName('yourTable_form')), //The array that holds our elements
'controller_name' => 'yourController', //The name of the controller, so that it can be used when generating the URLs
'primary_key' => 'id', //The name of the controller, so that it can be used when generating the URLs
'display_fields'=>array('yourTableField1','yourTableField2','yourRelatedField1'),
'field_to_lookup'=>array('yourRelatedField1'=>'yourRelatedTable.yourRelatedFieldName'),
'table_joins'=>array('yourJoinTable'=>'whatever = whatever','yourJoinTable2'=>'whatever = whatever'), //for use in $this->db->join()
'rules'=>$rules
);
Then, in the codexcontroller, make sure to declare your table_joins and field_to_lookup variables, and also populate those variables in the setConfig() function.
In the search() function, I replaced this code:
Code:
for($i=0;$i<count($keywords);$i++){
$this->db->like($fields[$i],$keywords[$i]);
}
with this code:
Code:
for($i=0;$i<count($keywords);$i++){
$lookup = $fields[$i];
if(count($this->field_to_lookup) > 0 && $this->field_to_lookup != '' && array_key_exists($fields[$i], $this->field_to_lookup))
{
if ($keywords[$i] != '')
{
$this->db->like($this->field_to_lookup[$lookup],$keywords[$i]);
$includejoinstatement = 'yes';
}
}
else
{
$this->db->like($this->table.'.'.$fields[$i],$keywords[$i]);
}
}
if(isset($includejoinstatement)) {
foreach($this->table_joins as $field=>$value)
$this->db->join($field,$value);
}
Did I write nice PHP? Nope. Did I put all this modified code in the right places? Probably not. Does it work? So far!
I'm sure many of you will find a much more elegant way to approach this. If you do have a better way, please share it so I can use well formed code, and not my sloppy PHP. :-)
~Paul