I have a Table KHDN references table Loaihinh via foreign key loaihinh_ten
Table Loaihinh:
ten int identity primary key; //
loai varchar(100);
Table KHDN:
...fields
loaihinh_ten: foreign key references Loaihinh(ten)
What should i do if i want to show Loaihinh. loai instead of the foreign key loaihinh_ten in khdn/views.php Yii2:
<?php
use yii\widgets\DetailView;
/* #var $this yii\web\View */
/* #var $model common\models\Khdn */
?>
<div class="khdn-view">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'ten:ntext',
'chudautu:ntext',
'ngaybatdau:ntext',
'ngayhoanthanh:ntext',
'giatri:ntext',
'trangthai_ten',
'diachi:ntext',
'ghichu:ntext',
'loaihinh_ten',
],
]) ?>
</div>
thanks you so much!!
Use
$model->relationName->fieldName
Example:
/**
* #return \yii\db\ActiveQuery
*/
public function getLoaihinh()
{
return $this->hasOne(KHDN::className(), ['id' =>
'loaihinh_ten']);
}
Then use the following relation in View:
$model->loaihinh->loai;
There are many method of doing this things. you can use any of the following methods for this problem.
you need not to use joinWith method for this you can get that relation data any where in your program just using
$model->relation_name_decleared_in_model
below is the sample code that is use a
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'type',
'value' => function ($model) {
return \app\models\BlockType::getConstantMessage($model->type);
},
],
[
'class' => 'kartik\grid\EditableColumn',
'header' => 'POSITION',
'attribute' => 'position',
],
[
'attribute' => 'access',
'value' => function ($model) {
return \app\components\Helper::ACCESS[$model->access];
},
],
'some_attribute',
[
'attribute' => 'other_relation',
'value' => 'model.relation.column'
],
[
'attribute' => 'Page',
'value' => function ($model) {
return $model->page->pageContent->name;
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Related
I have install the miloschuman\highcharts\Highcharts, website : https://www.yiiframework.com/extension/yii2-highcharts-widget. I have a database with the table columns lme_price and lme_name which I want to display the price of the copper in the highcharts. I'm using PHP.
Below is my code which I have done. This is the code in my models. I create a static function with the query inside it to find the data that I want from the database.
public static function getCopperPrice()
{
$getCopperPrices = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->all();
return $getCopperPrices;
}
Here is my code in the view. I have a table show in the view which show every data from the database.
<div class="lme_index">
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'searchModel'=> $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=> 'DATE',
'attribute' => 'lme_title',
],
[
'label'=> 'MATERIAL',
'attribute'=> 'lme_name',
],
[
'label'=> 'PRICE (US$)',
'attribute'=> 'lme_price',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Here is the code for the highcharts graph.
<div class = "graph">
<?= Highcharts::widget([
'options' => [
'title' => ['text' => 'Copper Price'],
'xAxis' => [
'categories' => []
],
'yAxis' => [
'title' => ['text' => 'Price $US']
],
'plotOption' => [
'series' => [
['name' => 'Copper',
'data' => [lme::getCopperPrices()]],
]
]
]
]);?>
I want to display only 1 material price in the highcharts. I call the function from the model inside the series but the graph shows nothing on it. The coding there didn't show any error to me.
Can someone help me or tell me how to solve this? Thanks.
Chart require integer values to display
public static function getCopperPrice()
{
$cooperPrices = [];
$copperPriceList = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->column();
foreach($copperPriceList as $price) {
$cooperPrices[] = (int) price;
}
return $cooperPrices;
}
I'm trying to modify a field of the GridView that I obtained following the Gii tutorial on the Yii framework website.
GII PAGE
I'm not satisfied on how the population field looks, so I'm trying to convert it with some separators.
This is the index.php of the Country View
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'label' => 'Population',
'value' => 'population',
/* 'value' => Yii::$app->formatter->asDecimal((integer)population) */
/* 'value' => Yii::$app->formatter->asDecimal($model->population) */
/* 'value' => Yii::$app->formatter->asDecimal('population') */
/*'value' => Yii::$app()->format->number('population')*/
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
I don't know why in the CountryModel the population field is listed as integer:
['population'], 'integer'
And then when I try to convert it in the view I have some problems because 'population' is basically a String.
I commented out some of my attempts.
You can use yii\i18n\Formatter
Go to your common\config\main.php if you are using app-advanced or the app/config/main.php if app-basic and add the following under components array.
'formatter' => [
'thousandSeparator' => ',',
],
Now you can format any given number like below
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'attribute' => 'population',
'label' => 'Population',
'value' => function($model){
return Yii::$app->formatter->asInteger($model->population); //Output 1,000
},
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
Update: Like was mentioned in comments, You can also use this two technics to format the value:
//Gridview
....
[
'attribute' => 'population',
'format' => 'integer',
],
....
and/or:
//Gridview
....
'population:integer',
....
I want to show staff has many hobbies from table hobbies in detail view and gridview.
But I got an error exception Trying to get property of non-object
Here's my schema code model:
app\model\TblDataStaff
....
public function getTblDataHobis()
{
return $this->hasMany(TblDataHobies::className(), ['id_staff' => 'id']);
}
view code: view.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
...
['attribute'=>'namHob','value'=>$model->tblDataHobis->id],
...
],
]) ?>
index:
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
......
['attribute'=>'namHob','value'=>function($namHob){return $namHob->tblDataHobis->name_hobby;},],
.....
['class' => 'yii\grid\ActionColumn'],
],]);?>
How to show many hobbies of staff ?
Nothing strange, you have a Trying to get property of non-object error simply because $model->tblDataHobis return an array of TblDataHobies objects.
You could simply try this :
// display hobbies names separated with commas
echo implode(', ', \yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby'));
For DetailView :
'value' => implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
For GridView :
'value' => function($model) {
return implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
},
Try with:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'header' => 'number of hobbies',
'value' => function($data) {
return $data->getTblDataHobis()->count();
}
]
]) ?>
Just get the item data, and after that create a simple yii2 query.
For example my 'worker' has city id, with this id we can find city name by this id.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name:ntext',
'surname:ntext',
'fathers',
[
'attribute' => 'city_id',
'value'=> function($data){
$city = City::find()->where(['id'=>$data->city_id])->one();
$info = $city->name;
return $info;
},
'format'=>'html',
],
'status',
'street',
'in_company',
],
]) ?>
have relation
model Shop.php
public function getShopAddr()
{
return $this->hasOne(SprShopAddr::className(), ['id' => 'shop_addr_id']);
}
model SprShopAddr.php
public function getDivision()
{
return $this->hasOne(SprDivision::className(), ['id' => 'division_id']);
}
model SprDivision.php
public function getShopAddrs()
{
return $this->hasMany(SprShopAddr::className(), ['division_id' => 'id']);
}
view index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'summary' =>false,
'columns' => [
'location_code',
[
'label' => 'Дивизион',
'attribute' => 'division_id',
'value' => 'shopAddr.division.division'
],
['class' => 'yii\grid\ActionColumn', 'template' => '{update}{delete}'],
]
]); ?>
sort on gridview for field shopAddr.division.division not working. How to fix it?
for related model you must configure properly the setSort function of the dataProvider
You can find the right information in this tutorial.
The most important part is that you must define the $dataProvider->setSor(...) function in yourModelSearch like this
$dataProvider->setSort([
'attributes' => [
'yuorRelatedFieldName' => [
'asc' => [ $tableRelated . '.yourField' => SORT_ASC ],
'desc' => [ $tableRelated . '.yourField' => SORT_DESC ],
'label' => 'yuorLabel'
],
],
'defaultOrder' => ['yuorDefaultOrderField' => SORT_ASC],
]);
i have Gridview in index i want to show width and height both in one column how can i do it
here is the view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_name',
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => 'fldMaterial.fld_name',
],
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => 'fldSize.fld_width',
],
// 'fld_size_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
i have relation fldSize in model here it is just only displaying fld_width i want to show it in the format fld_width."x".fld_height how can i do it in Yii2
You should simply use value callback, e.g. :
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => function ($model) {
return $model->fldSize->fld_width . 'x' . $model->fldSize->fld_height;
},
],
Sorry that after more than one year, but it works (not a dropdown but Select2).
Here is the code for the form
<?= $form->field($model, 'ID_MACH')->widget(Select2::classname(), [
'data'=> ArrayHelper::map(Maschines::find()->all(),'ID_MACH','fullname'),
'language'=>'ru',
'theme'=>'krajee',
'options'=>['placeholders'=>'suda...',
'prompt'=>'10-'],
'pluginOptions'=>[
'allowclear'=>true
],
Next is the Model for Mashines:
public function getFullName()
{
return $this->customer.','.$this->Manufacturer.','.$this->type.','.$this->serial;}