I am a newbie to the Yii framework.I want a multimodel form so I just went through this link and made all things like this.I have two table, first is group and another is member.
Group
ID
name
Member
id
group_id
firstname
lastname
Now I have made models for both tables and CRUD as well.I made change to GroupController file like this
public function actionCreate()
{
$group = new Group;
$member = new Member;
if(isset($_POST['Group'],$_POST['Member'])) {
//Populate input data to $group and $member
$group->attributes = $_POST['Group'];
$member->attributes = $_POST['Member'];
//Validate both $group and $member
$validate = $group->validate();
$validate = $member->validate() && $valid;
if($valid){
$group->save(false);
$member->save(false);
}
}
$this->render('create',array(
'group'=> '$group',
'member'=> '$member',
));
$model=new Group;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Group']))
{
$model->attributes=$_POST['Group'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
and after changing the group >> View >> create.php file like this
<?php echo $this->renderPartial('_form', array('group'=>$group, 'member'=>$member)); ?>
The _form file is like this
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'group-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($group,$member); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($member,'firstname'); ?>
<?php echo $form->textField($member,'firstname',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($member,'firstname'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
but after all I am getting error like this Undefined variable: group .
So can some one please tell me how to solve this issue. I have lost one day behind this.So any help and suggestions will be highly appreciable.
You are doing multiple mistakes here ->
when you call
$this->render('create',array(
'model'=>$model,
));
you are not passing $group or $member models which you created in the group create controller. Change it to -
$this->render('create',array(
'group'=>$group,
'member'=>$member,
));
and secondly, there is no variable named $valid... change this part
$validate = $member->validate() && $valid;
if($valid){
to
$validate = $member->validate() && $validate;
if($validate){
now things should work fine
Related
I have an feedback form in yii which simply takes input and save in database. I know yii uses PDO for data save but still it is SQL vulnerable.
Controller
public function actionFeedback()
{
$model = new Feedback;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Feedback']))
{
$model->attributes = $_POST['Feedback'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('feedback',array(
'model'=>$model,'managerList'=>$managerList,'branchList'=>$branchList,
));
}
Model
public function rules()
{
return array(
array('branch, manager,comments', 'required'),
array('branch,manager', 'length', 'max'=>5),
array('comments', 'length', 'min'=>10, 'max'=>'2000'),
);
}
View
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'branch'); ?>
<?php echo $form->dropDownList($model,'branch', $branchList, array('prompt'=>'Select branch')); ?>
<?php echo $form->error($model,'branch'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'manager'); ?>
<?php echo $form->dropDownList($model,'manager', $managerList, array('prompt'=>'Select manager')); ?>
<?php echo $form->error($model,'manager'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'comments'); ?>
<?php echo $form->extArea($model, 'comments', array('rows'=>15, 'cols'=>75)); ?>
<?php echo $form->error($model,'comments'); ?>
</div>
<?php $this->endWidget(); ?>
For me it is sql injection safe as in save the insert query is like
INSERT INTO feedback (branch,manager,comments) VALUES (:yp0,:yp1,:yp2). Bound with :yp0 = '2',:yp1='4',':yp2'='hello this is an testing comments'
Still on testing this by third party I saw they inserted a query in this so my system gives error like
duplicate entry for 4CuJhL8T2Oc1 for key 'group_key'
I searched it an found this error but unable to regenerate it from my front end anyone please suggest how it generated also please provide help to get prevent from this
Any help is appreciated
I am kinda new to Yii and i am finding some things not funny. I have a form that i want to update and it keeps throwing errors each time, when i'm trying to update it.
View: _form.php
<?php
/* #var $this BaseStationController */
/* #var $model BaseStation */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'base-station-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'c_id'); ?>
<select name="BaseStation[c_id]">
<?php foreach($models as $m):?> //Here coded the loop below to display customer's id from the database
<option value='<?php echo $m->id;?>'><?php echo $m->firstname.' '.$m->lastname;?></option>
<?php endforeach;?>
</select>
</div>
<div class="row">
<?php echo $form->labelEx($model,'base_station_num'); ?>
<?php echo $form->textField($model,'base_station_num',array('size'=>15,'maxlength'=>15)); ?>
<?php echo $form->error($model,'base_station_num'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BaseStation']))
{
$model->attributes=$_POST['BaseStation'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BaseStation']))
{
$model->attributes=$_POST['BaseStation'];
$model->c_id = $model->c_id;
$model->base_station_num = $model->base_station_num;
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
try this
i just need to do a registration form, i'm battling with completing this task with CActiveForm. Basically its just inserting a new db record on form submit. This is what i have,
MyView
<!--begin a form-->
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-registration-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
<!--error handling-->
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'age'); ?>
<?php echo $form->textField($model,'age'); ?>
<?php echo $form->error($model,'age'); ?>
</div>
<?php $this->endWidget(); ?>
<!--end a form-->
My Controller that renders the above view, this where i'm stuck, I also created a model called User(haven't done any code in it, default)
class RegisterController extends Controller
{
public function actionIndex()
{
$model = User::
$this->render('index', array('model'=>$model));
}
}
From my research i found there is something like, jst dnt know how to use it
link
$post=new Post;
$post->title='sample post';
$post->content='post body content';
$post->save();
Thanks in advance
You need to do in your actionIndex:
public function actionIndex()
{
$model = new User;
if(isset($_POST['User']))
{
$model->attributes = $_POST['User'];
if($model->save())
//Do any stuff here. for example redirect to created user view.
}
$this->render('index', array('model'=>$model));
}
I recommend you to read the Building a blog system with Yii tutorial. This is very good resource for learning yii better and also learn you the most important parts of any web application.
I have two models Register and Login.I insert data into these two tables from a single form,I want to display the entered data in a single view page,ie data from two models in a single view.php.
RegisterController.php
public function actionCreate()
{
$model = new Register;
$modelLogin = new Login;
$modelGenerate = new Generate;
$row = Generate::model()->findByPk('1') ;
$gen_reg = $row['gen_reg'];
$gen_log = $row['gen_log'];
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['btnRegister']))
{
$model->attributes=$_POST['Register'];
$modelLogin->attributes=$_POST['Login'];
$modelLogin->reg_id=$model->reg_id;
$valid = $model->validate();
$valid = $modelLogin->validate() && $valid;
if($model->save()&& $modelLogin->save())
$this->redirect(array('view','id'=>$modelLogin->log_id)
);
}
$this->render('create',array(
'model'=>$model,
'modelLogin'=>$modelLogin,
'gen_reg'=>$gen_reg,
'gen_log'=>$gen_log
));
}
_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'register-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'=>true,
)); ?>
<div class="row">
<?php echo $form->textField($model,'reg_id',array('size'=>10,'maxlength'=>10,'class'=>'txt'));?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>50,'maxlength'=>50,'class'=>'txt')); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($modelLogin,'email'); ?>
<?php echo $form->textField($modelLogin,'email',array('size'=>60,'maxlength'=>100,'class'=>'txt')); ?>
<?php echo $form->error($modelLogin,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($modelLogin,'password'); ?>
<?php echo $form->passwordField($modelLogin,'password',array('size'=>50,'maxlength'=>50,'class'=>'txt')); ?>
<?php echo $form->error($modelLogin,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($modelLogin,'passwordCompare'); ?>
<?php echo $form->passwordField($modelLogin,'passwordCompare',array('size'=>60,'maxlength'=>64,'class'=>'txt')); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'mobile'); ?>
<?php echo $form->textField($model,'mobile',array('size'=>10,'maxlength'=>10,'class'=>'txt')); ?>
<?php echo $form->error($model,'mobile'); ?>
</div>
<div class="row">
<?php echo $form->textField($modelLogin,'log_id',array('size'=>10,'maxlength'=>10,'class'=>'txt'));?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Register' : 'Save',array('name'=>'btnRegister','class'=>'btn')); ?>
</div>
<?php $this->endWidget(); ?>
Relation in register model
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(
'logins' => array(self::HAS_MANY, 'Login', 'reg_id'),
);
}
Relation in login model
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(
'reg' => array(self::BELONGS_TO, 'Register', 'reg_id'),
);
}
I want to display data from two models(that i just inserted before)to be displayed on my view.php page.how can i achive this??? now i got the output as not set for fields from Login model.
view.php
$this->breadcrumbs=array(
'Register'=>array('index'),
$model->reg_id,
);
$this->menu=array(
array('label'=>'List Register', 'url'=>array('index')),
array('label'=>'Create Register', 'url'=>array('create')),
array('label'=>'Update Register', 'url'=>array('update', 'id'=>$model->reg_id)),
array('label'=>'Delete Register', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->reg_id),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage Register', 'url'=>array('admin')),
);
?>
<h1>View Register #<?php echo $model->reg_id; ?></h1>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'reg_id',
'username',
'mobile',
),
)); ?>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$row,
'attributes'=>array(
'log_id',
'password',
'email',
),
)); ?>
im new at yii and im trying to save datas in different tables on CactiveForm.. this is my code on view:
<?php
/* #var $this UserController */
/* #var $user User */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-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,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($user, $gunwcuser, $game, $cash); ?>
<div class="row">
<?php echo $form->labelEx($user,'user'); ?>
<?php echo $form->textField($user,'user',array('size'=>16,'maxlength'=>16)); ?>
<?php echo $form->error($user,'user'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'Gender'); ?>
<?php echo $form->radioButtonList($user,'Gender', array(0 => 'Male', 1 => 'Female'), array('labelOptions'=>array('style'=>'display:inline'), 'separator'=>' ',)); ?>
<?php echo $form->error($user,'Gender'); ?>
</div><br>
<div class="row">
<?php echo $form->labelEx($user,'NickName'); ?>
<?php echo $form->textField($user,'NickName',array('size'=>16,'maxlength'=>16)); ?>
<?php echo $form->error($user,'NickName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'Password'); ?>
<?php echo $form->passwordField($user,'Password',array('size'=>16,'maxlength'=>16)); ?>
<?php echo $form->error($user,'Password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'E_Mail'); ?>
<?php echo $form->textField($user,'E_Mail',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($user,'E_Mail'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'Country'); ?>
<?php echo $form->textField($user,'Country',array('size'=>3,'maxlength'=>3)); ?>
<?php echo $form->error($user,'Country'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($user->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
there are 4 tables in DB.. which are (user, gunwcuser, game, cash). This form is for user table, the gunwcuser is going to have the same data thats put in user form, but not all the data thats being saved on the form is being requested..
this is my controller:
public function actionCreate()
{
$user = new User;
$gunwcuser =new Gunwcuser;
$game = new Game;
$cash = new Cash;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
$auth = 1;
$time = '0000-00-00 00:00:00';
if(isset($_POST['User']))
{
// Set data column in DB before saving
$user->Status = '1';
$user->MuteTime = $time;
$user->RestrictTime = $time;
$user->Authority = $auth;
$user->User_Level = '1';
$user->Authority2 = $auth;
$user->attributes=$_POST['User'];
$_POST['Gunwcuser'] = $_POST['User'];
$gunwcuser->Status = '1';
$gunwcuser->MuteTime = $time;
$gunwcuser->RestrictTime = $time;
$gunwcuser->Authority = $auth;
$gunwcuser->User_Level = '1';
$gunwcuser->Authority2 = $auth;
$gunwcuser->AuthorityBackup = $auth;
$gunwcuser->attributes=$_POST['Gunwcuser'];
$_POST['Game'] = $_POST['User'];
if($user->save())
$this->redirect(array('view','id'=>$user->Id));
}
$this->render('create',array(
'user'=>$user, 'gunwcuser'=>$gunwcuser, 'game'=>$game, 'cash'=>$cash,
));
}
i tried to copy the attributes from user to gunwcuser but when i save the data from user, i check on the DB to see if also gunwcuser has been saved but it wasnt..
theres also a few data thats going to be the same on game table but most of it, is going to be different, same as cash..
the common data are (id, username, nickname, gender, email, country, password)..
anyone can tell me how to save data in different tables on the same form? also saving different data on the same time
Hmm.. let's see. You have two models, but you save only one :). Add this code
$gunwcuser ->save()
before $this->redirect
if you wanna save both $user and $gunwcuser, You have to use save() method on each model object
if($user->save() && $gunwcuser ->save())
$this->redirect(array('view','id'=>$user->Id));