How to load selected list items in multiple-select-listbox in update view in yii? - yii

I have a multiple select-list-box for Staff in Create-Service-Form, used to select multiple staff when creating a new service. for this i can assign multiple staff on a single service.
I saved staff_id field as:
$model->staff_id = serialize($model->staff_id);
Here the update-view code for multiple-select-list-box:
<div class="row">
<?php echo $form->labelEx($model,'staff_id'); ?>
<?php
$data = array('1' => 'Sam', '2' => 'john', '3' => 'addy');
$htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'multiple');
echo $form->ListBox($model,'staff_id', $data, $htmlOptions);
?>
<?php echo $form->error($model,'staff_id'); ?>
</div>
Problem is, when i load form for updating a service. how do i select those staff, which are previously saved in database?
I tried this dropDownList-attributes, but it not working.
$select | string | the selected value
if someone has solution, then suggest me. Thanks All Mates...

Here's a quick code I wrote for you, its an example that will help you understand how it works.
<div class="row">
<?php echo $form->labelEx($model,'staff_id'); ?>
<?php
$data = array('101' => 'Faraz Khan', '102' => 'Depesh Saini', '103' => 'Nalin Gehlot', '104' => 'Hari Maliya');
$selected = array(
'102' => array('selected' => 'selected'),
'103' => array('selected' => 'selected'),
);
$htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'true', 'options' => $selected);
echo $form->listBox($model,'staff_id', $data, $htmlOptions);
?>
<?php echo $form->error($model,'staff_id'); ?>
</div>
Have Fun Ya !!!

Related

Wordpress orderby title using menu_order instead

