Yii Framework Forum: Scopes Relations And Other Tricky Bits - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Scopes Relations And Other Tricky Bits Rate Topic: -----

#1 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 14 November 2012 - 05:00 PM

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">
<?php echo $form->labelEx($model,'acct_mgr_id'); ?>
<?php echo $form->dropDownList($model, 'acctMgr', CHtml::listData(User::model()->acctmgrs()->findAll(),'id', 'username'), array('prompt' => 'Select Account Manager')); ?>
<?php echo $form->error($model,'acct_mgr_id'); ?>
</div>

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.
0

#2 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 14 November 2012 - 05:16 PM

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

Quote

But it blows up


What does this mean, what is the error ?
Hey Ho !
Let's go !
0

#3 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 15 November 2012 - 05:06 PM

View Postluc, on 14 November 2012 - 05:16 PM, said:

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
0

#4 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 16 November 2012 - 04:19 AM

/* 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 ?
Hey Ho !
Let's go !
0

#5 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 19 November 2012 - 04:52 PM

View Postluc, on 16 November 2012 - 04:19 AM, said:

/* 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.
0

#6 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 20 November 2012 - 04:50 AM

Quote

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 */

Hey Ho !
Let's go !
0

#7 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 30 November 2012 - 10:03 PM

View Postluc, on 20 November 2012 - 04:50 AM, said:

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users