Search code examples
phpmysqlphpactiverecord

php active record :: 2000, 1066, Not unique table/alias: 'category'


I got 2000, 1066, Not unique table/alias: 'category' this error. I very well know what the issue.

My Code :

static $belongs_to = array(
        array('Parent',
              'foreign_key'=>'parent_id',             
              'class_name'=>'categoryModel',
             ),
        array('gallery','class_name'=>'galleryModel','foreign_key'=>'image_id'),

    );

genrate below query

SELECT `category`.* FROM `category` INNER JOIN `category` ON(`category`.parent_id = `category`.category_id)

I need to use alias but I don't know how to use alias in php ActiveRecord

Model Code Is :

<?php

class categoryModel extends appModel
{
    static $table_name = '`category`';  
    static $primary_key = 'category_id';
    static $belongs_to = array(
        array('Parent',
                'foreign_key'=>'parent_id',           
              'class_name'=>'categoryModel',
             // 'conditions' => 'parent.parent_id is null'

            ),
        array('gallery','class_name'=>'galleryModel','foreign_key'=>'image_id'),

    );

    static $has_many = array(
        array('cr','class_name'=>'category_relationModel','foreign_key'=>'category_id')
    );

      static $delegate = array(
            array('name', 'to' => 'parent', 'prefix' => 'parent'));

}

and appModel Code :

<?php 
class appModel extends ActiveRecord\Model{
    public $app,$attr;
    public $db_config;

    function __construct(array $attributes=array(), $guard_attributes=true, $instantiating_via_find=false, $new_record=true){
        parent::__construct($attributes, $guard_attributes,$instantiating_via_find, $new_record);
    }


    function __call($key,$value){
        parent::__call($key,$value);
    }

    public function load($data){
        foreach($this->attributes as $attr=>$value){
            if(!isset($this->attr["".$attr])){
                //if(isset($data[$attr])) $this->attr["".$attr] =  $data[$attr];
                if(isset($data[$attr])) $this->attributes["".$attr] =  $data[$attr];
            }
        }
    }
}

Solution

  • I have Done Using Core Code Changing.

    Change In Table.php

    Method : create_joins($joins)

    if (array_key_exists($rel->class_name,$existing_tables))
    {   
    
                            $alias = $value;
                            $existing_tables[$rel->class_name]++;
    }else{
                            $existing_tables[$rel->class_name] = true;
                            $alias = NULL; // change this to  $alias = $value; 
    
    }
    

    change is

    if (array_key_exists($rel->class_name,$existing_tables))
    {   
    
                            $alias = $value;
                            $existing_tables[$rel->class_name]++;
    }else{
                            $existing_tables[$rel->class_name] = true;
                            $alias = $value; 
    
    }
    

    $value is a relation name