Dropdownlist Dependent

Buongiorno, scusate l’inesperienza ma nonostante i post letti non riesco ad implementare la dipendenza.

Ho 4 tabelle partners, activities, subactivities e activities_partners. ho generato tramite gii i form e li ho modificati come da necessità. Dato che partners e activities hanno un arelazione molti a molti gestita da activities_partners, vorrei inserire delle sottoattività selezionando nel form il partner e avendo nel dropdown attività soltanto quelle legate al partner. Utilizzo il form creato da gii ma non riesco a implementarlo.

questo è il mio _form.php

<?php

/* @var $this SubActivitiesController */

/* @var $model SubActivities */

/* @var $form CActiveForm */

?>

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'sub-activities-form',


// Please note: When you enable ajax validation, make sure the corresponding


// controller action is handling ajax validation correctly.


// There is a call to performAjaxValidation() commented in generated controller code.


// See class documentation of CActiveForm for details on this.


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'codice subattività'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'code',array('size'=&gt;30,'maxlength'=&gt;30)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'code'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'descrizione'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'name',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'name'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'partner'); ?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'partner_id', CHtml::listData(Partners::model()-&gt;findAll(), 'id', 'name'), array('empty'=&gt;'--scegli un partner--')); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'partner_id'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'attività'); ?&gt;


	&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'activity_id', CHtml::listData(Activities::model()-&gt;findAll(), 'id', 'name'), array('empty'=&gt;'--scegli un attività disponibile--')); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'activity_id'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Inserisci' : 'Save'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

e questo il controller

<?php

class SubActivitiesController extends Controller

{

/**


 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning


 * using two-column layout. See 'protected/views/layouts/column2.php'.


 */


public &#036;layout='//layouts/column2';





/**


 * @return array action filters


 */


public function filters()


{


	return array(


		'accessControl', // perform access control for CRUD operations


		'postOnly + delete', // we only allow deletion via POST request


	);


}





/**


 * Specifies the access control rules.


 * This method is used by the 'accessControl' filter.


 * @return array access control rules


 */


public function accessRules()


{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('index','view'),


			'users'=&gt;array('*'),


		),


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('create','update'),


			'users'=&gt;array('@'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


			'users'=&gt;array('admin'),


		),


		array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}





/**


 * Displays a particular model.


 * @param integer &#036;id the ID of the model to be displayed


 */


public function actionView(&#036;id)


{


	&#036;this-&gt;render('view',array(


		'model'=&gt;&#036;this-&gt;loadModel(&#036;id),


	));


}





/**


 * Creates a new model.


 * If creation is successful, the browser will be redirected to the 'view' page.


 */


public function actionCreate()


