I have created a dropdownlist which has a value and option of company_name what i want to do is fill another field with the Broker_id based on the selection of the dropdownlist.
i have tried to create a query to select Broker_id from the Broker_details table where = company name and then feed this value to the field before i save the record.
<?php
public function beforeSave()
{
$Broker_Name = $this->Broker_Name; //This takes the Broker Name from the view and works fine.
$Broker = BrokerDetails::model()->findAll(array('select' => 'Broker_Id'),$Broker_Name); //i'm having real trouble figuring out how to create my query.
$this->Broker_Id=$Broker; //This updates fine as well when i use $Broker_Name it inserts the broker name into the field.
return parent::beforeSave();
}?>
I have 2 models brokerdetails and clientdetails. In the form for clientdetails I have created a dropdownlist which pulls the company_name from the brokerdetails model and generates the dropdownlist with company_name as the value and as the option so the user can select the company name and the company name will be stored in the client_details table. What I am trying to do is when the user selects the company_name before it is saved it takes that company_Name and enters the broker_I’d of that company from the brokerdetails model into the broker_Id field of the client_details table.
Maybe im not explaining myself properly i want to do both. my clientdetails form has company_name and Broker_id both are available from my brokerdetails model in the case of company_name i created the dropdownlist and it works perfectly.
But rather than creating a second dropdownlist i want the broker_id field populated automatically based on the users selection of the company_name dropdownlist selection.
Ill post up my code tomorrow as im trying to reply here on my phone.
To Start again what i want to do is when the user selects the company_name from a dropdownlist to store it in the client_details table it will automatically store the relevant broker_id in the broker_id field of the client_details table, I have thought i would use a query that would "select broker_id from Broker_Details where company_name = the variable from the form in this case $Broker_Name from my clientdetails model" this query works fine in my sql terminal of mysql workbench.
So here’s my relavant code.
<?php
Yii::import('application.models._base.BaseClientDetails');
class ClientDetails extends BaseClientDetails
{
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function beforeSave()
{
if ($this->isNewRecord)
$this->Created = new CDbExpression('NOW()');
else
$this->Last_UpDate = new CDbExpression('NOW()');
$Broker_Name = $this->Broker_Name; //this works fine it assigns the company name selected to the variable $broker_Name
$Broker = BrokerDetails::model()->findAll(array('select' => 'Broker_Id'),$Broker_Name); //I'm aware that this query is totally wrong, The Brokerdetails model here is a model of the table broker_details of which i require the company_name and the Broker_id. Broker_id is the primary key of the table. i have no code in brokerdetails model that relate to this problem.
$this->Broker_Id=$Broker; //this works fine
return parent::beforeSave();
}
}
?>
As you can see i have commented out the form section that relates to the broker_id this is a back up where if i can’t automate the broker_id input ill revert to the user selecting the Company name and the broker_id being stored to the table via a dropdownlist.
Anyways i hope anybody can help and i appreciate the help.
In my BeforeSave() i added this which returns the broker_id based on the Company_name then i assigned it to the Broker_Id Field in my ClientDetails form.
public function relations()
{
return array(
'broker'=>array(self::BELONGS_TO, 'BrokerDetail', 'broker_id'),
);
}
(in application.models._base.BaseClientDetails), you can access directly to $model->broker related object (and $model->broker->broker_id, $model->broker->broker_name …)