PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label kartik-v. Show all posts
Showing posts with label kartik-v. Show all posts

Tuesday, March 8, 2022

[FIXED] How to create a custom action column in kartik gridview

 March 08, 2022     gridview, kartik-v, php, yii, yii2     No comments   

Issue

I am trying to create a column that displays a glyphicon. The glyphicon will link to an url which allows the user to download a file. Any help would be massively appreciated. Current code is as follows:

GridView::widget([
'dataProvider' => $dataProvider,
'pager' => [
    'class' => 'common\widgets\Pagination',
],
'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
        'label' => 'Date',
        'attribute' => 'call_datetime',
        'format' => 'date',
    ],
    [
        'label' => 'Time',
        'attribute' => 'call_datetime',
        'format' => 'time',

    ],
    'call_from',
    'call_to',
    'duration',
    'call_type',
    'extension',

    [
        'label' => 'File',
        'attribute' => 'fname',
        'value' => 'callRecFiles.fname',
    ],

It is the last attribute 'fname' that the user will be downloading.


Solution

Change your fname field array to:

[
    'label' => 'File',
    'attribute' => 'fname',
    'value' => function($model) {
         //here create glyphicon with URL pointing to your action where you can download file, something like 
         return $model->callRecFiles ? Html::a('Download', ['myController/download-action', 'fname' => $model->callRecFiles->fname]) : null;
    }
],

And prepare proper action to allow user to download file.



Answered By - Yupik
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, February 15, 2022

[FIXED] yii2 Filtering grid with dropdown list from same model

 February 15, 2022     kartik-v, yii, yii2, yii2-advanced-app, yii2-model     No comments   

Issue

I am using the following functions in my Contact model

public static function getParents()
{
    return $this->hasMany(Contact::className(), ['parent_id' => 'id']); 
}

I want to get the return of this array in the filter attribute of my view:

        [
        'attribute' => 'parent_id',
        'value' => function ($model) {
            return $model->parent ? $model->parent->name : null;
        },
        'hAlign' => 'left',
        'vAlign' => 'middle',
        //'filter' => ArrayHelper::map(Contact::find()->where(['<>', 'parent_id', 0])->orderBy('name')->asArray()->all(), 'id', 'name'),
        'filter' => ArrayHelper::map(Contact::parents()->asArray()->all(), 'id', 'name'),
        'filterWidgetOptions' => [
            'pluginOptions' => ['allowClear' => true],
        ],
        'filterInputOptions' => ['placeholder' => 'Parent'],
        'format' => 'raw'
    ],

but I get the following error: Call to undefined method common\models\Contact::parents()

the rest of the relations work fine for example the attribute $model->parent

I was using the following code but it is wrong because dosn't brings the name of the parent_id within the same table but the name of the records with parent_id

'filter' => ArrayHelper::map(Contact::find()->where(['<>', 'parent_id', 0])->orderBy('name')->asArray()->all(), 'id', 'name'),

I managed to fix my filter using 2 queries in the form but is not very elegant:

$subQuery = Contact::find()->select('parent_id')->where(['<>', 'parent_id', 0]);
$parents = ArrayHelper::map(Contact::find()->where(['in', 'id', $subQuery])->orderBy('name')->asArray()->all(), 'id', 'name')

UPDATED AFTER CORRECT ANSWER

I put the logic in the module to reuse it in other places and used getParentsArray instead of just parentsArray

public static function getParentsArray() {
    $subQuery = Contact::find()->select('parent_id')->where(['<>', 'parent_id', 0]);
    $parents = ArrayHelper::map(Contact::find()->where(['in', 'id', $subQuery])->orderBy('name')->asArray()->all(), 'id', 'name');
    return $parents;
}

in views or controllers

$parents = Contact::getParentsArray();

Solution

I think that your problem is this; You are using the method Contact::parents() but this method doesn't exist in your model. You have declared a method getParents(), which allows Yii to use the magic getter method to access the parents as Contact::parents.

There is a subtle but important difference in how Yii uses these two functions which will affect how you use them to generate an array suitable for use in your dropdown list: The first, Contact::parents() uses the relationship you've declared to get all the parent records. It returns an array of models of class Contact, as you've declared in your method.

The second way is to directly call the static method getParents(). This has the advantage that it returns an instance of ActiveQuery, which then allows you to use the asArray() and all() methods associated with ActiveQuery.

So, for your use case, I would put some more code in the model to generate your dropdown. The advantage of this is that it keeps model logic (which this is) inside the model, and it also then becomes reusable in other situations.

So, in your model, create this method;

public static function getParentsArray(){
return ArrayHelper::map(self::getParents()->asArray()->all(), 'id', 'name');
}

You can then use this in your view file like so;

'filter' => Contact::parentsArray()


Answered By - Joe Miller
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing