XF 2.2 relations

Robert9

Well-known member
Code:
public function findEventsByLeagueId($leagueId)
    {
        return $this->finder('Lala:Event')
        ->with('League')
        ->with('TeamHome')
        ->with('TeamAway')
        ->where(['league_id', $leagueId]);
    }

The relation with the same field_name league_id in both tables works fine.
But the two other relations TeamHome and TeamAway return empty values


Code:
$structure->relations = [
            'League' => [
                'entity' => 'Lala:League',
                'type' => self::TO_ONE,
                'conditions' => 'league_id',
                'primary' => true,
            ],
            'TeamHome' => [
                'entity' => 'Lala:Team',
                'type' => self::TO_ONE,
                'conditions' => [
                                    ['server_team_id', '=', 'team_home_id'],
                                ],
            ],
            'TeamAway' => [
                'entity' => 'Lala:Team',
                'type' => self::TO_ONE,
                'conditions' => [
                                    ['server_team_id', '=', 'team_away_id'],
                                ],
            ],

       ],

I have double checked the values; all data is there, also the query seems to be correct, but there is no result.

Is there a way to show the real query?
 
Ok. Problem is that
'conditions' => [
['server_team_id', '=', 'team_away_id'],
],

is translated to LEFTJOIN ... ON table.server_team_id = 'team_away_id'

instead of

is translated to LEFTJOIN ... ON table1.server_team_id = table2.team_away_id


How can i say:

ON table1.server_team_id = table2.team_away_id
 
Ok. Problem is that
'conditions' => [
['server_team_id', '=', 'team_away_id'],
],

is translated to LEFTJOIN ... ON table.server_team_id = 'team_away_id'

instead of

is translated to LEFTJOIN ... ON table1.server_team_id = table2.team_away_id


How can i say:

ON table1.server_team_id = table2.team_away_id

You can user the second param of with() as true , default is false for must exist.
 
Solved:

Code:
                'conditions' => [
                                    ['server_team_id', '=', '$team_home_id'],
                                ],

this is translated to
LEFT JOIN xf_lala_team AS xf_lala_team_TeamAway_3 ON (xf_lala_team_TeamAway_3.server_team_id = xf_lala_event.team_home_id)

If you want table1.field1 = table2.field2 then you need '$field2'
 
Top Bottom