[quote name='web-johnny' timestamp='1333188714' post='1018']
Hello [member='R2D2'] and welcome to the forum,
Actually perhaps it seems obvious to grocery CRUD to have joins and customs queries to the table, but it still NOT an available feature at this moment. That's why you didn't find it at google.
The only thing you can do for now and not change the core of grocery CRUD is to use the set_model function (
http://www.grocerycr...tions/set_model ) . So in your case you just have to create a model that it will look something like this (the name Users_join is just an example):
<?php
class Users_join extends grocery_CRUD_Model
{
//The function get_list is just a copy-paste from grocery_CRUD_Model
function get_list()
{
if($this->table_name === null)
return false;
$select = "{$this->table_name}.*";
// ADD YOUR SELECT FROM JOIN HERE, for example: <------------------------------------------------------
// $select .= ", user_log.created_date, user_log.update_date";
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);
// ADD YOUR JOIN HERE for example: <------------------------------------------------------
// $this->db->join('user_log','user_log.user_id = users.id');
$results = $this->db->get($this->table_name)->result();
return $results;
}
}
You can download the file from [attachment=89:users_join.php]
So in any case you have to create a separate file for each let's say different case.
[b]Important note: [/b]Before using the set_model method make sure that you totally understand how the set_relation and set_relation_n_n works. I am telling this for the reason that the set_model is suggested to use it from more let's say familiar users with grocery CRUD
[/quote]
@Johnny
I've succeed using my custom model only for get_list() and display selected record for update purpose. Because of using custom model, the grocery CRUD didn't work well for action INSERT, UPDATE & DELETE.
Why it happens?
Here is my custom model:
class Users_join extends grocery_CRUD_Model
{
function get_list()
{
if($this->table_name === null)
return false;
$select = "{$this->table_name}.*";
// ADD YOUR SELECT FROM JOIN HERE <------------------------------------------------------
// for example $select .= ", user_log.created_date, user_log.update_date";
$select .= ", users.*";
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);
// ADD YOUR JOIN HERE for example: <------------------------------------------------------
// $this->db->join('user_log','user_log.user_id = users.id');
$this->db->join('users','users.id = '. $this->table_name . '.user_id');
$results = $this->db->get($this->table_name)->result();
return $results;
}
And here is my grocery CRUD function:
function teachers_display(){
$crud = new grocery_CRUD();
$crud->set_model('users_join');
$crud->set_theme('flexigrid');
$crud->set_table('teachers','users');
$crud->set_subject('teacher');
$crud->columns('user_id','name','address','phone','city','username');
$crud->required_fields('name');
$crud->change_field_type('user_id','invisible');
$crud->fields('user_id','name','address','phone','city','username');
$output = $crud->render();
$this->_crud_output($output);
}
big thanks for your kindly reply