**i use model function in detail view how i use function in detail view **
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'title',
'demand',
'sell_for',
'property_category',
'detail',
[
'attribute' => 'dealer_id',
'format'=>'raw',
'value'=> function ($model) {
return Dealer::getName($model->dealer_id);
}
],
],
]) ?>
**i use model function in detail view how i use function in detail view **
In DetailView value don't need anonymous function but only the assign ..
[
'attribute' => 'dealer_id',
'format'=>'raw',
'value'=> Dealer::getName($model->dealer_id);
],
see yii2 doc
return Dealer::getName($model->dealer_id)
Write out the code inside the getName function. If you want to get the name of the dealer using its id, then I think you should use the yii\db\ActiveQuery to sort that out. Lemme see your getName function
Related
To show my GridView I use this ActiveDataProvider:
public function search($params)
{
$query = PublicationsPublication::find()
->select(['eid', 'title', 'pubdate', 'citedby', "STRING_AGG(DISTINCT(CONCAT(author.authid, ' - ', authname)), ', ') AS authors"])
->joinWith('publicationsAuthor')
->groupBy(['eid','title','pubdate','citedby']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
...
}
I can't figure out how to use the column generated by the STRING_AGG() function in the Gridview.
Just in case is needed, the publicationsAuthor relation is coded this way:
public function getPublicationsAuthor() {
return $this->hasMany(PublicationsAuthor::className(), ['authid' => 'authid'])
->viaTable('publications.pub_author', ['pubid' => 'id']);
}
I need to use the STRING_AGG() function because I want to show many authors in one cell of the Gridview.
I tried to use the "authors" column in this way:
$gridColumns = [
[
'class' => 'kartik\grid\SerialColumn',
'width' => '20px',
],
'eid',
'title',
'pubdate',
'citedby',
'authors',
];
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last'
],
...
]);
But unfortunately it didn't work. In the Grid all the values are set to "not set". The query works great because I tested it in PgAdmin.
yii\data\ActiveDataProvider works with models so only fields defined by model are available by default. The easiest way to add field generated by some expression is to add public property with same name to your model like this:
class PublicationsPublication extends ActiveRecord
{
public $authors;
// ... other code in PublicationsPublication model ...
public function attributeLabels()
{
// You can also add label for new field
return [
'authors' => 'Authors',
// ... other labels ...
];
}
}
The public property $authors will be loaded with data from field authors in the result of your SQL query. Then you can use it in grid as any other field.
The other option is to use yii\data\SqlDataProvider instead of yii\data\ActiveDataProvider.
I would like to define the format of an attribute in GridView based on a db field value. I was trying for example like this:
'format' => function ($model) {return $model->format;}, // it should return 'boolean'
but I have tried many other ways also, but it's not working. I'm getting:
PHP Notice – yii\base\ErrorException
Trying to get property of non-object
If I'm simply returning format as an attribute, it's working without any issues. It seems that it's not accepting it in the format section.
Is it possible anyways what I'm trying to achieve? Can you please point me to the right direction?
Closures for format are not supported. You may use raw format and do formatting in Closure for value:
[
'attribute' => 'name',
'type' => 'raw',
'value' => function ($model) {
$format = 'as' . ucfirst($model->format);
return Yii::$app->formatter->$format($model->name);
},
],
Refer Yii2 Formatter
Example:
<?= GridView::widget([
'id' => 'grid-list',
'dataProvider' => $dataProvider,
[
'attribute' => 'format_date',
// 'format' => 'raw',
'value' => function ($model) {
// Here use \Yii::$app->formatter->asDate();
// Ex: return \Yii::$app->formatter->asDate($model->format_date);
return \Yii::$app->formatter->asDate($model->format_date);
},
],
]) ?>
I'm using wbraganca dynamic forms. I have these codes in my controller :
public function actionView($id)
{
$rackObjects = RackObjects::find()->where(['rackID' => $id])->one();
if (!$rackObjects) {
throw new NotFoundHttpException('Empty rack');
}
return $this->render('view', [
'model' => $this->findModel($id),
'rackObjects' => $rackObjects,
]);
}
my view file :
<div class="col-lg-6">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'rackID',
'height',
'width',
],
]) ?>
</div>
<div class="col-lg-6">
<h3>Objects in <?= $this->title ?></h3>
<?= DetailView::widget([
'model' => $rackObjects,
'attributes' => [
'ObjectTitle',
'objectID',
'rack_unit',
'Attached'
],
]) ?>
</div>
I want to have all my rackObjects in multiple detailview not only one detailview like this example : http://wbraganca.com/yii2extensions/dynamicform-demo1/view?id=721
how should I write the codes like example? There is no codes in demos!
$rackObjects = RackObjects::find()->where(['rackID' => $id])->all();
probably as u have already tried all(),
i'd suggest a try in your view file along with querying all()
foreach ($rackObjects as $rackObject) {
<?= DetailView::widget([
'model' => $rackObject,
'attributes' => [
'ObjectTitle',
'objectID',
'rack_unit',
'Attached'
],
]) ?>
}
what it does is, when you have queried all, you get an array of objects and you might want to loop your widget over each of the objects. and gives you multiple detailViews as you desire.
hope this helps.
You are returnig only one row, as you specify
$rackObjects = RackObjects::find()->where(['rackID' => $id])->one();
Replace that with ->all();
Modify value of columns in you detail view if you want everything in one grid.
Amir zaghari.
I'm newbie in YII2 and I use wbraganca\dynamicform\DynamicFormWidget;
I try to follow your step to show data in Detailview but I got an error.
Undefined variable: id
maybe I made something wrong. so can you explain how to coding in
public function actionView($id)
in controller.php
and
DetailView::widget
to display data in _view.php
thank you, sir
I am trying to access a variable on view file into Gridview but it is throwing error that it should be array but null given i am declaring $totalDays on top of my view file and i am using it in Gridview like below
[
'attribute' => 'class_id',
'format' => 'raw',
'label' => "Class Date",
'value' => function ($model) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
'footer'=> '<span>Total Days</span>',
],
But it throws following error
array_push() expects parameter 1 to be array, null given
To explain, $totalDays is not available in the widget because the whole function only gets run when the widget is rendered, $totalDays is no longer declared. As #arogachev hinted above, you will need to make $totalDays in your model, then you can access it. Try this in your model;
public function getTotalDays(){
//Your logic here to generate totalDays
return $totalDays;
}
Then you can use it in your view like this;
[
'attribute' => 'class_id',
'format' => 'raw',
'label' => "Class Date",
'value' => function ($model) {
array_push($model->totalDays, totalDays,$model->class->date);
return $model->class->date;
},
'footer'=> '<span>Total Days</span>',
],
Insert use after function signature in value callable function:
[
'attribute' => 'class_id',
'format' => 'raw',
'label' => "Class Date",
'value' => function ($model) use($totaleDays) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
'footer'=> '<span>Total Days</span>',
],
As #Fabrizio said. Insert use after function signature in value callable function:
'value' => function ($model) use($totaleDays) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
For multiple value to pass in to value function you can use as below.
'value' => function ($model) use($var1, $var2....., $varN) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
Like this use($var1, $var2....., $varN)
While you can definetely pass this variable through use section of closure, it's wrong in your code. MVC principle is violated, because view it's only for display, and you exposing logic such as array_push.
I'd recommend to refactor it and place data calculation in model, so you can simply call return $model->yourMethod(); and it will return desired data.
I am beginner. I explain the topic:
there is this relationship in the Ticket model:
public function getTyp()
{
return $this->hasOne(Typology::className(), [ 'id' =>'typ_id']);
}
and in the ticket table there is the typ_id column (it is in relationship with the id of the Typology table).
In the view views/ticket/index.php there is GridView::widgetwith these columns:
[
'attribute' => 'typ_id',
'value' => 'typ.typology'
],
I want to anchor the value of the relationship.
I have tried this:
[
'attribute' => 'typ_id',
'value' => function ($model) {
return Html::a (
'typ.typology',
'/typology/view?id='.$model->typ_id
);
}
]
but it doesn't work
someone can help me?
Html::a() interprets typ.typology as raw string. Use $model in value closure to get necessary property through relation.
Also instead of manually concatenate the url with its parameters, just pass them in array (see Url::to() to understand how link is constructed).
[
'attribute' => 'typ_id',
'value' => function ($model) {
return Html::a($model->typ->typology, ['/typology/view', 'id' => $model->typ_id]);
},
],