i have two tables category and category description and i want to save the data with one form…to save the details to two table…i’m generating models code for gii below is the code …
this is the category table model
<?php
/**
-
This is the model class for table "t_category".
-
The followings are the available columns in table ‘t_category’:
-
@property string $pk_i_id
-
@property string $fk_i_parent_id
-
@property string $i_expiration_days
-
@property string $i_position
-
@property integer $b_enabled
-
@property integer $b_price_enabled
-
@property string $s_icon
-
The followings are the available model relations:
-
@property TCategory $fkIParent
-
@property TCategory[] $tCategories
-
@property TLocale[] $tLocales
-
@property TCategoryStats $tCategoryStats
-
@property TItem[] $tItems
-
@property TKeywords[] $tKeywords
-
@property TMetaFields[] $tMetaFields
-
@property TPluginCategory[] $tPluginCategories
*/
class TCategory extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public $s_name;
public $s_description;
public $i_expiration_days;
public function tableName()
{
return 't_category';
}
/**
* @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('pk_i_id,i_expiration_days,b_enabled,b_price_enabled','required','on'=>'Create'),
array('b_enabled, b_price_enabled', 'numerical', 'integerOnly'=>true),
array('fk_i_parent_id', 'length', 'max'=>10),
array('i_expiration_days', 'length', 'max'=>3),
array('i_position', 'length', 'max'=>2),
array('s_icon', 'length', 'max'=>250),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('pk_i_id, fk_i_parent_id, i_expiration_days, i_position, b_enabled, b_price_enabled, s_icon', '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(
'fkIParent' => array(self::BELONGS_TO, 'TCategory', 'fk_i_parent_id'),
'tCategories' => array(self::HAS_MANY, 'TCategory', 'fk_i_parent_id'),
'tLocales' => array(self::MANY_MANY, 'TLocale', 't_category_description(fk_i_category_id, fk_c_locale_code)'),
'tCategoryStats' => array(self::HAS_ONE, 'TCategoryStats', 'fk_i_category_id'),
'tItems' => array(self::HAS_MANY, 'TItem', 'fk_i_category_id'),
'tKeywords' => array(self::HAS_MANY, 'TKeywords', 'fk_i_category_id'),
'tMetaFields' => array(self::MANY_MANY, 'TMetaFields', 't_meta_categories(fk_i_category_id, fk_i_field_id)'),
'tPluginCategories' => array(self::HAS_MANY, 'TPluginCategory', 'fk_i_category_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'pk_i_id' => 'Pk I',
'fk_i_parent_id' => 'Fk I Parent',
'i_expiration_days' => 'Expiration Days',
'i_position' => 'I Position',
'b_enabled' => 'B Enabled',
'b_price_enabled' => 'B Price Enabled',
's_icon' => 'S Icon',
);
}
/**
* 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.
$criteria=new CDbCriteria;
$criteria->compare('pk_i_id',$this->pk_i_id,true);
$criteria->compare('fk_i_parent_id',$this->fk_i_parent_id,true);
$criteria->compare('i_expiration_days',$this->i_expiration_days,true);
$criteria->compare('i_position',$this->i_position,true);
$criteria->compare('b_enabled',$this->b_enabled);
$criteria->compare('b_price_enabled',$this->b_price_enabled);
$criteria->compare('s_icon',$this->s_icon,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return TCategory the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
category description table model
<?php
/**
-
This is the model class for table "t_category_description".
-
The followings are the available columns in table ‘t_category_description’:
-
@property string $fk_i_category_id
-
@property string $fk_c_locale_code
-
@property string $s_name
-
@property string $s_description
-
@property string $s_slug
*/
class TCategoryDescription extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 't_category_description';
}
/**
* @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('fk_i_category_id, fk_c_locale_code, s_slug', 'required'),
array('fk_i_category_id', 'length', 'max'=>10),
array('fk_c_locale_code', 'length', 'max'=>5),
array('s_name, s_slug', 'length', 'max'=>100),
array('s_description', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('fk_i_category_id, fk_c_locale_code, s_name, s_description, s_slug', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'fk_i_category_id' => 'Fk I Category',
'fk_c_locale_code' => 'Fk C Locale Code',
's_name' => 'S Name',
's_description' => 'S Description',
's_slug' => 'S Slug',
);
}
/**
* 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.
$criteria=new CDbCriteria;
$criteria->compare('fk_i_category_id',$this->fk_i_category_id,true);
$criteria->compare('fk_c_locale_code',$this->fk_c_locale_code,true);
$criteria->compare('s_name',$this->s_name,true);
$criteria->compare('s_description',$this->s_description,true);
$criteria->compare('s_slug',$this->s_slug,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return TCategoryDescription the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
and my view having only two fields catname and category description
below is my view file…
<?php
/* @var $this TCategoryDescriptionController */
/* @var $model TCategoryDescription */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'tcategory-description-addcategory-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// See class documentation of CActiveForm for details on this,
// you need to use the performAjaxValidation()-method described there.
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'category Name'); ?>
<?php echo $form->textField($model,'s_name'); ?>
<?php echo $form->error($model,'s_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'category Description'); ?>
<?php echo $form->textField($model,'s_description'); ?>
<?php echo $form->error($model,'s_description'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'Expiration Days');?>
<?php //echo $form->textField($model,'i_expiration_days');?>
<?php //echo $form->error($model,'i_expiration_days');?>
</div>
<div class="row buttons">
<?php// echo CHtml::submitButton('Submit'); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->