I have an active record with a couple of useful relations.
Sometimes I only want to access these relational AR’s without doing a find() for the main AR first.
I tried the following but it didn’t work
$model = new Model();
$model->id = 20; // I set the ID so that it knows how to relate
$relation = $model->relation; // here I try to get the relational AR's
$relation ends up being null
However, it does seem to be working for statistical relations. Is it supposed to be working this way, or am I doing something wacky that I shouldn’t be doing?
Did you use gii to generate the model class? If so, can you include it, and/or give more detail on the type of statistical relation you’re trying to model?
Yes, I used gii for this, but I mostly added/modified the relations myself.
I have an AR called Video with the following relation. I can’t retrieve this attribute using the method I mentioned in my first post (however, it does work after doing a find() first for the main AR).
But, there’s another AR called VideoComment with the following statistical relations, and these DO seem to be retrievable just by setting an ID and without doing a find() for the main AR
Well, here is the log, but I can’t make anything out of it. It seems that the lazy loading isn’t doing any mysql queries for ‘videoRatings’.
This is the lazy loading for the ‘videoRatings’ relation.
2010/12/26 09:42:09 [trace] [system.db.ar.CActiveRecord] lazy loading Video.videoRatings
in C:\wamp\www\env5.dev\protected\models\Video.php (50)
in C:\wamp\www\env5.dev\protected\controllers\VideoUserRatingController.php (52)
in C:\wamp\www\env5.dev\index.php (13)
2010/12/26 09:42:09 [trace] [system.db.ar.CActiveRecord] lazy loading Video.videoRatings
in C:\wamp\www\env5.dev\protected\models\Video.php (50)
in C:\wamp\www\env5.dev\protected\models\Video.php (59)
in C:\wamp\www\env5.dev\protected\controllers\VideoUserRatingController.php (53)
And the lazy loading for ‘positiveRatingCount’ and ‘negativeRatingCount’. These do execute a mysql query, and thus work!
2010/12/26 09:41:09 [trace] [system.db.ar.CActiveRecord] lazy loading VideoComment.positiveRatingCount
in C:\wamp\www\env5.dev\protected\controllers\VideoCommentUserRatingController.php (64)
in C:\wamp\www\env5.dev\index.php (13)
2010/12/26 09:41:09 [trace] [system.db.CDbCommand] Querying SQL: SELECT `video_comment_id` AS `c`, COUNT(*) AS `s` FROM `video_comment_user_rating` `t` WHERE (rating=:r) AND (`t`.`video_comment_id`=40) GROUP BY `video_comment_id`
in C:\wamp\www\env5.dev\protected\controllers\VideoCommentUserRatingController.php (64)
in C:\wamp\www\env5.dev\index.php (13)
2010/12/26 09:41:09 [trace] [system.db.ar.CActiveRecord] lazy loading VideoComment.negativeRatingCount
in C:\wamp\www\env5.dev\protected\controllers\VideoCommentUserRatingController.php (65)
in C:\wamp\www\env5.dev\index.php (13)
2010/12/26 09:41:09 [trace] [system.db.CDbCommand] Querying SQL: SELECT `video_comment_id` AS `c`, COUNT(*) AS `s` FROM `video_comment_user_rating` `t` WHERE (rating=:r) AND (`t`.`video_comment_id`=40) GROUP BY `video_comment_id`
in C:\wamp\www\env5.dev\protected\controllers\VideoCommentUserRatingController.php (65)
in C:\wamp\www\env5.dev\index.php (13)
lethargy > Yes, I used gii for this, but I mostly added/modified the relations myself.
I’ve not tried adding the relationships myself; I guess I’m more lethargic than you; I let gii do it for me. As long as I’ve properly created the relationships in the db, gii creates the relations() function flawlessly, and that allows me to access all related tables without doing a second query.
In your MySQL database, model.video_id has a one-to-many-relationship to the video table?
If so, I would recommend running gii to see what it gives you for the relations() method; there is perhaps either an error in your current relations() function, or an error in the way you’ve created relationships in the db.
Thanks for your help & suggestions. I still haven’t figured it out, but who cares I should stop being lazy and use some more DAO’s to speed up my web app anyway. Thanks again…
so i’d like to check whether manager_id has a relation or not, the purpose of doing this is to loop through the attributes and caught one or two attributes if it has a relation, then the special treatment will apply