Hi I also had a problem when try to upload picture using this method.
My Action/model:
class Image extends CActiveRecord
{
public $foto;
...
public function rules()
{
return array(
...
array('foto', 'file', 'types'=>'jpg, gif, png'),
...
);
}
}
My Controller:
class ImageController extends Controller
{
public function actionCreate()
{
$model=new Image;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Image']))
{
$model->attributes=$_POST['Image'];
$model->image=CUploadedFile::getInstance($model,'foto');
if($model->save())
$model->foto->saveAs('productimages');
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
}
My view:
<?php $form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
); ?>
<?php echo $form->labelEx($model, 'foto'); ?>
<?php echo $form->fileField($model, 'foto'); ?>
<?php echo $form->error($model, 'foto'); ?>
...
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
But when I run, two problems appeared:
Problem #1:
endWiget() call [SOLVED by Ivan Misic]
ImageController contains improperly nested widget tags in its view "/var/www/html/onlineshop-nimalogos/protected/views/image/_form.php". A CActiveForm widget does not have an endWidget() call.
Problem#2:
Since problem # 1 is solved, came with another problem, the image is not saving at my 'productimages' folder.
Please help me with the problem # 2. Many thanks..
You have somewhere opened redundant CActiveForm widget and you should look into views and partials.
Yii is generting three view files with gii, and these are:
create.php update.php
| |
| |
+-------+-------+
|
|
_form.php
create and update views are rendering the same partial _form, so you should look in all three to find redundant beginWidget call.
The view you supported in your question should be _form.php partial view.
Related
Hi, for the past 2 days i've been reading and reading lots of tutorials about saving files to folders in Yii, and neither of them have worked so far. I have the folowing form:
<div class="form">
<?php $form = $this->beginWidget('CActiveForm', array(
'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'Binaryfile'); ?>
<?php echo $form->fileField($model,'uploadedFile'); ?>
<?php echo $form->error($model,'uploadedFile'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
endWidget(); ?>
The file field submits the code to a BLOB field in mysql database.
The Controller is as follows:
public function actionCreate()
{
$model=new Estudos;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Estudos']))
{
$model->attributes=$_POST['Estudos'];
$model->binaryfile = CUploadedFile::getInstance($model,'binaryfile'); // grava na bd no campo binaryfile
// $model->binaryfile->saveAs(Yii::app()->params['uploadPath']);
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
And the Model is this one:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('fileName', 'length', 'max'=>100),
array('fileType', 'length', 'max'=>50),
array('binaryfile', 'safe'),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id, fileName, fileType, binaryfile', 'safe', 'on'=>'search'),
);
}
public $uploadedFile;
// Gravar imagem na base de dados - cria blob field
public function beforeSave()
{
if ($file = CUploadedFile::getInstance($this, 'uploadedFile'))
{
$this->fileName = $file->name;
$this->fileType = $file->type;
$this->binaryfile = file_get_contents($file->tempName);
}
return parent::beforeSave();
}
The code works fine to store a file as a BLOB field, but i need to change the code to store the file in images folder and next to display links that permits to open the file (pdf file) in any browser.
To store the file in images folder i tryed saveAs() in my controller actionCreate but Yii freezes and the webpage becames blank with no error, just blank.
**Anyone can help me... I need this very very much. Many thanks in advance. **
check this link,,in this link say how do it...
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
Finally i've figured it by myself. The answer was rather simple, but took me 4 days to write it.
In my actionCreate() i did:
public function actionCreate()
{
$model=new Estudos;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Estudos']))
{
$model->attributes=$_POST['Estudos'];
$model->uploadedFile=CUploadedFile::getInstance($model,'uploadedFile');
if($model->save())
$model->uploadedFile->saveAs("pdfs/".$model->uploadedFile,true);
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
**That way the saveAs() function worked like a charm and now saves my submited files in the pdfs folder.
The next step is to try and figure out how to create links for all files submitted to pdfs folder.
Maybe with a foreach() loop.
Best regards...**
I have this code Im trying to save the content and the title from a form I made..It has an id that autoincrement the id number adds in the database but the title and the content isn't/cant be save in the database. Can you please check my code if I've done something wrong? or what I'm lacking at.
Here is my model ContentForm.php
<?php
class ContentForm extends CActiveRecord{
public $content;
public $title;
public function tableName(){
return 'tbl_content';
}
public function attributeLabels()
{
return array(
'contentid' => 'contentid',
'content' => 'content',
'title' => 'title',
// 'email' => 'Email',
// 'usrtype' => 'Usrtype',
);
}
Here is my view content.php
<div>
<p>User: <a href="viewuserpost">
<?php
echo Yii::app()->session['nameuser'];
?>
</a>
</p>
</div>
<h1>Content</h1>
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
?>
Title:
<div class="row">
<?php
echo $form->textfield($model,'title');
?>
</div>
</br>
Body:
<div class="row">
<?php
echo $form->textArea($model,'content',array('rows'=>16,'cols'=>110));
?>
</div>
<div class="row buttons">
<?php
echo CHtml::submitButton($model->isNewRecord? 'Create':'Save');
?>
</div>
<?php $this->endWidget(); ?>
and here is my content action in my sitecontroller.php
public function actionContent(){
$model=new ContentForm;
if(isset($_POST['ContentForm'])) {
$model->attributes=$_POST['ContentForm'];
if($model->save())
$this->redirect(array('content','contentid'=>$model->contentid));
$this->redirect(array('content','title'=>$model->title));
$this->redirect(array('content','content'=>$model->content));
}
$this->render('content',array('model'=>$model));
}
Please help.
Remove
public $content;
public $title;
from your class.
Yii uses PHP magic methods. And when you add attributes to your class, PHP doesn't call them but references to your explicitly written attributes.
Moreover, you should add some validation, if you use $model->attributes=$_POST['ContentForm'];. Another variant is to use unsecure $model->setAttributes($_POST[ContentForm], false) where false tells Yii to set all attributes, not only that are considered safe.
Note, that attributes is not real Model attribute, this is virtual attribute accessed through magic methods.
Also, you don't need three redirects. This is HTTP redirect to other page. This time, you just should just specify route to model view action and its parameter that is id, for example. Like this $this->redirect(array('content/view','id'=>$model->contentid));.
Of course, simplest way for you is to create new model and controller with actions using Gii.
you may missed rules , add this in your model ContentForm.php
public function rules()
{
return array(
array('content,title', 'safe'),
);
}
For more about model validation
http://www.yiiframework.com/wiki/56/reference-model-rules-validation/
Don't know, if it is a typical problem, but can't figure how to realise it.
3 tables:
Application. It's relations:
return array(
'company_info' => array(self::HAS_ONE, 'AppCompany', 'applicant_id'),
'region'=>array(
self::HAS_ONE,'AppRegion',array('company_region_id'=>'id'),
'through'=>'company_info'
),
'sector'=>array(
self::HAS_ONE,'AppSector',array('company_sector_id'=>'id'),
'through'=>'company_info'
),
);
AppCompany. Relations:
return array(
'application' => array(self::BELONGS_TO, 'Application', 'applicant_id'),
'region' => array(self::BELONGS_TO, 'AppRegion', 'company_region_id'),
'sector' => array(self::BELONGS_TO, 'AppSector', 'company_sector_id'),
'goodsservices' => array(self::HAS_MANY, 'AppGoodsServices', 'company_id')
);
App.GoodsServices. Relations:
return array(
'company'=>array(self::BELONGS_TO, 'AppCompany', 'company_id'),
);
So, one user adds info about his company (company_name, date_of_registration etc), and than add some goods/services that this company produces. How to COLLECT, UPDATE AND VIEW all this information in one form? Is it possible?
How do I see this:
User addes some info and meets the field: "Goods and services and their percentage" and two fields - "GOODS AND SERVICES" and "PERCENTAGE" and ADD MORE button and he has 6 goods that he wants to add. So, he clicks on ADD MORE button and each time the new fields below appear. But the goods limit is 5, so the button becomes inactive after 5 fields have info.
And after SUBMIT button, all this info records to its table. It can be taken from table on UPDATE button and can be viewd in one CDetailView.
Please, how it can be implemented at least in general terms. Thank you
UPDATE
Ok, some work is done (don't know if it right, but...):
I used this extension: http://www.yiiframework.com/extension/esaverelatedbehavior/
Now I have:
public function actionCreate()
{
$model=new Application;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Application']))
{
$model->attributes=$_POST['Application'];
if(isset($_POST['AppGoodsServices']))
{
$model->goodsservices = $_POST['AppGoodsServices'];
}
if ($model->saveWithRelated('goodsservices'))
{
$this->redirect(array('view', 'id' => $model->id));
} else {$model->addError('goodsservices', 'Error occured while saving goods/services.'); }
}
$this->render('create',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['Application']))
{
$model->attributes=$_POST['Application'];
if(isset($_POST['AppGoodsServices']))
{
$model->goodsservices = $_POST['AppGoodsServices'];
}
if ($model->saveWithRelated('goodsservices'))
{
$this->redirect(array('view', 'id' => $model->id));
} else {$model->addError('goodsservices', 'Error occured while saving goods/services.'); }
}
$this->render('update',array(
'model'=>$model,
));
}
public function loadModel($id)
{
$model=Application::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
In my _form.php
<div id="goodsservices">
<?php
$index = 0;
foreach ($model->goodsservices as $id => $goods):
$this->renderPartial('goods/_form', array(
'model' => $goods,
'index' => $id,
));
$index++;
endforeach;
?>
</div>
and subform in goods/_form:
<div style="margin-bottom: 20px; width:100%; clear:left;" class="crow">
<div class="row" style="float: left;">
<?php echo CHtml::activeLabelEx($model, '['.$index.']goods_and_services'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']goods_and_services', array('size' => 30, 'maxlength' => 150)); ?>
<?php echo CHtml::error($model, '['.$index .']goods_and_services'); ?>
</div>
<div class="row" style="float: left;">
<?php echo CHtml::activeLabelEx($model, '['.$index .']percentage'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']percentage', array('size' => 5)); ?>
<?php echo CHtml::error($model, '['.$index.']percentage'); ?>
</div>
<div class="row" style="float: left;">
<?php echo CHtml::link('Delete', '#', array('onclick' => 'deleteGoods(this, '.$index.'); return false;'));
?>
</div>
</div>
<div style="clear: both;"></div>
<?php
Yii::app()->clientScript->registerScript('deleteGoods', "
function deleteGoods(elm, index)
{
element=$(elm).parent().parent();
/* animate div */
$(element).animate(
{
opacity: 0.25,
left: '+=50',
height: 'toggle'
}, 500,
function() {
/* remove div */
$(element).remove();
});
}", CClientScript::POS_END);
?>
Everything works, but I cant understand how to make a batch CREATE for children (goodsservices) model. Now it only shows me children in UPDATE form (I've made some records straight into DB) with DELETE button near each. But how to implement CREATE? So, one empty row should be visible and I could ADD other rows on click "Add row"?
It is possible. Using javascript you should create additional items in view, in controller action you are getting $_POST with all data, fill and validate all models in one transaction. In update action view you are showing related models with php foreach.
There is an example of code that can be useful for you. See comments also.
Here's a newbie question related to my latest exercise with Yii Framework.
I have a following structure in the database:
CREATE TABLE location(
id INTEGER PRIMARY KEY NOT NULL,
location TEXT);
CREATE TABLE parameter(
id INTEGER PRIMARY KEY NOT NULL,
name TEXT,
value TEXT);
CREATE TABLE temperature(
id INTEGER PRIMARY KEY NOT NULL,
locationId INTEGER,
value REAL NOT NULL,
createDate DATETIME NOT NULL,
FOREIGN KEY(locationId) REFERENCES location(id));
CREATE INDEX idx1_temperature ON temperature (createDate);
I'm trying to create a view containing both location data in a grid, but also a functionality to change a certain value in parameter-table. In practice I would have a list of all possible locations in a grid, in addition to a possibility to change the parameter.value where parameter.name="CURRENT_LOCATION".
What I've gotten so far is:
location/admin.php generating a view:
...
<h2>Change current location</h2>
<?php echo $this->renderPartial('_para', array('model'=>Parameter::model()->find('name="current_location"'))); ?>
<h2>Modify locations</h2>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'location-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'location',
array(
'class'=>'CButtonColumn',
),
),
));
...
location/_para.php for embedding a form:
<?php
/* #var $this ParameterController */
/* #var $model Parameter */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'parameter-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'value'); ?>
<?php //echo $form->textField($model,'value'); ?>
<?php echo $form->dropDownList($model,'value',CHtml::listData(Location::model()->findAll(), 'id', 'location')); ?>
<?php echo $form->error($model,'value'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
So, what would need is to Save(=update) a given parameter-row, but I just seem not to get it right. I have debugged so far that it seems like the Submit Button causes the Location-controller's actionAdmin-function to be executed. That would be fine by me, if I just could instruct there to save the Parameter-record instead of the Location.
Here's the actionAdmin-function from LocationController.php:
public function actionAdmin()
{
$model=new Location('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Location']))
{
$model->attributes=$_GET['Location'];
}
if(isset($_POST['Parameter']))
{
$model->attributes=$_POST['Parameter'];
if($model->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('admin',array(
'model'=>$model,
));
}
I see a lot to posts covering multi model forms and such, but I just cannot get a grip on this. It may very well be that I'm trying to accomplish this in a totally wrong way.
So, punch me to right direction, please.
Okay so, I think the best thing is to put the method for updating the Parameter in the ParameterController. That makes it easier to implement and is a bit cleaner.
So to do that, change the form code to this:
You might need to adjust the action in the form for it to work, e.g. /admin/parameter/update
<?php
/* #var $this ParameterController */
/* #var $model Parameter */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'parameter-form',
'enableAjaxValidation'=>false,
// Check that the action method below is correct
'action' => array('/parameter/update', 'id' => $model->id),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'value'); ?>
<?php //echo $form->textField($model,'value'); ?>
<?php echo $form->dropDownList($model,'value',CHtml::listData(Location::model()->findAll(), 'id', 'location')); ?>
<?php echo $form->error($model,'value'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
If you have an existing update method in your ParameterController, see if it works as is. If it doesn't or you don't have the update method, try something like this:
public function actionUpdate($id)
{
$model = Parameter::model()->findByPk($id);
if(isset($_POST['Parameter']))
{
$model->attributes=$_POST['Parameter'];
if($model->update())
{
// Below redirects to the previous URL
$this->redirect(Yii::app()->request->urlReferrer);
}
}
}
I changed $model->save() to $model->update() because that won't call the validation rules and make it return false. If you want to validate for some reason, you will need to change the rule so that name and value are only required when creating a new parameter, like this:
array('name, value', 'required', 'on' => 'create'),
And then when you create your new Parameter, you would need to do $model = new Parameter('create');
I need to understand how to build Ajax request in Yii. I searched on the Yii website and found the following article :
http://www.yiiframework.com/wiki/24/
I wrote the code and I tested it on my localhost ? but for some reason it did not work.
For a first attempt I only wanted to do something simple. I wanted to print the result of another action on my page by using Ajax. The text that I want to be displayed is 'Hi'.
This is how mu code looks like for that action:
view/index
<?php
/* #var $this CurrentController */
$this->breadcrumbs=array(
'Current'=>array('/current'),
'index',
);
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-index-form',
'enableAjaxValidation'=>true,
)); ?>
<?php
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city_id','', array());
?>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller
<?php
class CurrentController extends Controller
{
public function accessRules()
{
return array(
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','dynamiccities'),
'users'=>array('#'),
),
);
}
public $country_id;
public function actionIndex()
{
$this->render('index');
}
public function actionDynamiccities() /// Called Ajax
{
echo CHtml::tag('option',
array('value'=>'2'),CHtml::encode('Text'),true);
}
}
Unfortunately I'm not getting the desired result. What I get is:
drowpdown list contains country array.
another drowpdown list but empty ?!
How should I fix my example code so it would work? Can anyone see what I am doing wrong?
echo CHtml::dropDownList('city_id','', array());
use id as
echo CHtml::dropDownList('city_id','', array('id'=>'city_id'));