togheter and with options...

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?

anyone?

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.

mmm i will wait for a more clear answer…maybe from qjang :)

http://www.yiiframework.com/doc/api/CActiveFinder#together-detail

Since together is a method of CActiveFinder, usage should be like this:




Post::model()->findAll('y')->with('x')->together();



Yes but i still don’t understand the usage of both methods (with() and together()) at the same time

You can’t use together without with. The difference is SQL generated.

Ok so i return to my first question: What is the meaning of:

Post::model()->findAll()->with(‘Comments’)->together()

…knowing that the with() method alone already produce a single join statement?




	/**

	 * 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.

If someone’s playing around with it: I’d love to see two examples of generated SQL that show the difference. :)

ok anyway now with() will use a single join by default so we don’t have to “force” with toghether to true

btw., it’s already described in SVN docs: http://yiiframework.ru/doc/guide/en/database.arr (see Relational Query Performance)

No need for a new topic I guess so I’ll ask here.

Is it possible to define a default "with"-option? Example:


$items = Posts::model()->mostDiscussed()->with('category')->findAll();

I want it like this:


$items = Posts::model()->mostDiscussed()->findAll();

This doesn’t work for some reason:


	

	public function defaultScope()

	{

		return array(

			'with' => 'category',

		);

	}

Anyone? :huh: