We know that the active record “with” option (or the corresponding with() method) serve to join the related table in a relation in one join statement. Now i don’t understand the existance of the “together” option also, i mean from what i understood it is the same thing so redundant. Can anyone explain me how these two option related each other and specifically what does each one?
The way I understand it is that together overrides with() for special cases. When tables are joined using with(), that may generate a lot of redundant data (one user record joined with 100 post records will generate the same user data 100 times with a LEFT OUTER JOIN as used by default in Yii). if you set together to false, the data will still be eagerly loaded, but the queries will be separated to reduce this overhead.
/**
* Uses the most aggressive join approach.
* By default, several join statements may be generated in order to avoid
* fetching duplicated data. By calling this method, all tables will be joined
* together all at once.
* @param boolean whether we should enforce join even when a limit option is placed on the primary table query.
* Defaults to true. If false, we would still use two queries when there is a HAS_MANY/MANY_MANY relation and
* the primary table has a LIMIT option. This parameter is available since version 1.0.3.
* @return CActiveFinder the finder object
* @since 1.0.2
*/
public function together($ignoreLimit=true)
{
$this->joinAll=true;
if($ignoreLimit)
$this->baseLimited=false;
return $this;
}
joinAll is then used in buildQuery() and count().
Try profiling SQL generated when using limit.
So it’s to JOIN all tables at once even if there are duplicates.