dtamho6404 2015-12-01 10:17
浏览 12
已采纳

Yii - 加入多个表格

i am trying to join 3 tables in my model

trx_evaluation_details

trx_evaluation

rm_projects

trx_evaluation_details and trx_evaluation are already joined by using the relation function in my model.

'eval' => array(self::BELONGS_TO, 'Evaluation', 'eval_id'),

i am trying to join the rm_projects table so that i can access the project_name column in that table so i added this.

'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),

so i have this relation in my model..

public function relations() {
        return array(                
            'eval' => array(self::BELONGS_TO, 'Evaluation', 'eval_id'),
            'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
        );
    }

i tried to access it like this..

if ($search_date_end !== '' && $search_date_start !== '' && $search !== '') {
                    $criteria->condition = "start_date  >= '$search_date_start' 
                    AND end_date <= '$search_date_end' 
                    AND project.project_name like '%$search%'
                    AND t.employee_id = '$employee->company_id'"; 
            }

where i tried project.project_name.. but it is returning an error

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'project.project_name' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT t.id) FROM trx_evaluation_details t LEFT OUTER JOIN trx_evaluation eval ON (t.eval_id=eval.id) WHERE (start_date >= '2015-11-01' AND end_date <= '2015-12-01' AND project.project_name like '%sprobe%'AND t.employee_id = '120069')

which means that it cannot see project.project_name and the table rm_projects is not joined in the returned error.

how can i access the project_name and how can i join the rm_projects table.? please help.

  • 写回答

2条回答 默认 最新

  • dongxing7083 2015-12-04 08:13
    关注

    I have solved this issue.

    in my relations(), i have added this line

    'project' => array(self::HAS_ONE, 'RmProjects', array ('project_id'=>'project_id'), 'through'=> 'eval'),
    

    you can read it in here.. which is in relational query using through http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through

    this joins the models that has a connection that passes through another model. i hope that this answer can help anybody who has the same question as me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害