yii 多表联合查询的几种方法

本文深入探讨了使用Yii框架进行多表联合查询的方法,包括手动拼接SQL语句和利用Active Record (AR)类的查询方法。详细介绍了BaseActiveRecord类的使用,特别强调了在联表查询中如何处理字段名称冲突,并对比了使用AR的relation关联与直接使用queryAll或queryRow方法的区别。此外,文章指出在实际应用中应当避免过度依赖关系关联,以提高代码的可读性和维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

yii多表联合查询,

第一种,用command,自己拼接sql语句执行查询

 

第二种,用AR,model需继承下面的ar,执行queryall或queryrow方法

<?php
//application/components/BaseActiveRecord.php
class BaseActiveRecord extends CActiveRecord{

    /**
     * Returns the static model of the specified AR class.
     * The model returned is a static instance of the AR class.
     * It is provided for invoking class-level methods (something similar to static class methods.)
     *
     * <pre>
     * public static function model($className=__CLASS__)
     * {
     *     return parent::model($className);
     * }
     * </pre>
     *
     * @param string $className active record class name.
     * @return DBActiveRecord active record model instance.
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    
    /**
     * 获取字段rawName加表别名前缀,主要联表时候防止where条件中字段冲突用的
     * @param string $columnName
     * @return string
     */
    public function getColumnRawName($columnName)
    {
        $prefix = $this->getTableAlias(true) . '.';
        $columns = $this->tableSchema->columns;
        if (isset($columns[$columnName]))
        {
            return $prefix.$columns[$columnName]->rawName;
        }
        else
        {
            return $columnName;
        }
    }
    
    /**
     * 
     * @param mixed $criteria
     */
    public function queryAll($criteria = NULL)
    {
        if ( ! empty($criteria))
        {
            $this->getDbCriteria()->mergeWith($criteria);
        }
        
        $result = $this->getCommandBuilder()
            ->createFindCommand($this->tableSchema, $this->getDbCriteria())
            ->queryAll();
        
        $this->setDbCriteria(NULL);
        
        return $result;
    }
    
    public function queryRow($criteria = NULL)
    {
        if ($criteria != NULL)
        {
            $this->getDbCriteria()->mergeWith($criteria);
        }
        
        $result = $this->getCommandBuilder()
            ->createFindCommand($this->tableSchema, $this->getDbCriteria())
            ->queryRow();
        
        $this->setDbCriteria(NULL);
        
        return $result;
    }
    
    public function compare($column, $value, $partialMatch = FALSE, $operator = 'AND')
    {
        $criteria = new \CDbCriteria;
        $column = $this->getColumnRawName($column);
        
        if ($value === array())
        {
            $criteria->condition = "1 = 0";
        }
        else if ($value === '')
        {
            $criteria->condition = $column." = ''";
        }
        else
        {
            $criteria->compare($column, $value, $partialMatch, $operator, TRUE);
        }
        
        $this->getDbCriteria()->mergeWith($criteria);
        
        return $this;
    }
    
    
}

还有一种就是用ar的relation,做的关联容易忘记,这种不好用!

转载于:https://www.cnblogs.com/jami918/p/3757472.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值