View another attribute of related table - yii - 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 ,
],

Related

show view of a model in another model's view

I have a table name business and a table name review business. In business table, i just have business name, business image and business information. In review business table i have business_id as foreign key, user_id as foreign key and rate(int) five star and review field. Now i am as an admin can give review easily, by selecting a particular user from the drop down list, by choosing a particular business from a drop down list and then give rate and review. I am using dzraty extension. It is working fine. Also i am showing reviews given by admin, on the business page.My question is what if a user, wants to give a review on the business page? for that he needs the five star rating field and review field which is present in review business table, view "form". I want to show rating and review fields on the business page, so that user can write a review and post it. This functionality is working in review business view, i just want it in another view.
For understanding i am posting models data.
Buisness model
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(
'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
'businessItems' => array(self::HAS_MANY, 'BusinessItems', 'business_id'),
'businessPackages' => array(self::HAS_MANY, 'BusinessPackage', 'business_id'),
'facilities' => array(self::HAS_MANY, 'Facilities', 'business_id'),
'reviewBusinesses' => array(self::HAS_MANY, 'ReviewBusiness', 'business_id'),
'subCategoryBusinesses' => array(self::HAS_MANY, 'SubCategoryBusiness', 'business_id'),
);
}
Review business model
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(
'business' => array(self::BELONGS_TO, 'Business', 'business_id'),
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'profile' => array(self::BELONGS_TO, 'Profiles', 'id','through'=>'user'),
);
}
form.php of review business
<?php
/* #var $this ReviewBusinessController */
/* #var $model ReviewBusiness */
/* #var $form BSActiveForm */
?>
<?php $form=$this->beginWidget('bootstrap.widgets.BsActiveForm', array(
'id'=>'review-business-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="help-block">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->labelEx($model,'user_id'); ?>
<?php
$this->widget('ext.select2.ESelect2',array(
'name'=>'ReviewBusiness[user_id]',
'data'=>CHtml::listData(User::model()->findAll(), 'id', 'username'), //the whole available list
'htmlOptions'=>array(
'placeholder'=>' search User name?',
//'options'=>$options, //the selected values
//'multiple'=>'multiple',
'style'=>'width:530px',
),
));
?>
<?php //echo $form->textFieldControlGroup($model,'business_id'); ?>
<div class="gap-small"></div>
<?php echo $form->labelEx($model,'Business_name'); ?>
<?php
$this->widget('ext.select2.ESelect2',array(
'name'=>'ReviewBusiness[business_id]',
'data'=>CHtml::listData(Business::model()->findAll(), 'id', 'business_name'), //the whole available list
'htmlOptions'=>array(
'placeholder'=>' search Business name?',
//'options'=>$options, //the selected values
//'multiple'=>'multiple',
'style'=>'width:530px',
),
));
?>
<div class="gap-small"></div>
<div class="ratings"> <!--use class in order to show rating horizontally -->
<?php
$this->widget('ext.DzRaty.DzRaty', array(
'model' => $model,
'attribute' => 'rating',
)); ?>
</div>
<?php echo $form->textarea($model,'review',array('maxlength'=>500)); ?>
</br>
<?php echo BsHtml::submitButton('Submit', array('color' => BsHtml::BUTTON_COLOR_PRIMARY)); ?>
<?php $this->endWidget(); ?>

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

Yii CActiveForm expects object and not array

My Goal:
I am trying to filter a model, based on a GET parameter, and populate a form based on this.
What I require is that when users select the update icon on the Gridview
I will
grab the 'telephone number' they want to edit,
populate a form with the telephonenumber's data
allow the user to edit this data for the telephonenumber and submit
I then run my own custom sql update query based on the new data.
My Problem
My gridview can successfully capture the telephonenumber for a selected row.
It can successfully send this to the Controller Update method (see below)
I cannot however filter a model based on this telephonenumber and then populate a form with this model.
My Error
get_class() expects parameter 1 to be object, array given
/framework/web/helpers/CHtml.php(2220)
/framework/web/helpers/CHtml.php(2220): get_class(array())
/framework/web/helpers/CHtml.php(1236): CHtml::resolveName(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/widgets/CActiveForm.php(562): CHtml::activeLabelEx(array(), "TelephoneNumbers_TelephoneNumber", array())
/views/dateAudiid/editupdateform.php(18): CActiveForm->labelEx(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/CBaseController.php(126): require("/var/www/OMReport/protected/views/dateAudiid/editupdateform.php")
Here's my Gridview.
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}',
'buttons'=>array
(
'update' => array
(
'url'=>'Yii::app()->createUrl("dateAudiid/Update/",array("TelephoneNumbers_TelephoneNumber"=>$data->TelephoneNumbers_TelephoneNumber))',
),
),
),
'TelephoneNumbers_TelephoneNumber',
'FormId_Formid',
'Date',
'AudibeneID_Audibene_ID'
),
));
Here's my Controller
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$criteria=new CDbCriteria;
$criteria->compare('TelephoneNumbers_TelephoneNumber',$_GET['TelephoneNumbers_TelephoneNumber'],true);
$criteria->limit = 1;
$criteria->offset = 1;
$model = DateAudiidCondensedByAudibeneId::model()->findAll($criteria);
$this->render('editupdate',array('model'=>$model));
}
Here's my View
<?php
/* #var $this DateAudiidController */
/* #var $model DateAudiidCondensedByAudibeneId */
?>
<h1>Update Assignments </h1>
<?php echo $this->renderPartial('editupdateform', array('model'=>$model)); ?>
Here's my Form
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'date-audiid-condensed-by-audibene-id-customupdate-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,'TelephoneNumbers_TelephoneNumber'); ?>
<?php echo $form->textField($model,'TelephoneNumbers_TelephoneNumber'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'FormId_Formid'); ?>
<?php echo $form->textField($model,'FormId_Formid'); ?>
<?php echo $form->error($model,'FormId_Formid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Date'); ?>
<?php echo $form->textField($model,'Date'); ?>
<?php echo $form->error($model,'Date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->textField($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->error($model,'AudibeneID_Audibene_ID'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Thoughts / What I've tried so far
I am returning the model as an array...but my form wants it as an object.
I need to change something in the way I filter my model in the Controller Update action, however I cannot see how to do this.
All my other methods use similar model filtering code.
here is what you can do to fix this :
your form is trying to use models labeling but you have given an array, ->findAll() will return an array of models, here you need a single object of model
the error your having is because $model in filter of your grid needs to be an object of model which here will try to validate using this model, so won't work when you are giving it an array of your models,
if you want to filter the results based on something, you need to do this where the dataprovider of your grid is being populated, so in this case, is in $model->search()
get the parameter and append it to that criteria
so your grid could look like this:
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider' => $model->search(), //create a new model with search scenario
'filter' => $model, // here use that model to validate fields
'columns'=>array(
.
.
.
),
));
and in your models search method:
public function search() {
$criteria = new CDbCriteria;
// grab the sent data and use it here
$tel = Yii::app()->request->getParam('TelephoneNumbers_TelephoneNumber' , null);
if(!empty($tel))
$criteria->compare('TelephoneNumbers_TelephoneNumber' , $tel , true);
.
.
.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Here's what eventually worked for me
The usage of 'findByAttributes' to filter and return a model to the Form
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$model = DateAudiidCondensedByAudibeneId::model()->findByAttributes(array("TelephoneNumbers_TelephoneNumber" => $_GET['TelephoneNumbers_TelephoneNumber']));
$this->render('editupdate',array('model'=>$model));
}

