Can anybody help me resolve my issue? I'm able to do create new records but I can't modify or update my existing records.
Here is my source code:
public function actionCreate() {
$model = new Branchmaster;
$user = new Usermaster;
if(isset($_POST['Branchmaster'], $_POST['Usermaster'])) {
$model->attributes=$_POST['Branchmaster'];
$user->attributes=$_POST['Usermaster'];
$valid=$model->validate();
$valid=$user->validate() && $valid;
if($valid){
$model->save();
$chnuser->save();
$this->redirect(array('view','id'=>$model->Id));
}
}
$this->render('create',array(
'model'=>$model,
'user'=>$user,
));
}
public function actionUpdate($id) {
$model=$this->loadModel($id);
$user = Usermaster::model()->findByAttributes(array('branch_id'=>$model->Id));
if(isset($_POST['Branchmaster'], $_POST['Usermaster'])) {
$model->attributes=$_POST['Branchmaster'];
$user->attributes=$_POST['Usermaster'];
$valid=$model->validate();
$valid=$user->validate() && $valid;
if($valid){
$model->save();
$user->save();
$this->redirect(array('view','id'=>$model->Id));
}
}
$this->render('update',array(
'model'=>$model,
'user'=>$user,
));
}
I need help for this problem... Thanks in advance!
You have to check if id is exited than u have to do update...
use findByPk to load model...
than do process to same is above
here is sample code. you have to implement like these
$model->attributes=$_POST['Branchmaster'];
$user_post = $_POST['Usermaster'];
$id = $user_post['id'];
$user = ModelName::model()->findByPk($id);
$user->variable_name = $variable_value
$valid=$model->validate();
$valid=$user->validate() && $valid;
if($valid){
$model->save();
$user->save();
$this->redirect(array('view','id'=>$model->Id));
}
Thanks
Instead of find 'findByAttr' so that will be
// pls note tht $id must be a primary key of this particular model
$user = Usermaster::model()->findByPk($id);
// If the situation don't allow a primary key then we can go for query execution
$update = Yii::app()->db->createCommand()
->update('usermaster', array('field1'=>'value','field2'=>'value2'),
'branch_id=:id',array(':id'=>$branch_id));
Related
I have a form with multi-select fields named "city" form in Yii2. When I submit form
the post data show me the following:
$_POST['city'] = array('0'=>'City A','1'=>'City B','2'=>'City C')
But I want to save the array in serialize form like:
a:3:{i:0;s:6:"City A";i:1;s:6:"City B";i:2;s:6:"City C";}
But I don't know how to modify data before save function in Yii2. Followin is my code:
if(Yii::$app->request->post()){
$_POST['Adpackage']['Page'] = serialize($_POST['Adpackage']['Page']);
$_POST['Adpackage']['fixer_type'] = serialize($_POST['Adpackage']['fixer_type']);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model
]);
}
Please help me.
Thanks all for your effort. I have solved the issue. here is the code :
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->Page = serialize($_POST['Adpackage']['Page']);
$this->fixer_type = serialize($_POST['Adpackage']['fixer_type']);
return true;
} else {
return false;
}
}
Just put this code in model and its working
It's because Yii::$app->request->post() is different than $_POST at this stage. Try to change your code to:
$post = Yii::$app->request->post();
$post['Adpackage']['Page'] = serialize($post['Adpackage']['Page']);
$post['Adpackage']['fixer_type'] = serialize($post['Adpackage']['fixer_type']);
$model->load($post);
Update:
Also it would be better to do it on ActiveRecord beforeSave() method.
I have a tables named as ideyalar and persons.
In the registration page users can not create their profile if the username taken before.
And every user can create their ideas. But there I to face the problem that; users CAN CREATE the ideas with the same name.
I want to restrict this. But I don't know how..
If you help me, I'll be glad.
Thanks. Best regards.
P.S: I should use CUniqueValidator, but don't know how..
IdeyalarController code:
public function actionCreate()
{
$model = new Ideyalar;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Ideyalar']))
{
$model->attributes=$_POST['Ideyalar'];
$model->istifade = "1";
$model->idcontact = Yii::app()->user->getId();
if($model->save()){
// if($model->validate()) {
$command = Yii::app()->db->createCommand();
$command->insert ('mqrup', array(
'idperson'=> Yii::app()->user->getId(),
'idideya'=>$model->idideya));
$this->redirect(array('viewm','id'=>$model->idideya));
// }
}
}
$this->render('create',array(
'model'=>$model,
));
}
This is in your model file, not the controller under the method rules you should have the following rule:
array('your_attribute', 'unique'),
So you will have something like
public function rules()
{
return array(
//some rules
array('your_attribute', 'unique'),
);
}
See the wiki for more details
I am new in YII framework. I am doing update operation using YII framework. I have controller with name sitecontroller.php, model jobseekerprofile.php, view personal.php.
I got the error:
Fatal error: Call to a member function isAttributeRequired() on a non-object in E:\wamp\www\yii\framework\web\helpers\CHtml.php on line 1414
My table is job_seeker_profile
Fields
1.id
2.user_id
3.contact_no
4.gender
5.dob
6.mstatus
7.address
8.location_id
I want to edit the data in contact_no and address according to user_id
Model-Jobseekerprofile.php - rules
public function rules()
{
return array(
array('contact_no,address','required'),
);
}
controller-Sitecontroller.php
class SiteController extends Controller {
public function actionpersonal()
{
$user_id = trim($_GET['id']);
$model=Jobseekerprofile::model()->find(array(
'select'=>'contact_no,address',"condition"=>"user_id=$user_id",
'limit'=>1,));
$model = Jobseekerprofile::model()->findByPk($user_id);
if(isset($_POST['Jobseekerprofile']))
{
$model->attributes=$_POST['Jobseekerprofile'];
if($model->save())
{
$this->redirect(array('profile','user_id'=>$model->user_id));
}
}
$this->render('personal',array('model' =>$model));
}
}
Anybody help me?
Seems that $model = Jobseekerprofile::model()->findByPk($user_id) is not finding anything, so $model is null, and that is why $model->isAttributeRequired() throws an error. Check your incoming params because of this and check if there a profile with such id (or maybe you should search by attributes instead of by id?).
Besides you can use
public function actionPersonal($id) {
$model = Jobseekerprofile::model()->findByPk($id);
//
}
Instead of
public function actionpersonal() {
$user_id = trim($_GET['id']);
$model = Jobseekerprofile::model()->findByPk($user_id);
//
}
public function actionpersonal() {
$user_id = trim($_GET['id']);
$model = Jobseekerprofile::model()->findByPk($user_id);
if (isset($_POST['Jobseekerprofile'])) {
$model->attributes = $_POST['Jobseekerprofile']; //post key edited
if ($model->save()) {
$this->redirect(array('profile', 'user_id' => $model->user_id));
}
}
$this->render('personal', array('model' => $model));
}
First Check what you are getting in $_POST
and if all is ok then try to save like
$model = Jobseekerprofile::model()->findByPk($user_id);
if (isset($_POST['Jobseekerprofile'])) {
$model->attributes = $_POST['jobseekerprofile'];
$model->contact_no= $_POST['Jobseekerprofile']['contact_no']; //post key edited
$model->address = $_POST['Jobseekerprofile']['address'];
if ($model->save()) {
$this->redirect(array('profile', 'user_id' => $model->user_id));
}
}
$this->render('personal', array('model' => $model));
if not work then check what model returns
$error=$model->getErrors();
print_r($error);
above code surely gives you idea why it is not saving
everyone. Just like my post, how to create a file only once?
Here's FileController.php
public function actionCreate()
{
$model=new File;
if(isset($_POST['File']))
{
$simpan=$model->nama_file=CUploadedFile::getInstance($model,'nama_file');
if(empty($simpan)){
$model->attributes=$_POST['File'];
$model->save();
}
else{
$model->attributes=$_POST['File'];
$model->pengunggah=Yii::app()->user->id;
$model->nama_file = CUploadedFile::getInstance($model, 'nama_file');
if($model->save()){
$simpan->saveAs(Yii::app()->basePath .
'/../files/' . $model->nama_file.'');
//Yii::app()->user->setFlash('success', "Data berhasil disimpan!");
$this->redirect(array('view','id'=>$model->id_file));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
After a user create a file, he/she cannot create another, it only allows user to create only once. How to implement it? Thanks a lot
When I use redirect function after auto login this error occur:
session_regenerate_id(): Session object destruction failed
Can anyone hep me?
<?php
public function actionRegister()
{
$model = new UserProfileForm;
$this->performAjaxValidation($model,'userProfile-form');
if(isset($_POST['UserProfileForm']))
{
$model->attributes = $_POST['UserProfileForm'];
if ($model->save())
{
$u = new LoginForm;
$u->username = $model->username;
$u->password = $model->password;
$u->login();
$this->redirect(Yii::app()->user->returnUrl);
}
}
$this->render('register',array('model'=>$model,));
}
?>
The following works.
if($model->save()){
$identity=new UserIdentity($model->username,$model->password);
$identity->authenticate();
Yii::app()->user->login($identity);
$this->redirect(Yii::app()->user->returnUrl);
}
PS: $model->save() runs $model->validate() so you are repeating yourself