Yii Framework Forum: Dropdowns Not Saving Relational Data - Yii Framework Forum

Jump to content

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

Dropdowns Not Saving Relational Data depdent dropdown and tree dropdowns Rate Topic: -----

#1 User is offline   skworden 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 29 January 2013 - 06:21 PM

I having a problem saving the data in the datadata base. It's showing the proper names and working like it should on the form however, when I hit create it doesn't save the data (says fields cannot be blank and resets the fields to the prompt messages).

I have two different form fields that are doing this (same form) the first part is a dependent dropdown and the second is a tree dropdown.

Here is all the code for the dependent dropdown. You select a manufacturer and it shows all the products for that manufacturer in the second.

model

//creates the dependent dropdown menu between company and products in _form.php
'product' => array(self::BELONGS_TO, 'Products', 'product_id'),


controller
	public function actionProducts() {

		{
		$data=Products::model()->findAll('company_id=:company_id', 
			      array(':company_id'=>(int) $_POST['company_id']));
	     
		$data=CHtml::listData($data,'id','model');
		foreach($data as $value=>$name)
		{
		    echo CHtml::tag('option',
			       array('value'=>$value),CHtml::encode($name),true);
		}
	    }
	}


Form

<div class="row">
		<?php echo $form->labelEx($model, 'company_id'); ?>
		<?php
		$companies = CHtml::listData(Companies::model()->findAll(array('order' => 'id')), 'id', 'name');
		echo CHtml::dropDownList('company_id','',$companies,
		array(
		'prompt'=>'Select Manufacturer',
		'ajax' => array(
			'type'=>'POST',
			'url'=>CController::createUrl('Products'),
			'update'=>'#product_id',
			)
		)); 	
		?>
	</div>
	
	<div class="row">
		<?php echo $form->labelEx($model, 'product_id'); ?>
		<?php echo CHtml::dropDownList('product_id','', array(),
			array(
			      'style' => 'width:170px;',
			      'prompt'=>'Select Product Model',
		));
		?>
		<?php echo $form->error($model, 'product_id'); ?>
	</div>





This is the second field that isn't saving it is a tree dropdown. It pulls that data from the locations table and displays them in a parent child tree view in the dropdown. i.e.

1
-1.1
--1.1.1
2
3
-3.1
--3.2
---3.3



model

'parent' => array(self::BELONGS_TO, 'Locations', 'id'),
'children' => array(self::HAS_MANY, 'Locations', 'parent'),


form

<div class="row">
		<?php echo $form->labelEx($model,'location'); ?>
		<?php
		$parents = Locations::model()->findAll('parent = 0');
		$cm = new CommonMethods();
		$data = $cm->makeDropDown($parents);
		echo $form->dropDownList($model,'parent',  $data, array('style'=>'width: 185px;', 'empty'=>'Please Select Location')); ?>
		<?php echo $form->error($model,'location'); ?>
	</div>


CommonMethods.php

<?php
class CommonMethods {
 
    private $data = array();
   
    public function makeDropDown($parents)
    {
        global $data;
        $data = array();
        $data['0'] = '';
        foreach($parents as $parent)
        {
                $data[$parent->id] = $parent->name;
                $this->subDropDown($parent->children);      
        }
       return $data;
    }
   
  public function subDropDown($children,$space = '--')
    {
        global $data;
       
        foreach($children as $child)
                {
                    $data[$child->id] = $space.$child->name;
                    $this->subDropDown($child->children,$space.'--');
                }
    }
}
?>



Posted Image

Help with this would be greatly appreciated!
0

#2 User is offline   AustinGeek 

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

Posted 29 January 2013 - 06:59 PM

View Postskworden, on 29 January 2013 - 06:21 PM, said:

I having a problem saving the data in the datadata base. It's showing the proper names and working like it should on the form however, when I hit create it doesn't save the data (says fields cannot be blank and resets the fields to the prompt messages).

I have two different form fields that are doing this (same form) the first part is a dependent dropdown and the second is a tree dropdown.

Here is all the code for the dependent dropdown. You select a manufacturer and it shows all the products for that manufacturer in the second.

model

//creates the dependent dropdown menu between company and products in _form.php
'product' => array(self::BELONGS_TO, 'Products', 'product_id'),


controller
	public function actionProducts() {

		{
		$data=Products::model()->findAll('company_id=:company_id', 
			      array(':company_id'=>(int) $_POST['company_id']));
	     
		$data=CHtml::listData($data,'id','model');
		foreach($data as $value=>$name)
		{
		    echo CHtml::tag('option',
			       array('value'=>$value),CHtml::encode($name),true);
		}
	    }
	}


Form

<div class="row">
		<?php echo $form->labelEx($model, 'company_id'); ?>
		<?php
		$companies = CHtml::listData(Companies::model()->findAll(array('order' => 'id')), 'id', 'name');
		echo CHtml::dropDownList('company_id','',$companies,
		array(
		'prompt'=>'Select Manufacturer',
		'ajax' => array(
			'type'=>'POST',
			'url'=>CController::createUrl('Products'),
			'update'=>'#product_id',
			)
		)); 	
		?>
	</div>
	
	<div class="row">
		<?php echo $form->labelEx($model, 'product_id'); ?>
		<?php echo CHtml::dropDownList('product_id','', array(),
			array(
			      'style' => 'width:170px;',
			      'prompt'=>'Select Product Model',
		));
		?>
		<?php echo $form->error($model, 'product_id'); ?>
	</div>





This is the second field that isn't saving it is a tree dropdown. It pulls that data from the locations table and displays them in a parent child tree view in the dropdown. i.e.

1
-1.1
--1.1.1
2
3
-3.1
--3.2
---3.3



model

'parent' => array(self::BELONGS_TO, 'Locations', 'id'),
'children' => array(self::HAS_MANY, 'Locations', 'parent'),


form

<div class="row">
		<?php echo $form->labelEx($model,'location'); ?>
		<?php
		$parents = Locations::model()->findAll('parent = 0');
		$cm = new CommonMethods();
		$data = $cm->makeDropDown($parents);
		echo $form->dropDownList($model,'parent',  $data, array('style'=>'width: 185px;', 'empty'=>'Please Select Location')); ?>
		<?php echo $form->error($model,'location'); ?>
	</div>


CommonMethods.php

<?php
class CommonMethods {
 
    private $data = array();
   
    public function makeDropDown($parents)
    {
        global $data;
        $data = array();
        $data['0'] = '';
        foreach($parents as $parent)
        {
                $data[$parent->id] = $parent->name;
                $this->subDropDown($parent->children);      
        }
       return $data;
    }
   
  public function subDropDown($children,$space = '--')
    {
        global $data;
       
        foreach($children as $child)
                {
                    $data[$child->id] = $space.$child->name;
                    $this->subDropDown($child->children,$space.'--');
                }
    }
}
?>



Posted Image

Help with this would be greatly appreciated!



Check the Controller for actionCreate()
You probably added these fields to the table and views but the controller doesn't know what to do with them.
0

#3 User is offline   skworden 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 30 January 2013 - 11:28 AM

View PostAustinGeek, on 29 January 2013 - 06:59 PM, said:

Check the Controller for actionCreate()
You probably added these fields to the table and views but the controller doesn't know what to do with them.


The fields work if I don't use the above setup. If i use something like this it works.

<?php echo $form->dropDownList($model, 'company_id', CHtml::listData( Companies::model()->findAll(),
		'id', 'name'),array('class'=>'', 'prompt' => 'Select a Manufacturer')); ?>
		?>

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