I want to retrive custom post and want to sort it by title. However when i made a dump of what exact SQL request is sent, i found out the orderby is using menu_order instead of title
Here's the code:
$args=array(
'post_type' => 'custom_post',
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
'posts_per_page' => '-1',
);
Heres the dump
"SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'custom_post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.menu_order ASC "
Hence when i retrive the custom posts, its not in the order i want it to be.
Your help is appreciated
You can create new WP_Query instance to achieve the exact expected result.
<?php
$args = array(
'post_type' => 'custom_post_type',
'order' => 'ASC',
'orderby' => 'title',
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="smal-sec">
<h3><?php the_title();?></h3>
<?php the_content();?>
</div>
<?php
endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
This query will help you to display all posts order by title (A -> Z). Please make sure no such plugins are there, which is actually overwriting the WP_Query instance.

Is it possible to have 2 different CGridView in one admin.php in Yii?

Is it possible to have 2 different CGridView Tables in one admin.php ?
For example, I have a page of Services and a page of Packages.
Services are basically individual single services whereas Packages consist of individual services.
So, my question is, can I have the Service's CGridView to be displayed in Package/admin.php page? A seperate CGridView table.
Top part with list of Packages, and at the bottom, a different table, with Individual services.
If so, please guide me through it. Thanks in advance.
Updated
public function actionAdmin() {
$model = new Package('search');
$model2 = new Service('search');
$model->unsetAttributes();
$model2->unsetAttributes();
$model->active=1;
$model2->active=1;
if (isset($_GET['Package'])){
$model->setAttributes($_GET['Package']);
}
$this->render('admin', array(
'model' => $model,
'model2' => $model2,
));
}
Under _form.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'service-grid',
'dataProvider' => $model2->search(),
'htmlOptions'=>array('class'=>'grid-view grid-size'),
'filter' => $model2,
'columns' => array( //the appropriate columns
));
Yes this is possible and you can have any number of grid views in one page/action.
here is a example where i display two gridviews, namely Manage Subjects 1 & Manage Subjects 2
<?php
/* #var $this SubjectController */
/* #var $model Subject */
$this->breadcrumbs = array(
Yii::t('edu', 'Subjects') => array('index'),
Yii::t('edu', 'Manage'),
);
?>
<?php echo $this->renderPartial('application.views.layouts._actions', array('model' => $model)); ?>
<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('data-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h3><?php echo Yii::t('edu', 'Manage Subjects 1'); ?></h3>
<!-- search-form -->
<div class="search-form" style="display:none">
<p>You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.</p>
<?php $this->renderPartial('_search', array('model' => $model)); ?>
</div>
<!-- search-form -->
<?php echo $this->renderPartial('_grid', array('model' => $model)); ?>
<h3><?php echo Yii::t('edu', 'Manage Subjects 2'); ?></h3>
<!-- search-form -->
<div class="search-form" style="display:none">
<p>You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.</p>
<?php $this->renderPartial('_search', array('model' => $model)); ?>
</div>
<!-- search-form -->
<?php echo $this->renderPartial('_grid', array('model' => $model)); ?>
Update
you need to create 2 models, see my below code, model2 is created and its a different table. you pass it to admin.php and in the gridview change model to model2 in your second gridview. thats all.
/**
* Manages all models.
*/
public function actionAdmin()
{
$model = new Subject('search');
$model2 = new Institute('search');
Yii::app()->appLog->writeLog('Manage Subjects.'); // Activity log entry
$model->unsetAttributes(); // Clear any default values
$data = TK::get('Subject');
if ($data !== null)
$model->attributes = $data;
$params = array('model' => $model, 'model2' => $model2);
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('_grid', $params);
else
$this->render('admin', $params);
}

"Object of class Accounts could not be converted to string" in Yii dropDownList

similarly I have 2 tables in database,accounts and banks,and One to Many Relation between them.
'banks' => array(self::BELONGS_TO, 'Banks', 'banks_id'), in Accounts model.
I need to use Chtml::dropDownList in account creation form,so I edit _form.php to it:
<div class="row">
<?php echo $form->labelEx($model,'banks_id'); ?>
<?php echo CHtml::dropDownList($model, 'banks_id', CHtml::listData(Banks::getBanksList(), 'id', 'title','country')); ?>
<?php echo $form->error($model,'banks_id'); ?>
</div>
and also in Banks model develop `getBanksList by:
public static function getBanksList()
{
$userLanguage = Yii::app()->user->getState('lang');
$result = array();
$banks = self::model()->findAll();
foreach ($banks as $bank){
array_push($result, array(
'id' => $bank->id,
'title' => ($userLanguage == $bank->default_lang)?$bank->default_name:$bank->en_name,
'country' => Yii::t('countries',$bank->country),
));
}
return $result;
}
but it raise Object of class Accounts could not be converted to string error ,and in trace it occured at
public static function getIdByName($name)
{
return str_replace(array('[]', '][', '[', ']', ' '), array('', '_', '_', '', '_'), $name);
}
in Chtml.php file.where is the problem?I searched a lot,but I couldn't solve the problem.
I understood where is my problem,I should use $form instead of Chtml.

ZF2 form element description

in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>
Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }
If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here

View another attribute of related table - yii

I want to display country name instead of country id in user profile view( country id is FK in user profile tbl) .
anyone can help me with this ?
This is my view controller/action in which I have Country model as well:
public function actionView($id)
{
$model = new TblUserProfile;
$model = TblUserProfile::model()->find('user_id=:user_id', array(':user_id' => $id));
$countrymodel = new Country;
$countrymodel = Country::model()->findAll();
// var_dump($countrymodel->name);
// die();
$this->render('view', array(
'model' => $model ,
'country' =>$countrymodel
));
}
This is my view
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('user_id')); ?>:</b>
<?php echo CHtml::encode($data->user_id); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('user_occuption')); ?>:</b>
<?php echo CHtml::encode($data->user_occuption); ?>
<br />
<b><?php
// $model = TblUserProfile::model()->find('country_id=:country_id', array(':user_id' => $id));
//echo CHtml::encode($model->getAttributeLabel('country')); ?>:</b>
<?php// echo CHtml::encode($model->name);
?>
<br />
</div>
Now I want to display country name in above view.
These are the relationships of country and user profile table
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(
'user' => array(self::BELONGS_TO, 'TblUser', 'user_id'),
'country' => array(self::BELONGS_TO, 'country', 'country_id'),
'state' => array(self::BELONGS_TO, 'state', 'state_id'),
'city' => array(self::BELONGS_TO, 'city', 'city_id')
);
}
Use the following code:
<b><?php echo CHtml::encode($data->country->getAttributeLabel('name')); ?>:</b>
<?php echo CHtml::encode($data->country->name); ?>
<br />
Provided that in your country model, the country name column is name.
Since the relation is in place we can use it to access the related country for each user.
In detailview it'll change to:
// instead of 'country_id'
array(
'name'=>'country_id',
'value'=>$model->country->name
)
Read the CDetailView doc to see how you can change even more things.
I would use:
$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));
with('country') explained: the string country comes from the array relations key.
Then to display in the view you would use:
$model->country->CountryName;
$model contains
country contains a "model" of country joined by the relation foreign key
CountryName is the column name in the Country table
This is the recommended way to do it in the MVC way. Both my example and the one above work, but the one above has more process/logic decisions in the view, which breaks the concept of MVC where the Model should be the fat one and contain all logic/processing possible, then controller will pull this data and provide it to the view.
You can also follow this below answer:
Just put the below code in your view.php file
[
"attribute"=>"User_Country_Id",
'value' =>$model->country->conName ,
],
[
"attribute"=>"User_State_Id",
'value' =>$model->states->stsName ,
],
[
"attribute"=>"User_City_Id",
'value' =>$model->cities->ctName ,
],