Create query from Dropdownlist value [SOLVED]

Hello Once again

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();

}?>

Anyways i hope you can help

Ibecake

Hi,

looks like you have a Broker Model and an another called BrokerDetail.

Did you declare relations between these two models, it will save you many hassels ?

Did your dropdown have the broker_id for option and the broker_name as value ? (It seems to me that this is not the case) ?

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.

I have no relations set up.

Probably made it worse with my explanation.

Ibecake

It’s ambiguous:

firstly, you say that you want to store company_name in the client_detail table and after you say that it is borker_id …

(second option is defintively better, because of the power of active record see below)

In your form create the dropdown like this:




<?php echo $form->dropDownList($model,'broker_id',CHtml::listData(BrokerDetail::model()->findAll(),'id','company_name')); ?>



The form will send the id corresponding to the given company name.

you have to declare the relations: assuming that there is a column called broker_id in your client_detail table.

in ClientDetail model




'company'=>array(self::BELONGS_TO, 'BrokerDetail', 'broker_id'),



in BrokerDetail model




'clients'=>array(self::HAS_MANY, 'ClientDetail', 'broker_id'),



Now in you client_detail view you can use:




$model->company->company_name;



Please read the link given above.

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.

Thanks

ibecake

I think, it would be better if you show us all your models files …

Right!

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();

}


}

?>

and heres my clientdetails form




                <div class="row">

		<?php echo $form->labelEx($model,'Broker_Name'); 

		$models = BrokerDetails::model()->findAll(array('order' => 'Company_Name')); 

		$list = CHtml::listData($models, 'Company_Name', 'Company_Name'); 

		echo $form->dropDownList($model, 'Broker_Name', $list,array('empty'=>'--please select--'));

 		echo $form->error($model,'Broker_Name'); ?>

		</div><!-- row -->

		<div class="row">

		<?php// echo $form->labelEx($model,'Broker_Id'); 

		//$models = BrokerDetails::model()->findAll(array('order' => 'Company_Name')); 

		//$list = CHtml::listData($models, 'Broker_Id', 'Company_Name'); 

		//echo $form->dropDownList($model, 'Broker_Id', $list,array('empty'=>'--please select--'));

 		//echo $form->error($model,'Broker_Id'); ?>

		</div><!-- row --> 

		

i’ve attached a capture of my view result. 2565

capture.jpg

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.

Ibecake

Fixed it Myself thank to all for the help.

Heres what i did.

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.

Nice and Simple.


$Broker_Name = $this->Broker_Name;

$Broker = BrokerDetails::model()->find("Company_Name = '$Broker_Name'");

$this->Broker_Id=$Broker->Broker_Id;  

Thanks Again.

Ibecake.

with well declared relations, like this :




    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 …)

No.

I didn’t have to declare any relations to get it it to work for me but thanks anyways for the help.

Ibecake