DropDownList from database in YII - php

i am creating yii application first time, i have used following code to create dropdownlist in my view
echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll());
it show me error
include(Forms.php): failed to open stream: No such file or directory
C:\wamp\www\LearningYii\protected\views\codelookup_form.php(35):
<?php echo $form->textField($model,'ShortDesc',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'ShortDesc'); ?>
</div>
<div class="row">
<?php echo $form->dropDownList($model,'CodeLookupId',Forms::model()->findAll()); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

$opts = CHtml::listData(Codelookup::model()->findAll(),'CodeLookupId','CodeDesc');
echo $form->dropDownList($model,'ParentCodeLookupId',$opts,array('empty'=>''));
This code is working

use CHtml::listData to convert the db object to list data:
Here ID , NAME is your table values which should be used in option value and option text.
<?php
$opts = CHtml::listData(Forms::model()->findAll(),'CodeLookupId','CodeLookupId');
//to check list
print_r($opts);
echo $form->dropDownList($model,'CodeLookupId',$opts);
?>

try hope this will help..
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php
$records = loaddataform::model()->findAll();
$list = CHtml::listData($records, 'id', 'username');
echo CHtml::dropDownList('loaddataform', $model,
CHtml::listData($records,
'a_id', 'a_name'),
array('empty' => '(Select a name)'));
?>
</div>
and in controller
public function actionloaddata()
{
$model = new loaddataform;
$models = loaddataform::model()->findAll(array('order' => 'a_name'));
$list = CHtml::listData($models, 'a_id', 'a_name');
$this->render('loaddata',array('model'=>$model));
}
This works correctly..it retrives name from database.

Related

undefined variable model in yii

