Scopes Relations And Other Tricky Bits

I have the following tables

Role

(id int…

,name …

);

User

(id

,name

,role_id (foreign key to Role.id)

)

Partner

(id

,acct_mgr (foreign key to user.id)

)

I defined scopes for Role

public function scopes()

	{


    


	return array(


      'acctmgr'=>array('condition'=>"role='Account Manager'",),


    );


    


}

I defined scopes for User

public function scopes()

{

return array(


      'acctmgrs'=>array( 'condition'=>"role.role='Account Manager'", 'with' => 'role', ), /* works */

/* does not work --> ‘acctmgrs’=>array( ‘condition’=>“role_id=”.Role::model()->acctmgr()->find(), ), */

    );

}

In the _form (create page for the Partner model) I have the following:

<div class="row">

        &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'acct_mgr_id'); ?&gt;


        &lt;?php echo &#036;form-&gt;dropDownList(&#036;model, 'acctMgr', CHtml::listData(User::model()-&gt;acctmgrs()-&gt;findAll(),'id', 'username'), array('prompt' =&gt; 'Select Account Manager')); ?&gt;


        &lt;?php echo &#036;form-&gt;error(&#036;model,'acct_mgr_id'); ?&gt;


    &lt;/div&gt;

But it blows up

Should I be able to define the scope as

‘acctmgrs’=>array( ‘condition’=>“role_id=”.Role::model()->acctmgr()->find(), ),

Get the set of Account Manager Roles (could be one or more) and then get the set of Users who belong to that role.

I am missing something very basic and confused.

Thanks for any help.

Hi,

please use "insert code snippet" button for your code, it will be more readable.

What does this mean, what is the error ?

Thanks for the tip on the code snippet…

I was thinking I should be able to select the row (or rows) for the Role named ‘Account Manager’ then grab the rows from User

for all the users that belong to that role. I thought using scopes would allow me to do that but it didn’t work

the way I expected.

In my BaseUser model file I have…


public function relations() {

        return array(

            'demandpartners' => array(self::HAS_MANY, 'Demandpartner', 'acct_mgr_id'),

            'demandpartners1' => array(self::HAS_MANY, 'Demandpartner', 'acct_id'),

            'role' => array(self::BELONGS_TO, 'Role', 'role_id'),

            'websites' => array(self::HAS_MANY, 'Website', 'leadowner_id'),

            'websites1' => array(self::HAS_MANY, 'Website', 'network_coord_id'),

            'websiteAudits' => array(self::HAS_MANY, 'WebsiteAudit', 'user_id'),

        );

    }



In the partner model I have the following relations


 public function relations() {

        return array(

            

            'acctMgr' => array(self::BELONGS_TO, 'User', 'acct_mgr_id'),

            'partnerType' => array(self::BELONGS_TO, 'Partnertype', 'partner_type'),

            'acct' => array(self::BELONGS_TO, 'User', 'acct_id'),

            

        );

    }

Errors I received…

CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘Buyer’ in ‘where clause’. The SQL statement executed was: SELECT * FROM user t WHERE role_id=Buyer


/* does not work --> 'acctmgrs'=>array( 'condition'=>"role_id=".Role::model()->acctmgr()->find(), ), */

could the role_id be affected with a Role::model()->acctmgr()->find() object ?

I expected the acctmgr() scope to filter the set of data from the Role model…but system did not like it.

If there is more than one Role returned by the filter then role_id= might give an error…but there should only be one

based on my data.

I think that doesn’t matter, Role::model()->acctmgr()->find() is still an object.

But are you satisfied with :


'acctmgrs'=>array( 'condition'=>"role.role='Account Manager'", 'with' => 'role', ), /* works */

Thanks Luc,

I am ok with it, it just didn’t seem to make sense that it did not work the other way…I am still learning much

and questioning all that I know.