Using variable across 2 models

Hi,

I’m currently trying to pass a function using variables from two different model.

The model SystemEmails is using two variables I need


CONST USERNAME = ':email_username';

 CONST PASSWORD = ':email_password';

I need to pass these details to cpanel alongside the other details I’m sending.

The relationship is set up like so in systememail.


  public function relations() {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'sysSystem' => array(self::BELONGS_TO, 'System', 'sys_system_id'),

        );

    }

And inside the system model I’m trying to get the system email like so




  public function relations() {

// NOTE: you may need to adjust the relation name and the related

// class name for the relations automatically generated below.

        return array(

'email' => array(self::BELONGS_TO, 'SystemEmail', 'id')

}

and in my function


 $email_user = $emailModel->email_username;

$email_password = $emailModel->email_password;

I have also tried


//$email_user = SystemEmail::USERNAME;

//$email_password = SystemEmail::PASSWORD;

Hello.

First of all, it’s advisable to call the related variables by the same name.

So, SystemEmail->sys_sistem_id and System->sys_sistem_id.

You don’t need to pass variables between models.

SystemEmail model




public function relations() 

{

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'sysSystem' => array(self::BELONGS_TO, 'System', 'sys_system_id'),

        );




System model




public function relations() {

// NOTE: you may need to adjust the relation name and the related

// class name for the relations automatically generated below.

        return array(

'email' => array(self::HAS_ONE, 'SystemEmail', 'sys_system_id')

}



Then, you can show this data as follows:




$systemEmailModel = SystemEmail::model()->findByAttributes('sys_system_id' => 'somedata');


echo($systemEmailModel->email_username);

echo($systemEmailModel->sysSystem->propertyName);// Change property name for an existing property name.








$systemModel = System::model()->findByPk('somedata');


echo($systemModel->SystemEmail->email_username);

echo($systemModel->propertyName);// Change property name for an existing property name.



Regards.