0 follower

CJoinQuery

Package system.db.ar
Inheritance class CJoinQuery
Since 1.0
Source Code framework/db/ar/CActiveFinder.php
CJoinQuery represents a JOIN SQL statement.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
conditions array list of WHERE clauses CJoinQuery
distinct boolean whether to select distinct result set CJoinQuery
elements array list of join element IDs (id=>true) CJoinQuery
groups array list of GROUP BY clauses CJoinQuery
havings array list of HAVING clauses CJoinQuery
joins array list of join statement CJoinQuery
limit integer row limit CJoinQuery
offset integer row offset CJoinQuery
orders array list of ORDER BY clauses CJoinQuery
params array list of query parameters CJoinQuery
selects array list of column selections CJoinQuery

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Constructor. CJoinQuery
createCommand() Creates the SQL statement. CJoinQuery
join() Joins with another join element CJoinQuery

Property Details

conditions property
public array $conditions;

list of WHERE clauses

distinct property
public boolean $distinct;

whether to select distinct result set

elements property
public array $elements;

list of join element IDs (id=>true)

groups property
public array $groups;

list of GROUP BY clauses

havings property
public array $havings;

list of HAVING clauses

joins property
public array $joins;

list of join statement

limit property
public integer $limit;

row limit

offset property
public integer $offset;

row offset

orders property
public array $orders;

list of ORDER BY clauses

params property
public array $params;

list of query parameters

selects property
public array $selects;

list of column selections

Method Details

__construct() method
public void __construct(CJoinElement $joinElement, CDbCriteria $criteria=NULL)
$joinElement CJoinElement The root join tree.
$criteria CDbCriteria the query criteria
Source Code: framework/db/ar/CActiveFinder.php#1282 (show)
public function __construct($joinElement,$criteria=null)
{
    if(
$criteria!==null)
    {
        
$this->selects[]=$joinElement->getColumnSelect($criteria->select);
        
$this->joins[]=$joinElement->getTableNameWithAlias();
        
$this->joins[]=$criteria->join;
        
$this->conditions[]=$criteria->condition;
        
$this->orders[]=$criteria->order;
        
$this->groups[]=$criteria->group;
        
$this->havings[]=$criteria->having;
        
$this->limit=$criteria->limit;
        
$this->offset=$criteria->offset;
        
$this->params=$criteria->params;
        if(!
$this->distinct && $criteria->distinct)
            
$this->distinct=true;
    }
    else
    {
        
$this->selects[]=$joinElement->getPrimaryKeySelect();
        
$this->joins[]=$joinElement->getTableNameWithAlias();
        
$this->conditions[]=$joinElement->getPrimaryKeyRange();
    }
    
$this->elements[$joinElement->id]=true;
}

Constructor.

createCommand() method
public CDbCommand createCommand(CDbCommandBuilder $builder)
$builder CDbCommandBuilder the command builder
{return} CDbCommand DB command instance representing the SQL statement
Source Code: framework/db/ar/CActiveFinder.php#1340 (show)
public function createCommand($builder)
{
    
$sql=($this->distinct 'SELECT DISTINCT ':'SELECT ') . implode(', ',$this->selects);
    
$sql.=' FROM ' implode(' ',array_unique($this->joins));

    
$conditions=array();
    foreach(
$this->conditions as $condition)
        if(
$condition!=='')
            
$conditions[]=$condition;
    if(
$conditions!==array())
        
$sql.=' WHERE (' implode(') AND (',$conditions).')';

    
$groups=array();
    foreach(
$this->groups as $group)
        if(
$group!=='')
            
$groups[]=$group;
    if(
$groups!==array())
        
$sql.=' GROUP BY ' implode(', ',$groups);

    
$havings=array();
    foreach(
$this->havings as $having)
        if(
$having!=='')
            
$havings[]=$having;
    if(
$havings!==array())
        
$sql.=' HAVING (' implode(') AND (',$havings).')';

    
$orders=array();
    foreach(
$this->orders as $order)
        if(
$order!=='')
            
$orders[]=$order;
    if(
$orders!==array())
        
$sql.=' ORDER BY ' implode(', ',$orders);

    
$sql=$builder->applyLimit($sql,$this->limit,$this->offset);
    
$command=$builder->getDbConnection()->createCommand($sql);
    
$builder->bindValues($command,$this->params);
    return 
$command;
}

Creates the SQL statement.

join() method
public void join(CJoinElement $element)
$element CJoinElement the element to be joined
Source Code: framework/db/ar/CActiveFinder.php#1312 (show)
public function join($element)
{
    if(
$element->slave!==null)
        
$this->join($element->slave);
    if(!empty(
$element->relation->select))
        
$this->selects[]=$element->getColumnSelect($element->relation->select);
    
$this->conditions[]=$element->relation->condition;
    
$this->orders[]=$element->relation->order;
    
$this->joins[]=$element->getJoinCondition();
    
$this->joins[]=$element->relation->join;
    
$this->groups[]=$element->relation->group;
    
$this->havings[]=$element->relation->having;

    if(
is_array($element->relation->params))
    {
        if(
is_array($this->params))
            
$this->params=array_merge($this->params,$element->relation->params);
        else
            
$this->params=$element->relation->params;
    }
    
$this->elements[$element->id]=true;
}

Joins with another join element