Answer the question
In order to leave comments, you need to log in
How to fasten nested relations with one Yii request?
So:
There is a table of games (games)
There is a table of applications for the game (proposal)
There is a table waiting to play (waiting)
games
public function relations()
{
return array(
'proposal' => array(self::HAS_MANY, 'GameProposalModel', '', 'on'=>'t.game_id=proposal.game_id'),
);
}
public function relations()
{
return array(
'waiting' => array(self::HAS_MANY, 'GameWaitingModel', '', 'on'=>'t.proposal_id=waiting.proposal_id'),
);
}
$criteria = new CDbCriteria;
$criteria->select = 't.game_id, t.game_name, t.alter_name';
$criteria->condition = "t.game_id=:game_id AND proposal.status=-1";
$criteria->params = array(':game_id'=>$game_id);
$criteria->with = array(
'proposal' => array('joinType'=>'LEFT JOIN'),
);
$data = GamesModel::model()->find($criteria);
Answer the question
In order to leave comments, you need to log in
If there is no request, then false is returned.
$criteria->with = array(
'proposal' => array('joinType'=>'LEFT JOIN'),
'proposal.waiting',
);
Added 1 more relation in the table of games: waiting
public function relations()
{
return array(
//'rule' => array(self::HAS_MANY, 'GamesRuleModel', '', 'on'=>'t.game_id=rule.game_id'),
'proposal' => array(self::HAS_MANY, 'GameProposalModel', '', 'on'=>'t.game_id=proposal.game_id'),
'waiting' => array(self::HAS_MANY, 'GameWaitingModel', '', 'on'=>'proposal.proposal_id=waiting.proposal_id'),
);
}
$criteria->with = array(
'proposal' => array('joinType'=>'LEFT OUTER JOIN'),
'waiting.proposal',
);
$game_id = 1;
$criteria = new CDbCriteria;
$criteria->select = 't.game_id, t.game_name, t.alter_name';
$criteria->condition = "t.game_id=:game_id AND proposal.status=-1";
$criteria->params = array(':game_id'=>$game_id);
$criteria->together = true;
$criteria->with = array(
'proposal' => array('joinType'=>'LEFT OUTER JOIN'),
'waiting',
);
$data = GamesModel::model()->find($criteria);
echo '<pre>';
print_r($data);
echo '</pre>';
GamesModel Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[game_id] => 1
[game_name] => Клабор (Деберц)
[alter_name] => deberc
)
[_related:CActiveRecord:private] => Array
(
[proposal] => Array
(
[0] => GameProposalModel Object
(
[user_rules] =>
[waiting_rules] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[proposal_id] => 114
[game_id] => 1
[user_id] => 234
[date] => 2014-01-13 17:10:28
[status] => -1
[rules] => [{"rule_id":"1","param":"30"},{"rule_id":"2","param":"0"},{"rule_id":"3","param":"4"},{"rule_id":"4","param":"301"},{"rule_id":"5","param":"0"}]
[hash] => 61fe96c3d7291c7c04c6fcb71b8b49de
[count_player] => 4
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 114
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => GameProposalModel Object
(
[user_rules] =>
[waiting_rules] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[proposal_id] => 113
[game_id] => 1
[user_id] => 500
[date] => 2014-01-13 17:07:26
[status] => -1
[rules] => [{"rule_id":"1","param":"60"},{"rule_id":"2","param":"1"},{"rule_id":"3","param":"4"},{"rule_id":"4","param":"501"},{"rule_id":"5","param":"0"}]
[hash] => 750da4a838006f676f0e0808ded1ac74
[count_player] => 4
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 113
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[2] => GameProposalModel Object
(
[user_rules] =>
[waiting_rules] =>
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[proposal_id] => 112
[game_id] => 1
[user_id] => 234
[date] => 2014-01-13 08:03:36
[status] => -1
[rules] => [{"rule_id":"1","param":"30"},{"rule_id":"2","param":"0"},{"rule_id":"3","param":"2"},{"rule_id":"4","param":"301"},{"rule_id":"5","param":"0"}]
[hash] => b0105dc5a8908f82cb858987ea0cac50
[count_player] => 2
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 112
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
[waiting] => Array
(
[0] => GameWaitingModel Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[waiting_id] => 182
[proposal_id] => 114
[user_id] => 234
[sort] => 0
[root] => 1
[hash] => 61f7b277d2cb69b7af12bc0ab90dd814
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 182
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[1] => GameWaitingModel Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[waiting_id] => 183
[proposal_id] => 113
[user_id] => 231
[sort] => 0
[root] => 1
[hash] => 61f7b277d2cb69b7af12bc0ab90dd814
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 183
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[2] => GameWaitingModel Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[waiting_id] => 184
[proposal_id] => 112
[user_id] => 232
[sort] => 0
[root] => 1
[hash] => 61f7b277d2cb69b7af12bc0ab90dd814
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 184
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
// транзакция
$data['game'] = .... findByPK($game_id);
if($data['game'])
{
$data['proposal'] = ... findByAttibutes(array(...)) ;
$data['waiting'] = ... findByAttibutes(array(...));
}
$transaction->commit();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question