⚠ 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

error set relation when use custom model



Trần Đình Trọng
  • profile picture
  • Member

Posted 23 May 2013 - 08:44 AM

I have used custom model

class Crud_work_model extends grocery_CRUD_Model {
    function get_list(){
        $pj_id = $this->uri->segment(3);
        $sql = "SELECT * FROM tblworks WHERE projectid='$pj_id'";
        $arr = $this->db->query($sql)->result();   
        return $this->works_multi_level_result($arr);
        
    }
    function db_insert($post_array) {
        $post_array['projectid'] = $this->uri->segment(3);
        $post_array['parentid']= $this->uri->segment(4);;
        parent::db_insert($post_array);
    }
 .......
........
}

works_multi_level_result is my function, test ok

 

My controller :

<?php
require_once APPPATH.'controllers/app.php';
class Works extends App{
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->library('grocery_CRUD');
    }
    function manager($pj_id){
        //echo $this->uri->segment(3);
        $crud = new grocery_CRUD();
        $crud->set_subject('Công việc');
        $crud->set_table('tblworks');
        $crud->set_model('crud_work_model');
        $crud->columns('name','beginonreality','reportdate');
        $crud->unset_fields('tiedate','beginonplan','timevalueonplan','labour','timetieid','costonreality','datetieid','projectid','percent','parentid');
        $crud->unset_edit_fields('tiedate','beginonplan','timevalueonplan','labour','timetieid','costonreality','datetieid','projectid','parentid');
        $crud->set_relation('timetype','tbltimetype','name');
        $crud->set_relation('timeunitid','tbltimeunit','name');
        $crud->display_as('name','Tên công việc');
        $crud->display_as('beginonreality','Bắt đầu');
        $crud->display_as('reportdate','Ngày báo cáo');
        $crud->display_as('timevalueonreality','Thời gian thực hiện');
        $crud->display_as('timeunitid','Đơn vị thời gian');
        $crud->display_as('costonplan','Chi phí');
        $crud->display_as('timetype','Loại thời gian');
        $crud->display_as('parentid','Công việc cha');
        $crud->display_as('percent','Tiến độ hoàn thành (%)');
        //$crud->add_action('Phân rã công việc', base_url()."assets/public/img/icon/settings.png','works/manager/add");
        $output = $crud->render();
        $output->title = "Công việc";
        $output->content_title = "Công việc";
        $output->sb = $this->_menu;
        $this->load->view('common/layout_view',$output);
        
    }
}
?>

Every thing ok with add, edit, but with view :

 

Error Number: 1066

Not unique table/alias: 'jda11d612'

SELECT * FROM (`tblworks`) LEFT JOIN `tbltimetype` as jda11d612 ON `jda11d612`.`id` = `tblworks`.`timetype` LEFT JOIN `tbltimeunit` as jd6e1890b ON `jd6e1890b`.`id` = `tblworks`.`timeunitid` LEFT JOIN `tbltimetype` as jda11d612 ON `jda11d612`.`id` = `tblworks`.`timetype` LEFT JOIN `tbltimeunit` as jd6e1890b ON `jd6e1890b`.`id` = `tblworks`.`timeunitid` LIMIT 25

Filename: /home/dinhtrong/public_html/pm/models/grocery_crud_model.php

Line Number: 186

 

Left Join is repeated,

Is it a bug ? and what is slution for this problem

Thanks very much


davidoster
  • profile picture
  • Member

Posted 24 May 2013 - 05:53 AM

I really don't know what is wrong with your code but I suspect that this function of yours, works_multi_level_result($arr), is the one to blame.

Other than that, why don't you implement the "WHERE ..." of your query within your controller instead of having it inside the get_list function?

This Grocery CRUD after this change won't be able to to work with other tables if it needs to!

 

Why don't you,

$crud->where('projectid', $pj_id);

within your controller?


Trần Đình Trọng
  • profile picture
  • Member

Posted 24 May 2013 - 08:21 AM

Thanks for your help.

Why don't you,

$crud->where('projectid', $pj_id);

within your controller?

 

I must use custom model, because i must arrange result to : multi lever parent-childild. That is :

parent 1

parent 2

----------child 2.1

----------child 2.2

-----------------child 2.2.1

-----------------child 2.2.3

---------child 2.3

parent 3

 

( ---------- : &rarr; )

 

 

this is code, i  use recursion, i tested this function, it return a array, container  objects

<?php 
class Crud_work_model extends grocery_CRUD_Model {
    function get_list(){
        $pj_id = $this->uri->segment(3);
        $sql = "SELECT * FROM tblworks WHERE projectid='$pj_id'";
        $arr = $this->db->query($sql)->result();   
        return $this->works_multi_level_result($arr);
        
    }
    function db_insert($post_array) {
        $post_array['projectid'] = $this->uri->segment(3);
        $post_array['parentid']= $this->uri->segment(4);;
        parent::db_insert($post_array);
        
    }
    function works_multi_level($arrData, $parentid = 0, $level = 0, &$result) {
        if (count($arrData) > 0) {
            foreach ($arrData as $key => $val) {
                if ($parentid == $val->parentid) {
                    $val->level = $level;
                    $result[] = $val;
                    $_parentid = $val->id;
                    unset($arrData[$key]);
                    $this->works_multi_level($arrData, $_parentid, $level + 1, $result);
                }
            }
        }
 }
    function works_multi_level_result($arrData,$selected_id=null){
        $this->works_multi_level($arrData, 0, 0, $result);
        for($i=0;$i<count($arrData);$i++){
            
        }
        foreach ($result as $key => $val) {
            $numRepeat = $val->level;
            $char = '';
            if ($numRepeat > 0) {
                $char .= str_repeat('&rarr;', $numRepeat);
            }
            $val->name = $char.'&nbsp;'.$val->name;
            
        }
        return $result;
 }
}
?>

52ef9d129084a5700afcfa672b89ff29_5583173