Yii Framework Forum: togheter and with options... - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

togheter and with options... Rate Topic: -----

#1 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 03 December 2009 - 06:17 AM

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?
0

#2 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 21 December 2009 - 12:31 PM

View Postskyblaze, on 03 December 2009 - 06:17 AM, said:

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?
0

#3 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 22 December 2009 - 03:39 AM

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

#4 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 22 December 2009 - 10:28 AM

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

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 23 December 2009 - 06:46 AM

http://www.yiiframew...together-detail

Quote

Uses the most aggressive join approach. By calling this method, even if there is LIMIT/OFFSET option set for the primary table query, we will still use a single SQL statement. By default (without calling this method), the primary table will be queried by itself so that LIMIT/OFFSET can be correctly applied.


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

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

Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#6 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 24 December 2009 - 08:24 AM

View Postsamdark, on 23 December 2009 - 06:46 AM, said:

http://www.yiiframew...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
0

#7 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 24 December 2009 - 08:27 AM

You can't use together without with. The difference is SQL generated.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#8 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 24 December 2009 - 10:04 AM

View Postsamdark, on 24 December 2009 - 08:27 AM, said:

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?
0

#9 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 25 December 2009 - 03:32 AM

	/**
	 * 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.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 December 2009 - 11:18 AM

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

#11 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 25 December 2009 - 04:30 PM

View Postsamdark, on 25 December 2009 - 03:32 AM, said:

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

ok anyway now with() will use a single join by default so we don't have to "force" with toghether to true
0

#12 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,788
  • Joined: 17-January 09
  • Location:Russia

Posted 27 December 2009 - 04:29 PM

btw., it's already described in SVN docs: http://yiiframework....en/database.arr (see Relational Query Performance)
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#13 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 27 December 2009 - 11:27 PM

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:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users