N
N
nepster-web2014-01-13 06:33:53
Yii
nepster-web, 2014-01-13 06:33:53

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'),
        );
    }

proposal
public function relations()
    {
        return array(
            'waiting' => array(self::HAS_MANY, 'GameWaitingModel', '', 'on'=>'t.proposal_id=waiting.proposal_id'),
        );
    }

There is a task to make such a request to get the following:
Game data
All requests with status -1
All users who are pending
I started something like this:
$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);

And immediately ran into 2 jambs:
If there is no application, then false is returned.
How can I add another nested waiting relation?
Tell me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladlen Grachev, 2014-01-13
@gwer

If there is no request, then false is returned.

Checking for false in the returned value.
If I understand correctly what you need, then something like this:
$criteria->with = array(
    'proposal' => array('joinType'=>'LEFT JOIN'),
    'proposal.waiting',
);

N
nepster-web, 2014-01-13
@nepster-web

CDbCommand failed to execute SQL query

N
nepster-web, 2014-01-13
@nepster-web

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',
        );

Now we make a request:
$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>';

[b]Object pops up:[/b]
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] => 
)

Now let's figure out what we have here:
1) Data about the game, in this case, for the test, I got 3 parameters. IDs and names
2) Applications for the game proposals
3) Users who are waiting for the game waiting
So how this whole thing looks in practice:
We have an event server that makes a request for such an object:
Having received the object, Javascript draws a table of applications according to proposals and waiting
And also, according to the game data, it updates the statistics of the game (for example, how many games were played per day, the number of players online, etc.)
Now the moments:
- if there is not one waiting (no waiting records), everything is fine, the waiting object comes empty . Of course, this cannot be, but I check it on the test.
- further, for example, more players joined these three applications, the applications received a different status (-1 pending, 1 the game is in progress, 0 the game is over, -2 the game did not take place) and, as it were, there are no applications.
a request is made to the server, and if there are no applications, then emptiness is returned. In this case, I will draw a table with JavaScript, with the inscription there are no applications. But how to update the statistical data of the game if a void has come?
That is, the task is to make it so that if there is no proposal, just the game object comes.
// транзакция 

$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 question

Ask a Question

731 491 924 answers to any question