Hi,
I have trouble saving related models of table, maybe I’m confused about HAS_MANY and BELONGS_TO relations.
I have table Pagaments (A) that is related with FacturesPropies(B), FacturesProveidors( C ) and Segsocial(D) with HAS_MANY
B,C and D are related with A with BELONG_TO, all three tables has got A_id foreign key.
Those relations aren’t like invoice, line_invoice. It works like this: In A form the user can choose some B, C or D items to related with A item. But always B,C or D items exist before the user creates A item.
I’m having troubles to find some extension to save this relation between A and B,C and D
esaverelatedbehavior and CSaveRelationsBehavior seems that is thought for has_many relation that when you create A also create B or C or D.
Do you know what extension can help me ?
Maybe I don’t understand how use this extensions ?
Maybe I don’t understand the relations and this is not a HAS_MANY relation ?
This is the model pagaments the A that I’m talking. B, C and D are FacturesPropies, FacturesProveidors, Segsocial
I have a form with 3 CGridView with a CCheckBoxColumn that sends ids of related items. Then I receive an array of ids
This code is generated with gii and then I’ve changed parts, I omitted code to make text easier to read
<?php
class Pagaments extends CActiveRecord
{
public $comissio = 0.1;
/**
* @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('data, soci', 'required'),
array('soci', 'numerical', 'integerOnly'=>true),
array('facturesPropiesImport, facturesProveidorsImport, segsocialsImport, comissioImport', 'numerical'),
array('descripcio', 'length', 'max'=>255),
array('data', 'date','format'=>Yii::app()->locale->getDateFormat('medium')),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('pid, data, facturesPropiesImport, facturesProveidorsImport, segsocialsImport, comissioImport, soci, descripcio', 'safe', 'on'=>'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(
'facturesPropies' => array(self::HAS_MANY, 'FacturesPropies', 'pagament'),
'facturesProveidors' => array(self::HAS_MANY, 'FacturesProveidors', 'pagament'),
'soci0' => array(self::BELONGS_TO, 'Socis', 'soci'),
'segsocials' => array(self::HAS_MANY, 'Segsocial', 'pagament'),
);
}
public function behaviors()
{
return array(
'datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior'),
'CAdvancedArBehavior' => array('class' => 'ext.CAdvancedArBehavior'),
);
}
}
And this the controller
<?php
class PagamentsController extends Controller
{
public function actionCreate()
{
$model=new Pagaments;
if(isset($_POST['Pagaments']))
{
$model->attributes = $_POST['Pagaments'];
if (isset($_POST['facturesPropies'])) $model->facturesPropies = $_POST['facturesPropies'];
if (isset($_POST['facturesProveidors'])) $model->facturesProveidors = $_POST['facturesProveidors'];
if (isset($_POST['segsocials'])) $model->segsocials = $_POST['segsocials'];
if($model->save())
$this->redirect(array('view','id'=>$model->pid));
} else $model->data = Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('medium'),time());
$this->render('create',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Pagaments']))
{
$model->attributes=$_POST['Pagaments'];
$model->facturesPropies = isset($_POST['facturesPropies']) ? $_POST['facturesPropies'] : array();
$model->facturesProveidors = isset($_POST['facturesProveidors']) ? $_POST['facturesProveidors'] : array();
$model->segsocials = isset($_POST['segsocials']) ? $_POST['segsocials'] : array();
if($model->save())
$this->redirect(array('view','id'=>$model->pid));
}
$this->render('update',array(
'model'=>$model,
));
}
}