개발 Q&A

제목 grocery_crud 사용 문제
글쓴이 예아 작성시각 2014/03/11 16:47:11
댓글 : 1 추천 : 0 스크랩 : 0 조회수 : 16431   RSS
 grocery_crud를 사용하고 있습니다.

ex1은 지금 제가 사용하려는 방법을 예를 든 소스입니다.

ex1)
$crud = new grocery_CRUD();
 
$crud->set_theme('datatables');
$crud->set_table('employees');
$crud->set_relation('officeCode','offices','city');
$crud->display_as('officeCode','Office City');
$crud->set_subject('Employee');
 
        $crud->set_model('Custom_query_model');
$crud->basic_model->set_query_str('SELECT * FROM employees');

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



위와 같이 작성한 이유는 ...

grocery_CRUD에서 제공하는 set_relation를 이용하면
DB에 추가할 때 그 내용을 자동적으로 ComboBox형태로 바꿔서 표시해 주는것을 이용하려고 한거구요.

두번째로 query문으로 summary를 union시킬 필요성이 있어서
(SELECT * FROM employees)에서 그 query를 작성하려고 하고 있구요.
(http://www.grocerycrud.com/forums/topic/1963-simple-guide-to-executing-custom-queries/ 이 사이트를 참고했습니다.)
 
문제는 위와 같이 작성했을때 'offices' Table의 join을 두번한다는 겁니다.

ex2는 join을 두번함으로써 나타난 페이지이구요.

ex2)

Error Number: 1066

Not unique table/alias: 'jd29d42cf'

SELECT COUNT(*) AS `numrows` FROM (`employees`) LEFT JOIN `offices` as jd29d42cf ON `jd29d42cf`.`officeCode` = `employees`.`officeCode` LEFT JOIN `offices` as jd29d42cf ON `jd29d42cf`.`officeCode` = `employees`.`officeCode`

Filename: C:\wamp\www\dain\system\database\DB_driver.php

Line Number: 330



그 join을 하는 소스는 ex3)와 같습니다.

ex3)
if(!empty($this->relation))
foreach($this->relation as $relation)
$this->basic_model->join_relation($relation[0],$relation[1],$relation[2]);

그리고 join_relation을 따라가 확인해보면 ex4)과 같습니다.
ex4)
    function join_relation($field_name , $related_table , $related_field_title)
    {
$related_primary_key = $this->get_primary_key($related_table);
        
if($related_primary_key !== false)
{
            
            $unique_name = $this->_unique_join_name($field_name);
$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;
    }


결론은 위에서 'office' Table을 두번 Join하는 것을 방지하고 싶은데요.

어떻해야할지 난감합니다.

선배님들의 도움을 부탁드립니다. _ _)
태그 grocery crud,grocery_crud
 다음글 ckeditor질문입니다 (3)
 이전글 필드 값을 Array에 저장하는 방법 좀 알려주세요 (1)

댓글

예아 / 2014/03/11 17:19:08 / 추천 0
     private $mintest = '';
    function join_relation($field_name , $related_table , $related_field_title)
    {
$related_primary_key = $this->get_primary_key($related_table);
        
if($related_primary_key !== false)
{
            if($this->mintest !=$field_name)
            {
            $unique_name = $this->_unique_join_name($field_name);
$this->db->join( $related_table.' as '.$unique_name , "$unique_name.$related_primary_key = {$this->table_name}.$field_name",'left');
            
            $this->mintest =$field_name;
$this->relation[$field_name] = array($field_name , $related_table , $related_field_title);
            }
            return true;
}
 
    return false;
    }

현재 위와같이 변경하여 해결하였습니다.