Yii Framework Forum: How To Fetch Multiple Fields - Yii Framework Forum

Jump to content

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

How To Fetch Multiple Fields

#1 User is offline   AllenRaj 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-October 13

Posted 04 June 2014 - 05:41 AM

I try to auto fill two fileds from the foreign key table in the matching the auto-complete word. But i can't fill the auto complete value

My scenario is, when i type the product name, i need to fetch id and price of a product from the product table and set the id for the product name and set price in another html input.

Here is my code

1) unzip the extension into ../extensions/

2) make sure config/main.php has:

3) ensure the relationship exists in the model:
'relations'=>return array(
			'soNo' => array(self::BELONGS_TO, 'SoMaster', 'so_no'),
			'prod_name' => array(self::BELONGS_TO, 'ProductMaster', 'prod_id'),
			//'prodPrize' => array(self::BELONGS_TO, 'ProductMaster', 'prod_price'),

4) optional.
5) in the _form.php for the main record
<div class="row">
		<?php //echo $form->labelEx($so_model,'prod_id'); ?>
		<?php //echo $form->textField($so_model,'prod_id',array('size'=>10,'maxlength'=>10)); ?>
// echo $form->error($so_model,'prod_id');
		echo $form->labelEx ( $so_model, 'prod_id' );
		$this->widget ( 'EJuiAutoCompleteFkField', array (
				'model' => $so_model,
				'attribute' => 'prod_id', // the FK field (from CJuiInputWidget)
				                        // controller method to return the autoComplete data (from CJuiAutoComplete)
				                        // 'sourceUrl'=>'index.php?r=productMaster/productName',
				'sourceUrl' => Yii::app ()->createUrl ( '/soDetail/productName' ),
				// defaults to false. set 'true' to display the FK field with 'readonly' attribute.
				'showFKField' => true,
				// display size of the FK field. only matters if not hidden. defaults to 10
				'FKFieldSize' => 15,
				'relName' => 'prod_name', // the relation name defined above
				'displayAttr' => 'ProductIdAndPrice', // attribute or pseudo-attribute to display
				                                    // length of the AutoComplete/display field, defaults to 50
				'autoCompleteLength' => 60,
				// any attributes of CJuiAutoComplete and jQuery JUI AutoComplete widget may
				// also be defined. read the code and docs for all options
				'options' => array (
						// number of characters that must be typed before
						// autoCompleter returns a value, defaults to 2
						'minLength' => 1 ,
						'onclick' =>'productidvalue();'
		) );
		echo $form->error ( $so_model, 'prod_id' );

        <div class="row">
  <?php echo $form->labelEx($so_model,'prod_price'); ?>
   <?php echo $form->textField($so_model,'prod_price',array('size'=>50,'maxlength'=>50,'id'=>'product_price')); ?>
  <?php echo $form->error($so_model,'prod_price'); ?>

6) in the Controller for the model, create a method to return the autoComplete data. NOTE: make sure to give users the correct permission to execute this method, according to your security scheme

public function actionProductName() {
		$q = $_GET ['term'];

		if (isset ( $q )) {
			$criteria = new CDbCriteria;
			$criteria->select = array('prod_name, prod_id, prod_price');
			$criteria->addSearchCondition('prod_name',  $_GET['term'] ) ;
			$productId = ProductMaster::model()->findAll($criteria);
			if (!empty($productId)) {
				$out = array();
				foreach ($productId as $p) {
					//Yii::trace($p, 'application');
					//Yii::log($p->prod_price, CLogger::LEVEL_ERROR, 'Message Here...');
					//$log = Yii::getLogger($p->prod_price);
					$out[] = array(
							'label' => $p->prod_name,
							'value' => $p->prod_name,
							'id' => $p->prod_id, // return value from autocomplete
				echo CJSON::encode($out);

The above code works if i retrieve prod_id and prod_price seprately.

I can't able to send both values.

Is it possible to set product price for the text box?

Thank you in advance.

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