I am getting the error undefined variable model. I have the following codes in my view and controller. My aim is to search the results and show it in same page. I have tried the following things but its not working.
Again,I this this the conventional yii method to do so,or do I have to use a search() function from model SearchEmployee().?
public function actionSearch()
{
$model = new SearchEmployee();
/*Getting Data From Search Form For Processing */
if (isset($_POST['SearchEmployee'])) {
$category = $_POST['SearchEmployee']['category_id'];
$skills = $_POST['SearchEmployee']['skills'];
$experience = $_POST['SearchEmployee']['experience'];
$model = SearchEmployee::model()->find(array(
'select' => array('*'), "condition" => "category_id=$category AND key_skills like '%$skills%' AND experience=$experience",
));
$this->render('search',$model);
}
/*Getting Data From Search Form For Processing */
$this->render('search', array('model' => $model));
}
In View Section: search.php//To display and search the desired result
<div class="row">
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo $form->dropDownList($model,'category_id',$list,array('empty' =>'(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<div class="view">
<h1>Results </h1>
<div class="view" id="id">
<h1> Records Display </h1>
<h4>Name: <?php echo $form->labelEx($model,'name'); ?></h4>
<h4>Skills: <?php echo $form->labelEx($model,'experience'); ?></h4>
<h4>Experience: <?php echo $form->labelEx($model,'skills'); ?></h4>
<h5> &nbsp ;<?php echo $form->labelEx('VIew Details'); ?></h5>
</div>
</div>
In view section I am using the search and view results option in the same form.I am getting the error after clicking the search button.Is this the correct way to do
$model = SearchEmployee::model()->find(array(
'select' => array('*'), "condition" => "category_id=$category AND
key_skills like '%$skills%' AND experience=$experience",
));
There is error here. You can't use find in that way.
public function find($condition='',$params=array())
$model = SearchEmployee::model()->find("category_id=$category AND
key_skills like '%$skills%' AND experience=$experience");
But better
$model = SearchEmployee::model()->find("category_id=:category AND key_skills like :skill AND experience=:experience", array(
'category'=>$category,
'skill'=>'%'.$skills.'%',
'experience'=>$experience
));
Remove this line:
$this->render('search',$model);
First it's invalid because it should be:
$this->render('search', array('model' => $model));
But also it's unnecessary because you already have it lower in your code.

Drop Down list in yii.?

I am facing hard times working with yii framework,The latest I ecounterd is while populating data from database in a drop down.i have tried few things including codes from the site,but no use,I have the following codes,Have a model named as PostJob.PHP.
Do i have to write a seprate function in model.?
view: postjob.php
<div class="row">
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo CHtml::dropDownList('category', $model->title, $list, array('empty' => '(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
</div>
and in view section , I have given :
<?php
$list = CHtml::listData(PostJob::model()->findAll(array('jobpost','description')),'id','title');
?>
geting nothing.Pls Help.
you can do this:
<?php $list = CHtml::listData(PostJob::model()->findAll(),'id','title'); ?>
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo $form->dropDownList($model , 'category', $list, array('empty' => '(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>

how to assign array to $model in yii

Sorry. I was shorten my question because i was consider that, those users who work on yii can easily understand my question but now i am modifying so you can easily understand.
I want to update two different table at a time through single controller/view.
_form.php (update form)
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'verse-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'topic_id'); ?>
<?php echo $form->textField($model,'topic_id', $model->topic_id); ?>
<?php echo $form->error($model,'topic_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'sorah_id'); ?>
<?php echo $form->textField($model,'sorah_id', $model->topic_id); ?>
<?php echo $form->error($model,'sorah_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'verse_text'); ?>
<?php echo $form->textArea($model,'verse_text',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'verse_text'); ?>
</div>
<?php
$trans = array();
foreach($model->verseTranslations as $p)
{
$trans[$p->id] = $p->translation_text;
}
?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I have an array in my _form.php file.
<?php
$trans = array();
foreach($model->verseTranslations as $p)
{
$trans[$p->id] = $p->translation_text;
}
?>
if i write print_r($trans); Its output like this.
Array ( [3] => In the name of ALLAH, most Gracious, most Compassionate. [4] => شروع الله کا نام لے کر جو بڑا مہربان نہایت رحم والا ہے [5] => En el nombre de Alá, el Compasivo, el Misericordioso )
Now I want to assign this variable i.e. $trans to $model in _form.php file of yii framework, so that i easily access it on controller and then easily submit it to the separate table.
I also want to use it in textArea.
Hope you understand my question.
Any help will be appreciated.
Thanks.
instead of performing this task in view file , you should do it in the controller like
public function actionCreate()
{
$model=new MODELNAME;
// your rest of code
$trans = array();
foreach($model->verseTranslations as $p)
{
$trans[$p->id] = $p->translation_text;
}
// and then you can send this variable to the view by rendering the view file
$this->render('create',array('model'=>$model,'trans'=>$trans));
}
You can not send an array from view to controller, but there is a way (No Professional Way) to send:
put it in a session in your view (_form):
<?php $_SESSION["t"]=$trans;?>
and catch it in controller and then destroy session.
$trans=$_SESSION["t"];
it is better to do such operations in controller itself, instead of doing it in view and pass it to controller!
Add new property on your model like this.
class YOUR_MODEL_WITH_TRANS extends ParentModel
{
public $trans;
---
}
Go to your view file and assign it easily like this.
$model->trans=$trans;

Yii, CactiveForm saving multiples tables in DB

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));

Yii: How to update multiple models using only 1 form 1 model and 1 action?

I have a model website and a model url.
Each url data is attached to a website data. A url is related to a website via a website_id.
On my web app, I need to check the data before accepting it.
I want to see on screen the entire data regarding a url data and I managed to do this.
Now, I also want to update the entire data that is listed on screen.
On screen I have the entire url data and website data, but when I try to save the data, the website data is empty.
My logic was to add to the url model a property: public $website;
And within the url model, a method aftersave:
protected function afterSave() {
$w = null;
$w = Website::model()->findByAttributes(array('id' => $this->website_id));
//echo '<pre>';
//print_r($w);
print_r($this->website);
$w->link = $this->website['link'];
$w->domain = $this->website['domain'];
$w->description = $this->website['description'];
$w->save(false);
die;
return parent::afterSave();
}
and here is the important code from the _form file:
<div class="row">
<?php echo $form->labelEx($model,'will_expire'); ?>
<?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
<?php echo $form->error($model,'will_expire'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'link'); ?>
<?php echo $form->textField($model_website,'link'); ?>
<?php echo $form->error($model_website,'link'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'domain'); ?>
<?php echo $form->textField($model_website,'domain'); ?>
<?php echo $form->error($model_website,'domain'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'description'); ?>
<?php echo $form->textField($model_website,'description'); ?>
<?php echo $form->error($model_website,'description'); ?>
</div>
There are few questions:
Is the relation url and website being defined in the model such that $url->website of a existing url gives you valid website record?
You form shows, you are not using any website_id, means that you are creating a new website record for every URL, means $w = Website::model()->findByAttributes(array('id' => $this->website_id)); will always return NULL
I assume you have defined a relation of website within your URL model.
Suggested function from my understanding:
protected function afterSave() {
//data is already assigned by the caller
$w = null;
$w = Website::model()->findByAttributes(array('id' => $this->website_id));
if($w)
{
Yii::log("updating existing website data","info");
$this->website->save();
}
else
{
Yii::log("creating new website data","info");
$this->website->save();
$this->website_id = $this->website->website_id;
$this->update(array('website_id')); //save the relation
Yii::log("created new website {$this->website_id}","info");
}
return parent::afterSave();
}
It turned out to be small mistakes like w instead of W, object property instead of array;
Ok, so let me teach you how to do this;
I have 2 models: Url and Website.
Url model has a foreign key website_id and a relation to model Website;
Within the Url model I have a property called website and i declared it like: public $website = array();;
The data gets added thru a bookmarklet that uses a API so the data will be there when the admin comes to update/check it;
In the Url model i have a method afterSave:
protected function afterSave() {
$w = null;
$w = Website::model()->findByAttributes(array('id' => $this->website_id));
if($w)
{
$w->link = $this->website['link'];
$w->domain = $this->website['domain'];
$w->description = $this->website['description'];
$w->save();
}
return parent::afterSave();
}
where $this->website gets populated in the UrlController on actionUpdate like:
public function actionUpdate($id, $type = 'update') {
$model = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Url'])) {
$model->attributes = $_POST['Url'];
$model->website = $_POST['Website'];
if ($model->save())
if ($type == 'update')
$this->redirect(array('view', 'id' => $model->id));
else
$this->redirect(array('/admin/url/approvePublicLink'));
}
$model_website = Website::model()->findByAttributes(array('id'=>$model->website_id));
$this->render('update', array(
'model' => $model,
'model_website' => $model_website,
));
}
and gets passed to the afterSave method later;
this is the _update view:
<div style="padding:20px 20px;">
<h1>Update Url, Website, Keywords ...</h1>
<?php echo $this->renderPartial('_form', array(
'model'=>$model,
'model_website' => $model_website,
)); ?>
</div>
and this is the _form view that gets rendered partial:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'url-form',
'enableAjaxValidation'=>false,
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'website_id'); ?>
<?php echo CHtml::link($model->relation_website->domain,$model->relation_website->domain,array('class'=>'avia','target'=>'_blank')); ?>
<?php echo $form->error($model,'website_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'link'); ?>
<?php echo $form->textField($model,'link',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'link'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'important'); ?>
<?php echo $form->dropDownList($model,'important',array(0=>'Normal',1=>'Important')); ?>
<?php echo $form->error($model,'important'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'views'); ?>
<?php echo $form->textField($model,'views'); ?>
<?php echo $form->error($model,'views'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'created'); ?>
<?php echo $model->created; ?>
<?php echo $form->error($model,'created'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'updated'); ?>
<?php echo $model->updated; ?>
<?php echo $form->error($model,'updated'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'will_expire'); ?>
<?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
<?php echo $form->error($model,'will_expire'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'link'); ?>
<?php echo $form->textField($model_website,'link'); ?>
<?php echo $form->error($model_website,'link'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'domain'); ?>
<?php echo $form->textField($model_website,'domain'); ?>
<?php echo $form->error($model_website,'domain'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model_website,'description'); ?>
<?php echo $form->textField($model_website,'description'); ?>
<?php echo $form->error($model_website,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'status'); ?>
<?php echo $form->dropDownList($model,'status',array(-1=>'Banned',0=>'Normal',1=>'Active')); ?>
<?php echo $form->error($model,'status'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I hope it helps.

Categories