hello i have 2 data table name as hotel table & hotelUser table
i want insert data in to hoteltable which available in hotelUser table when i update hotel user data it must insert both of table
additional( hotel user is tempory data table frist i insert data in to this table after check data by admin he click update button & send data to hotel table)
As long as I figured out that , You have two CActiverecord model Hotel and Hotel user. You want to collect input from one single form and save both the models . You can do it in below way :
public function actionCreate()
{
$hotel=new Hotel;
$hotelUser=new HotelUser;
if(isset($_POST['HotelUser']))
{
$hotel->attributes=$_POST['HotelUser'];
$hotelUser->attributes=$_POST['HotelUser'] //-- User $_POST['HotelUser'] for both model to load attributes
$hotel->save();$hotelUser->save();
}
$this->render('formView',array('model'=>$hotelUser)); // -- Pass hotelUser model to view for rendering form.
}
did you see did you see in [color="#FF0000"]hotel User table[/color] have additional column name as Username when insert data how insert in to hotel table with out Username i pleasure if you can give me solution…?
Do you want to set the username with value that is not exists in form ?
In this case as the Chandrakanta post, add an extra line code like this
public function actionCreate()
{
$hotel=new Hotel;
$hotelUser=new HotelUser;
if(isset($_POST['HotelUser']))
{
$hotel->attributes=$_POST['HotelUser'];
$hotelUser->attributes=$_POST['HotelUser'] //-- User $_POST['HotelUser'] for both model to load attributes
$hotel->save();
$hotelUser->username = 'anything that you want by php or Yii code about username'
$hotelUser->save();
}
$this->render('formView',array('model'=>$hotelUser)); // -- Pass hotelUser model to view for rendering form.
}
if I didn’t understood exactly what you want, please give more information
{
$model=new HotelUser;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['HotelUser']))
{
$model->attributes=$_POST['HotelUser'];
//image upload
$myfile=CUploadedFile::getInstance($model,'image');
if (is_object($myfile) && get_class($myfile)==='CUploadedFile') {
// get image with image path
$model->image="http://10.0.2.2/TheGuidersite_service/images/Hotel/{$myfile->name}";
}
if($model->save())
{ //save image URL
$myfile->saveAs(Yii::app()->basePath.'/../images/Hotel/'.$myfile->name); // image will uplode to rootDirectory/banner/
//$this->redirect(array('view','id'=>$model->id));
//$this->redirect(array('admin','id'=>$model->id));
$mail=Yii::app()->Smtpmail;
$mail->SetFrom( 'onasha@bizappsi.com',$model->username);
$msg=$model->username.'<br/>'.$model->hotel_name.'<br/>'.$model->discription .'<br/>'.$model->contact_detail.'<br/>'.'Thank you for insert Hotel detail . We will respond to you soon as possible. ' ;
//$mail->subject = $subject->subject;
$mail->MsgHTML($msg);
$mail->AddAddress('onasha@bizappsi.com','programmer channa');
$mail->AddAddress($model->username,'programmer channa');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}else {
echo "Thank you for insert Hotel detail . Please Check your mail";
//Yii::app()->user->setFlash('','Thank you for contacting us. We will respond to you as soon as possible.');
//$this->redirect(array('view','id'=>$model->hotel_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['HotelUser']))
{
$model->attributes=$_POST['HotelUser'];
$myfile=CUploadedFile::getInstance($model,'image');
if (is_object($myfile) && get_class($myfile)==='CUploadedFile') {
// get image with image path
$model->image="http://10.0.2.2/TheGuidersite_service/images/Hotel/{$myfile->name}";
}
if($model->save())
$myfile->saveAs(Yii::app()->basePath.'/../images/Hotel/'.$myfile->name);
$mails=Yii::app()->Smtpmail;
$mails->SetFrom( 'onasha@bizappsi.com',$model->username);
$msg=$model->username.'<br/>'.$model->hotel_name.'<br/>'.'Congratulations we are add Your new Hotel detail' ;
$mails->MsgHTML($msg);
$mails->AddAddress('onasha@bizappsi.com','programmer channa');
$mails->AddAddress($model->username,'programmer channa');
if(!$mails->Send()) {
echo "Mailer Error: " . $mails->ErrorInfo;
}else {
echo "Congratulations we added Your new Hotel detail . ";
//$this->redirect(array('view','id'=>$model->hotel_id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
[b]
model(hoteluser)[/b]
<?php
/**
This is the model class for table "hotel_user".
The followings are the available columns in table ‘hotel_user’:
@property integer $hotel_id
@property string $username
@property integer $type_id
@property integer $Rate_id
@property string $hotel_name
@property string $discription
@property string $image
@property string $contact_detail
@property integer $city_id
@property double $latitute
@property double $lontute
@property string $metadata
The followings are the available model relations:
@property Cities $city
@property TypeHotel $type
*/
class HotelUser extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return HotelUser the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'hotel_user';
}
/**
* @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('username, type_id, Rate_id, hotel_name, discription, image, contact_detail, city_id, latitute, lontute, metadata', 'required'),
array('type_id, Rate_id, city_id', 'numerical', 'integerOnly'=>true),
array('latitute, lontute', 'numerical'),
array('username, hotel_name, image', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('hotel_id, username, type_id, Rate_id, hotel_name, discription, image, contact_detail, city_id, latitute, lontute, metadata', 'safe', 'on'=>'search'),
array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
array(' image', 'length', 'max'=>255, 'on'=>'insert,update'),
);
}
/**
* @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(
'city' => array(self::BELONGS_TO, 'Cities', 'city_id'),
'type' => array(self::BELONGS_TO, 'TypeHotel', 'type_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'hotel_id' => 'Hotel',
'username' => 'Username',
'type_id' => 'Type',
'Rate_id' => 'Rate',
'hotel_name' => 'Hotel Name',
'discription' => 'Discription',
'image' => 'Image',
'contact_detail' => 'Contact Detail',
'city_id' => 'City',
'latitute' => 'Latitute',
'lontute' => 'Lontute',
'metadata' => 'Metadata',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('hotel_id',$this->hotel_id);
$criteria->compare('username',$this->username,true);
$criteria->compare('type_id',$this->type_id);
$criteria->compare('Rate_id',$this->Rate_id);
$criteria->compare('hotel_name',$this->hotel_name,true);
$criteria->compare('discription',$this->discription,true);
$criteria->compare('image',$this->image,true);
$criteria->compare('contact_detail',$this->contact_detail,true);
$criteria->compare('city_id',$this->city_id);
$criteria->compare('latitute',$this->latitute);
$criteria->compare('lontute',$this->lontute);
$criteria->compare('metadata',$this->metadata,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The followings are the available columns in table ‘hotels’:
@property integer $hotel_id
@property integer $type_id
@property integer $Rate_id
@property string $hotel_name
@property string $discription
@property string $image
@property string $contact_detail
@property integer $city_id
@property double $latitute
@property double $lontute
@property string $metadata
The followings are the available model relations:
@property TypeHotel $type
@property Rate $rate
@property Cities $city
@property Places[] $places
*/
class Hotels extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Hotels the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'hotels';
}
/**
* @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('type_id, Rate_id, hotel_name, discription, image, contact_detail, city_id, latitute, lontute, metadata', 'required'),
array('type_id, Rate_id, city_id', 'numerical', 'integerOnly'=>true),
array('latitute, lontute', 'numerical'),
array('hotel_name, image', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('hotel_id, type_id, Rate_id, hotel_name, discription, image, contact_detail, city_id, latitute, lontute, metadata', 'safe', 'on'=>'search'),
array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
array(' image', 'length', 'max'=>255, 'on'=>'insert,update'),
);
}
/**
* @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(
'type' => array(self::BELONGS_TO, 'TypeHotel', 'type_id'),
'rate' => array(self::BELONGS_TO, 'Rate', 'Rate_id'),
'city' => array(self::BELONGS_TO, 'Cities', 'city_id'),
'places' => array(self::HAS_MANY, 'Places', 'hotel_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'hotel_id' => 'Hotel',
'type_id' => 'Type',
'Rate_id' => 'Rate',
'hotel_name' => 'Hotel Name',
'discription' => 'Discription',
'image' => 'Image',
'contact_detail' => 'Contact Detail',
'city_id' => 'City',
'latitute' => 'Latitute',
'lontute' => 'Lontute',
'metadata' => 'Metadata',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('hotel_id',$this->hotel_id);
$criteria->compare('type_id',$this->type_id);
$criteria->compare('Rate_id',$this->Rate_id);
$criteria->compare('hotel_name',$this->hotel_name,true);
$criteria->compare('discription',$this->discription,true);
$criteria->compare('image',$this->image,true);
$criteria->compare('contact_detail',$this->contact_detail,true);
$criteria->compare('city_id',$this->city_id);
$criteria->compare('latitute',$this->latitute);
$criteria->compare('lontute',$this->lontute);
$criteria->compare('metadata',$this->metadata,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public function gethotelsoptions()
{
return CHtml::listData(Hotels::model()->findAll(),'hotel_id','hotel_name');
}
}
above code is 100% work …thank you but when i insert data in to hotel table i not insert due to it dont have username field how i insert data in to hotel table from hotel user with out Username