【问题标题】:php active record :: 2000, 1066, Not unique table/alias: 'category'php 活动记录 :: 2000, 1066, Not unique table/alias: 'category'
【发布时间】:2016-07-18 06:07:48
【问题描述】:

我得到 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)

我需要使用 alias 但我不知道如何在 php ActiveRecord 中使用 alias

型号代码是:

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

【问题讨论】:

  • 您确定包含的代码会生成该查询吗?我想你也看到了别名问题。
  • 是的,它会生成该查询@TimBiegeleisen

标签: php mysql phpactiverecord


【解决方案1】:

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

更改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 是一个关系名称

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2018-05-18
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多