php 事件记录::2000、1066,不唯一的表/别名: 'category'

标签 php mysql phpactiverecord

我收到 2000, 1066, Not unique table/alias: 'category' 此错误。我很清楚问题所在。

我的代码:

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

    );

生成以下查询

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

我需要使用别名,但我不知道如何在php ActiveRecord中使用别名

型号代码是:

<?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'));

}

和appModel代码:

<?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];
            }
        }
    }
}

最佳答案

我已完成使用核心代码更改。

Table.php中的更改

方法: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; 

}

变化是

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 是关系名称

关于php 事件记录::2000、1066,不唯一的表/别名: 'category',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38429594/

相关文章:

php - SQL 查询 ID 号以逗号分隔的列?

php - 遍历具有私有(private)属性的对象

php - 无法更新 php activerecord : model is read-only

PHP activerecord mysql 服务器已经消失

mysql - 如果存在具有该值的行,则从聚合组中获取具有特定值的行

ajax 生成的 PHP 表单无法正常工作

php - 搜索数组并在找到匹配时返回所有键和值

php - 正确的 PrettyPhoto rel 属性?

mysql - 重复和无序的数字

PHP、Mysql同一个函数查询不同的表