This is my code
can everyone help me please? i want my system insert new record when i update it in yii framework
public function actionUpdate($id)<br/>
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Entrydata']))
{
$_POST['Entrydata']['photo'] = $model->photo;
$model->attributes=$_POST['Entrydata'];
$uploadedFile = CUploadedFile::getInstance($model,'photo');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$model->photo); // image will uplode to rootDirectory/banner/
}
$this->redirect(array('view','id'=>$model->id_entry));
}
}
$this->render('create',array(
'model'=>$model,
));
}
Unset attribute before save.
$model->unsetAttributes(array("id_entry"));
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Entrydata']))
{
$_POST['Entrydata']['photo'] = $model->photo;
$model->attributes=$_POST['Entrydata'];
$uploadedFile = CUploadedFile::getInstance($model,'photo');
// Unset Primary Key value.
$model->unsetAttributes(array("id_entry"));
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$model->photo); // image will uplode to rootDirectory/banner/
}
$this->redirect(array('view','id'=>$model->id_entry));
}
}
$this->render('create',array(
'model'=>$model,
));
}
Related
I would like to upload an image and save the image name in database. I can create it and everything is ok, but update has problems. If i update the $model->img (image) value will be blank in db.
This is my model rules:
array('img', 'file','types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'on'=>'update'),
array('title, img', 'length', 'max'=>255, 'on'=>'insert,update'),
And this is the controller:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$_SESSION['KCFINDER']['disabled'] = false;
$_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/../images/"; // URL for the uploads folder
$_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../../images/"; // path to the uploads folder
// $this->performAjaxValidation($model);
if(isset($_POST['News']))
{
$_POST['News']['img'] = $model->img;
$model->attributes=$_POST['News'];
$model->img = $_POST['News']['img'];
$uploadedFile=CUploadedFile::getInstance($model,'img');
if($model->save()){
if(!empty($uploadedFile))
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../../images/'.$model->img);
}
}
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
Before the save i check $model->img and it has value.
So i think something wrong with save() in update.
$_POST['News']['img'] = $model->img;
Why you did it? You must remove this line.
$model->attributes=$_POST['News'];
$model->img = $_POST['News']['img'];
I try to upload a file to MySQL table and it does not work.
here what i write:
view:
<div class="row">
<?php echo $form->labelEx($model,'doc_ordered_recieved'); ?>
<?php echo $form->fileField($model,'doc_ordered_recieved'); ?>
<?php echo $form->error($model,'doc_ordered_recieved'); ?>
</div>
model:
i add this attribute:
public $doc_ordered_recieved;
and this rulse:
array('doc_ordered_recieved','file','types'=>'pdf', 'allowEmpty'=>true, 'on'=>'update'),
controllers:
public function actionCreate()
{
$model=new Orders;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Orders']))
{
$model->attributes=$_POST['Orders'];
$model->doc_ordered_recieved=CUploadedFile::getInstance($model,'doc_ordered_recieved');
if($model->save())
{
$doc_ordered_recieved->saveAs('http://localhost/files');
$this->redirect(array('view','id'=>$model->oid));
}
}
$this->render('create',array('model'=>$model,
));
}
please help me i don't know why its not work????
thanks you all
eliya
First you need to change the rule to add create scenario:
array('doc_ordered_recieved','file','types'=>'pdf', 'allowEmpty'=>true, 'on'=>'insert,update'),
and in your create action from your controller you need to do this:
public function actionCreate()
{
$model=new Orders;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Orders']))
{
$model->attributes=$_POST['Orders'];
$uploadedFile = CUploadedFile::getInstance($model,'doc_ordered_recieved');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
if($model->image == null || empty($model->image)){
$rnd = rand(0,9999);// generate random number between 0-9999
$fileName = "{$rnd}-{$uploadedFile}";
$model->image = $fileName;
}
$uploadedFile->saveAs(dirname(__FILE__)..'/files/'. $model->doc_ordered_recieved);
// redirect to success page
}
$this->redirect(array('view','id'=>$model->oid));
}
}
$this->render('create',array('model'=>$model,
));
}
Just by looking at your code I can see that you need to change your file path from
$doc_ordered_recieved->saveAs('http://localhost/files');
to
$doc_ordered_recieved->saveAs(Yii::app()->basePath.'path/to/localFile');
Also, you should provide more information about your model.
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));
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
I am quite new to php yiiframework. I have users model where it takes users basic information including profile image but i have to delete a image file when I updates new image for my profile.
I uploaded file by using following method.
public function actionUpdate()
{
$model=$this->loadModel($id);
$random = substr(number_format(time() * rand(),0,'',''),0,10);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$uploadedFile=CUploadedFile::getInstance($model,'image_path');
$fileName = $milliseconds. '-' .$random;
$model->image_path = $fileName;
if($model->save())
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/uploaded/'.$fileName);
$this->redirect(array('view','id'=>$model->id));
}
$data = PanXCore::getDataForCreateUser();
$userRoles = new UserRoles();
$this->render('create',array(
'model'=>$model,
'roles' => $data['roles'],
'userRoles' => $userRoles
));
}
use unlink() for example
unlink(Yii::app()->basePath.'/../images/uploaded/'. $oldfile);