{


	&#036;model=new SubActivities;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['SubActivities']))


	{


		&#036;model-&gt;attributes=&#036;_POST['SubActivities'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


	}





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Updates a particular model.


 * If update is successful, the browser will be redirected to the 'view' page.


 * @param integer &#036;id the ID of the model to be updated


 */


public function actionUpdate(&#036;id)


{


	&#036;model=&#036;this-&gt;loadModel(&#036;id);





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['SubActivities']))


	{


		&#036;model-&gt;attributes=&#036;_POST['SubActivities'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


	}





	&#036;this-&gt;render('update',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Deletes a particular model.


 * If deletion is successful, the browser will be redirected to the 'admin' page.


 * @param integer &#036;id the ID of the model to be deleted


 */


public function actionDelete(&#036;id)


{


	&#036;this-&gt;loadModel(&#036;id)-&gt;delete();





	// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


	if(&#33;isset(&#036;_GET['ajax']))


		&#036;this-&gt;redirect(isset(&#036;_POST['returnUrl']) ? &#036;_POST['returnUrl'] : array('admin'));


}





/**


 * Lists all models.


 */


public function actionIndex()


{


	&#036;dataProvider=new CActiveDataProvider('SubActivities');


	&#036;this-&gt;render('index',array(


		'dataProvider'=&gt;&#036;dataProvider,


	));


}





/**


 * Manages all models.


 */


public function actionAdmin()


{


	&#036;model=new SubActivities('search');


	&#036;model-&gt;unsetAttributes();  // clear any default values


	if(isset(&#036;_GET['SubActivities']))


		&#036;model-&gt;attributes=&#036;_GET['SubActivities'];





	&#036;this-&gt;render('admin',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Returns the data model based on the primary key given in the GET variable.


 * If the data model is not found, an HTTP exception will be raised.


 * @param integer &#036;id the ID of the model to be loaded


 * @return SubActivities the loaded model


 * @throws CHttpException


 */


public function loadModel(&#036;id)


{


	&#036;model=SubActivities::model()-&gt;findByPk(&#036;id);


	if(&#036;model===null)


		throw new CHttpException(404,'The requested page does not exist.');


	return &#036;model;


}





/**


 * Performs the AJAX validation.


 * @param SubActivities &#036;model the model to be validated


 */


protected function performAjaxValidation(&#036;model)


{


	if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='sub-activities-form')


	{


		echo CActiveForm::validate(&#036;model);


		Yii::app()-&gt;end();


	}


}

}

per favor epotreste aiutarmi? grazie Domenico

Puoi postare il Model?

Come hai definito le relations al suo interno?

<?php

/**

  • This is the model class for table "activities_partners".

  • The followings are the available columns in table ‘activities_partners’:

  • @property integer $id

  • @property string $item_key

  • @property integer $activity_id

  • @property integer $partner_id

*/

class ActivitiesPartners extends CActiveRecord

{

/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'activities_partners';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('activity_id, partner_id', 'required'),


		array('activity_id, partner_id', 'numerical', 'integerOnly'=&gt;true),


		// The following rule is used by search().


		// @todo Please remove those attributes that should not be searched.


		array('id, activity_id, partner_id', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


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(


		'activity' =&gt; array(self::BELONGS_TO, 'Activities', 'activity_id'),


		'partner' =&gt; array(self::BELONGS_TO, 'Partners', 'partner_id'),


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'activity_id' =&gt; 'Activity',


		'partner_id' =&gt; 'Partner',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 *


 * Typical usecase:


 * - Initialize the model fields with values from filter form.


 * - Execute this method to get CActiveDataProvider instance which will filter


 * models according to data in model fields.


 * - Pass data provider to CGridView, CListView or any similar widget.


 *


 * @return CActiveDataProvider the data provider that can return the models


 * based on the search/filter conditions.


 */


public function search()


{


	// @todo Please modify the following code to remove attributes that should not be searched.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('activity_id',&#036;this-&gt;activity_id);


	&#036;criteria-&gt;compare('partner_id',&#036;this-&gt;partner_id);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}





/**


 * Returns the static model of the specified AR class.


 * Please note that you should have this exact method in all your CActiveRecord descendants&#33;


 * @param string &#036;className active record class name.


 * @return ActivitiesPartners the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}

}

e questo il model per expenses

<?php

/**

  • This is the model class for table "expenses".

  • The followings are the available columns in table ‘expenses’:

  • @property string $id

  • @property string $item_key

  • @property integer $user_id

  • @property integer $activity_id

  • @property integer $subactivity_id

  • @property integer $proof_id

  • @property string $description

  • @property string $supplier

  • @property string $date

  • @property string $amount

  • @property string $vat

  • @property string $total

  • The followings are the available model relations:

  • @property Activities $activity

  • @property SubActivities $subactivity

  • @property Proof $proof

  • @property Suppliers $supplier

*/

class Expenses extends CActiveRecord

{

    public &#036;proofIds;


    public &#036;activityIds;


    public &#036;subactivityIds;


    


    public &#036;selectOption;


    





const SELECT_PROOF=1;


    const SELECT_ACTIVITY=1;


    const SELECT_SUBACTIVITY=1;





    /**


 * @return array select options


 */


public function getSelectOptions()


{


	return array(


		


            


                    self::SELECT_PROOF=&gt;Yii::t('ui', 'Proof'),


                    self::SELECT_ACTIVITY=&gt;Yii::t('ui', 'Activities'),


                    self::SELECT_SUBACTIVITY=&gt;Yii::t('ui', 'SubActivities'),


                


	);


}


/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'expenses';


}


    


    public function beforeSave() {


    parent::beforeSave();


    if(&#036;this-&gt;isNewRecord)


    {


        &#036;this-&gt;dateline = time();


        &#036;this-&gt;user_id=Yii::app()-&gt;user-&gt;id;


        


    }


    &#036;this-&gt;total = &#036;this-&gt;amount + &#036;this-&gt;vat;


    return true;


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('user_id, activity_id, subactivity_id, proof_id, description, supplier, amount, vat', 'required'),


		array('activity_id, subactivity_id, proof_id', 'numerical', 'integerOnly'=&gt;true),


		array('description', 'length', 'max'=&gt;50),


		array('date, proofIds, activityIds, subactivityIds', 'safe'),


		// The following rule is used by search().


		// @todo Please remove those attributes that should not be searched.


		array('user_id, activity_id, subactivity_id, proof_id, description, supplier, date, amount, vat',  'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


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(


		'user' =&gt; array(self::BELONGS_TO, 'User', 'id'),


		'activity' =&gt; array(self::BELONGS_TO, 'Activities', 'activity_id'),


		'subactivity' =&gt; array(self::BELONGS_TO, 'SubActivities', 'subactivity_id'),


		'proof' =&gt; array(self::BELONGS_TO, 'Proof', 'proof_id'),


		'supplier' =&gt; array(self::BELONGS_TO, 'Suppliers', 'supplier'),


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'user_id' =&gt; 'User ID',


		'activity_id' =&gt; 'Attività',


		'subactivity_id' =&gt; 'Subattività',


		'proof_id' =&gt; 'Tipologia di spesa',


		'description' =&gt; 'Descrizione',


		'supplier' =&gt; 'Fornitore',


		'date' =&gt; 'Data',


		'amount' =&gt; 'Importo imponibile',


		'vat' =&gt; 'Importo IVA',


		'total' =&gt; 'Importo lordo',


	);





}


    


    


/**


 * Retrieves a list of models based on the current search/filter conditions.


 *


 * Typical usecase:


 * - Initialize the model fields with values from filter form.


 * - Execute this method to get CActiveDataProvider instance which will filter


 * models according to data in model fields.


 * - Pass data provider to CGridView, CListView or any similar widget.


 *


 * @return CActiveDataProvider the data provider that can return the models


 * based on the search/filter conditions.


 */


public function search()


{


	// @todo Please modify the following code to remove attributes that should not be searched.





	&#036;criteria=new XDbCriteria(array(


		'with'=&gt;'proof',


	));


	&#036;criteria-&gt;icompare('id',&#036;this-&gt;id,true);


	&#036;criteria-&gt;icompare('user_id',Yii::app()-&gt;user-&gt;id,false);


	&#036;criteria-&gt;icompare('activity_id',&#036;this-&gt;activity_id);


	&#036;criteria-&gt;icompare('subactivity_id',&#036;this-&gt;subactivity_id);


	&#036;criteria-&gt;icompare('proof_id',&#036;this-&gt;proof_id);


	&#036;criteria-&gt;icompare('description',&#036;this-&gt;description,true);


	&#036;criteria-&gt;icompare('supplier',&#036;this-&gt;supplier);


	&#036;criteria-&gt;icompare('date',&#036;this-&gt;date,true);


	&#036;criteria-&gt;icompare('amount',&#036;this-&gt;amount,true);


	&#036;criteria-&gt;icompare('vat',&#036;this-&gt;vat,true);


	&#036;criteria-&gt;icompare('total',&#036;this-&gt;total,true);








	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


		'pagination'=&gt;array(


			'pageSize'=&gt;Yii::app()-&gt;params['pageSize'],


			'pageVar'=&gt;'page',


		),


		'sort'=&gt;array(


                'defaultOrder'=&gt;'user_id, activity_id',


			'sortVar'=&gt;'sort',


			'attributes'=&gt;array(


				'user_id',


				'activity_id'=&gt;array(


					'asc'=&gt;'activities.name',


					'desc'=&gt;'activities.name DESC',


				),


				'subactivity_id'=&gt;array(


					'asc'=&gt;'sub_activities.name',


					'desc'=&gt;'sub_activities.name DESC',


				),


				'proof_id'=&gt;array(


					'asc'=&gt;'proof.name',


					'desc'=&gt;'proof.name DESC',


				)


			),


		),


	));


}





/**


 * Returns the static model of the specified AR class.


 * Please note that you should have this exact method in all your CActiveRecord descendants&#33;


 * @param string &#036;className active record class name.


 * @return Expenses the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}

}

cerco di spiegarmi meglio:

tabelle

'partners` (

id int(11) unsigned NOT NULL AUTO_INCREMENT,

item_key char(40) NOT NULL,

code char(30) NOT NULL,

name char(255) NOT NULL,

PRIMARY KEY (id),

UNIQUE KEY code (code),

KEY code_2 (code)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=22 ;

activities (

id int(11) unsigned NOT NULL AUTO_INCREMENT,

item_key char(40) NOT NULL,

code char(30) NOT NULL,

name char(255) NOT NULL,

PRIMARY KEY (id),

UNIQUE KEY code (code),

KEY code_2 (code)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

activities_partners (

id int(11) unsigned NOT NULL AUTO_INCREMENT,

item_key char(40) NOT NULL,

activity_id int(11) NOT NULL COMMENT ‘CONSTRAINT FOREIGN KEY (activity_id) REFERENCES activities (id)’,

partner_id int(11) NOT NULL COMMENT ‘CONSTRAINT FOREIGN KEY (partner_id) REFERENCES partners (id)’,

PRIMARY KEY (id),

KEY activity_id (activity_id),

KEY partner_id (partner_id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

sub_activities (

id int(11) unsigned NOT NULL AUTO_INCREMENT,

item_key char(40) NOT NULL,

code char(30) NOT NULL,

name char(255) NOT NULL,

partner_id int(11) NOT NULL COMMENT ‘CONSTRAINT FOREIGN KEY (partner_id) REFERENCES activities_partners (partner_id)’,

activity_id int(11) NOT NULL COMMENT ‘CONSTRAINT FOREIGN KEY (activity_id) REFERENCES activities_partners (activity_id)’,

PRIMARY KEY (id),

UNIQUE KEY code (code),

KEY partner_id (partner_id),

KEY activity_id (activity_id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

io vorrei una dropdown er compilare questa ultima in modo che alal scelta del Partner appaianole activity relative a quel partner ma devo prendere il name dalle altre tabelle