Yii Framework Forum: Joins Are Back - Yii Framework Forum

Jump to content

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

Joins Are Back sry guys I cannot hold my tongue

#1 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,688
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 07 January 2014 - 03:18 PM

For those who haven't noticed yet, INNER/LEFT joins for relations are back!

Moreover, now you can do some complex joins like
parent LEFT JOIN child ON parent.id = child.parent_id AND child.some_field = 'some_value'

and even self-joins/multiple joins to another table using table aliases.

This is quite important thing for those of us who deals with complex datasets and still wants to use all the power of AR/AQ.
It allows you to do filtering based on related model's fields without using extra join() call, thus keeping code DRY.

As far as I know almost every modern php framework (including Phalcon) does not include this feature for some reason (the only FW that now has powerful ORM is CakePHP 3.0, which is in deep pre-alpha).

So now your code can look like this:

$query = MyModel::find()
    ->joinWith(['relation1', 'relation2']); // relation1 and relation2 are joined to MyModel and loaded eagerly

$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 15]);

$items = $query
    ->orderBy($sort->orders)
    ->offset($pages->offset)
    ->limit($pages->limit)
    ->with(['relation3', 'relation4']) // we also want to eagerly load relation3 and relation4, but we don't need to filter on them
    ->all();


Here is documentation on that.

Thanks guys from dev team, you made my day (week?..)!
God is real unless declared as integer
2

#2 User is offline   Kartik V 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 579
  • Joined: 29-August 12

Posted 07 January 2014 - 03:56 PM

View PostORey, on 07 January 2014 - 03:18 PM, said:

As far as I know almost every modern php framework (including Phalcon) does not include this feature for some reason (the only FW that now has powerful ORM is CakePHP 3.0, which is in deep pre-alpha).

Here is documentation on that.

That's a great update B).
0

#3 User is offline   samdark 

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

Posted 07 January 2014 - 06:43 PM

The best part of it is that you can get JOIN from SQL and noSQL in a single AR "query".

Thanks for posting btw. very well written.
Yii 1.1 Application Development Cookbook

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

#4 User is offline   Davey 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 14-September 11

Posted 08 January 2014 - 09:46 AM

I also noticed the update, very nice! A few weeks ago i posted a question about this here, so i'm very happy you guys implemented this! Thanks!

Edit:
Quick question though, is it possible to use joinWith() with an extra condition, so i can perform a join like this (i mean the 'AND photoType = 1' part):

LEFT JOIN photo ON user.id = photo.userId AND photoType = 1

0

#5 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 08 January 2014 - 11:39 AM

@Davey: yes. Use onCondition(). Please see the doc at https://github.com/y...-with-relations
0

#6 User is offline   Davey 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 14-September 11

Posted 09 January 2014 - 03:28 AM

View Postqiang, on 08 January 2014 - 11:39 AM, said:

@Davey: yes. Use onCondition(). Please see the doc at https://github.com/y...-with-relations


Great, thanks!
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