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'=>'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'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'codice subattività'); ?>
<?php echo $form->textField($model,'code',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'code'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'descrizione'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'partner'); ?>
<?php echo $form->dropDownList($model,'partner_id', CHtml::listData(Partners::model()->findAll(), 'id', 'name'), array('empty'=>'--scegli un partner--')); ?>
<?php echo $form->error($model,'partner_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'attività'); ?>
<?php echo $form->dropDownList($model,'activity_id', CHtml::listData(Activities::model()->findAll(), 'id', 'name'), array('empty'=>'--scegli un attività disponibile--')); ?>
<?php echo $form->error($model,'activity_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Inserisci' : 'Save'); ?>
</div>
<?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 $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'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new SubActivities;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SubActivities']))
{
$model->attributes=$_POST['SubActivities'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SubActivities']))
{
$model->attributes=$_POST['SubActivities'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('SubActivities');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new SubActivities('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['SubActivities']))
$model->attributes=$_GET['SubActivities'];
$this->render('admin',array(
'model'=>$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 $id the ID of the model to be loaded
* @return SubActivities the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model=SubActivities::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param SubActivities $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='sub-activities-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
per favor epotreste aiutarmi? grazie Domenico