ActiveRecord and Relational Active Record

I selected Yii for a new project after researching online for about a week – been completely satisfied with the decision, this is a well thought-out and feature-rich framework (I’ve used Django, Ruby on Rails and Struts in the past, so it was a breeze to pick up Yii’s MVC structure).

I’ve run into an interesting issue when using the Relational Active Record feature.

Background:

I have two tables that I’d like to marge with a left outer join (obviously on the ‘reward_id’ column):


"rewards"


+---------------+--------------+------+-----+---------+----------------+

| Field         | Type         | Null | Key | Default | Extra          |

+---------------+--------------+------+-----+---------+----------------+

| id            | int(11)      | NO   | PRI | NULL    | auto_increment | 

| checkpoint_id | int(11)      | NO   | MUL | NULL    |                | 

| souvenir_id   | int(11)      | NO   | MUL | NULL    |                | 

| message       | varchar(250) | NO   |     | NULL    |                | 

+---------------+--------------+------+-----+---------+----------------+


"user_rewards"


+------------+-----------+------+-----+-------------------+----------------+

| Field      | Type      | Null | Key | Default           | Extra          |

+------------+-----------+------+-----+-------------------+----------------+

| id         | int(11)   | NO   | PRI | NULL              | auto_increment | 

| user_id    | int(11)   | NO   | MUL | NULL              |                | 

| reward_id  | int(11)   | NO   | MUL | NULL              |                | 

| earned_dts | timestamp | NO   |     | CURRENT_TIMESTAMP |                | 

+------------+-----------+------+-----+-------------------+----------------+

These are in MySQL using the InnoDB storage engine and the appropriate foreign key relationship is defined. Using the yiic shell I’ve created models named “rewards.php” and “user_rewards.php”

Based on the instructions in the Definitive Guide, I wrote the following code:


$condition = "checkpoint_id=:checkpoint_id and user_id = :user_id";

$params = array(":checkpoint_id" => $checkpoint_id,

                ":user_id" => $user_id);

    

if ($reward = rewards::model()->with('user_rewards')->find( array('condition' => $condition, 'params' => $params,) ) )

               $ret_data["reward"] = $reward;

However, when attempting to execute, I get the following error (truncated for brevity):


2010/10/14 18:10:36 [error] [php] YiiBase::include(UserRewards.php) [<a href='function.YiiBase-include'>function.YiiBase-include</a>]: failed to open stream: No such file or directory (/usr/local/yii-1.1.4.r2429/framework/YiiBase.php:341)

Stack trace:

#0 /usr/local/yii-1.1.4.r2429/framework/db/ar/CActiveRecord.php(352): spl_autoload_call()

#1 /usr/local/yii-1.1.4.r2429/framework/db/ar/CActiveFinder.php(185): model()

...

So (at last) here’s the question:

Is there a way to explicitly name the module required and bypass the Camel-case translation?

When using single-table AR queries [i.e. user_rewards::model()-&gt;findByPk(…) ] there is no problem. It’s only when I include a model in the ‘with’ clause.

Any thoughts? Did I miss something in the doc?

Just change the relation in relations function in rewards model file.

From something like this:

‘user_rewards’ => array(self::HAS_MANY, ‘UserRewards’, ‘reward_id’),

to

‘user_rewards’ => array(self::HAS_MANY, ‘user_rewards’, ‘reward_id’),

Quick note:

When you create a forum post you can select the text and click the “<>” icon in the editor to get code highlighting / mono space font. You can also edit your post above and fix this… ;)

That did the trick – thanks! (And I updated the formatting as well)

it works for the author, but i am in the opposite situation:

change user_rewards => UserRewards

so, which option determine this feature?