Displaying data from different tables in a view

I have a requirement where I would like to display a list of records and the information from each record can come from a umber of tables. To further explain I have the following tables:
Table srp with columns id (PK), srpname, idbusiness (FK), idsite (FK)
Table business with columns id (PK), businessname
Table site with columns id (PK), sitename
Table srpprimary with columns id (PK), idsrp (FK), pname
Table srpdepname with columns id (PK), idsrp (FK), dname
An srp can have multiple entries in the table srpprimary and one entry in each of the other tables business, site, srpdepname
What I would like is to display an srp record along with all the pname entries in the table srpprimary, the dname from the table srpdepname and the actual business name and site name.
I looked at the CListView but could not see how I could get this additional data.
Any suggestions on how the above could best be achieved would be greatly appreciated.
Kind regards
e25taki
To help you in this question and in your next project I recommended you to use database relations :
1- prraper your datbase
2- write your relations on paper bettwen tables
3- do it (use phpmyadmin for example ):
by GUI methode
How to create a relation between two tables using PHPMyAdmin?
https://www.youtube.com/watch?v=IdQGFZwP7Xc
Sql method
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
4- now you can use yii gii to create your code and all you relations will be save in model class
5- load data in view will be so so easy now :
For Example if your model is like :
<?php
..
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(
'co' => array(self::BELONGS_TO, 'Country', 'co_id'),
'events' => array(self::HAS_MANY, 'Events', 'city'),
'news' => array(self::HAS_MANY, 'News', 'city'), /// here
'users' => array(self::HAS_MANY, 'Users', 'city'),
);
}
...
?>
So we can access to all news table that related to current city by call it as :
echo $model->news->title;
It's all about the relations in your models.
With correct relations, you can access data like:
$srp = Srp::model()->findByPk(1);
$sites = $srp->sites; // gets an active record-array with a HAS_MANY-relation
$site = $srp->site; // gets the active record directly with a HAS_ONE-relation
Considering the code generation in Gii, you'll have a much easier time if you rename your foreign keys so they end with "id" or "_id". That way, you'll get sensiblle relations automatically when you generate your models. (Though they still might need to be tweaked a bit to suit your needs.)
With your new relations, you can create a listview like this in your view:
$dataProvider=new CActiveDataProvider('Srp');
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view',
));
And access your data something like this (_view.php) :
<b>Id:</b> // Or better yet: echo CHtml::encode($data->getAttributeLabel('id'));
<?php echo $data->id; ?>
<br />
<b>Business:</b>
<?php echo $data->business->name; ?>
<br />
<b>Site:</b>
<?php echo $data->site->name; ?>
<br />
<b>Depname:</b>
<?php foreach ($data->srpdepnames as $dep): ?>
<?php echo $dep->dname; ?>,
<?php endforeach ?>
<br />
<b>Depname:</b>
<b><?php echo CHtml::encode($data->getAttributeLabel('srpprimary_id')); ?>:</b>
<?php foreach ($data->srpprimaries as $prime ){
echo CHtml::link( // If you want links instead of just text.
CHtml::encode($prime->pname),
array('SrpPrimary/View', 'id'=>$prime->id)
);
}?>
(I haven't tried this, so it's not a working example. Sorry! But it should give you an idea and it's not far from the truth. )
In controller.php
public function actionQueries()
{
$dataProvider=new CActiveDataProvider('Student', array(
'criteria' => array(
'with' =>'student',
'join' => 'INNER JOIN studentinfo si ON si.stud_id=t.id',
)
));
$this->render('query',array(
'dataProvider'=> $dataProvider,
));
}
in a view,in tables model view make php file for ex query.php
<?php
/* #var $this StudentController */
/* #var $model Student */
$this->breadcrumbs=array(
'Students'=>array('query'),
//$user->name,
);
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view', // refers to the partial view named '_post'
'enablePagination'=>true,
'sortableAttributes'=>array(
'name',
)
));
?>
// add your new feilds in _view.php file:
// in controller.php
in accsessRule() add your new file for authentication for ex.queris
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','queries'),
'users'=>array('*'),
),
}

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