Menus

Thursday 1 February 2024

How to get relation value in index in yii2 and Filter


How to get relation value in index in yii2


Certainly! When using Yii2, the convention is to create a separate search model for handling search and filtering logic. Here's a basic example of a PostSearch model with filtering capabilities for the author.name attribute:

Assuming you have a Post model with a hasOne relation to Author:

  1. Create a PostSearch model:

// models/PostSearch.php

namespace app\models;

use yii\base\Model;

use yii\data\ActiveDataProvider;

class PostSearch extends Post

{

    public $authorName;

    public function rules()

    {

        return [

            [['id', 'author_id'], 'integer'],

            [['title', 'content', 'authorName'], 'safe'],

        ];

    }


    public function search($params)

    {

        $query = Post::find()->joinWith('author');

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'sort' => [

                'attributes' => [

                    'id',

                    'title',

                    'content',

                    'authorName' => [

                        'asc' => ['author.name' => SORT_ASC],

                        'desc' => ['author.name' => SORT_DESC],

                    ],

                ],

            ],

        ]);

        $dataProvider->sort->attributes['authorName'] = [

            'asc' => ['author.name' => SORT_ASC],

            'desc' => ['author.name' => SORT_DESC],

        ];

        $this->load($params);

        if (!$this->validate()) {

            return $dataProvider;

        }

        $query->andFilterWhere(['like', 'author.name', $this->authorName]);

        // Your other filtering logic goes here


        return $dataProvider;

    }

}


In your views/post/index.php, you can now use the GridView widget with the search model:

// views/post/index.php

use yii\grid\GridView;

// Other view code

<?= GridView::widget([

    'dataProvider' => $dataProvider,

    'filterModel' => $searchModel,

    'columns' => [

        // Your other columns


        [

            'attribute'=>'authorName'

            'value'=> 'author.name',

            'label' => 'Author Name',

        ],

        // Other columns

    ],

]); ?>


  1. In your Post model, ensure you have defined the relation to Author:
// models/Post.php

namespace app\models;

use yii\db\ActiveRecord;

class Post extends ActiveRecord
{
    // Your other attributes and methods here

    // Define the relation to Author

    public function getAuthor()
    {
        return $this->hasOne(Author::class, ['id' => 'author_id']);
    }

}


In your PostController, use the PostSearch model in the actionIndex method:

// controllers/PostController.php

namespace app\controllers;

use Yii;

use yii\web\Controller;

use app\models\PostSearch;

class PostController extends Controller

{

    public function actionIndex()

    {

        $searchModel = new PostSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }

}


 

Wednesday 20 September 2023

Yii2 Gridview Dataprovider create with relationship

 

In Yii2, if you want to create a GridView with a DataProvider that includes related models and additional select columns, you can do the following:

  1. Create a Query with Related Models and Additional Select Columns:

$query = YourModel::find()

    ->select(['your_table.*', 'related_table.column AS alias'])

    ->joinWith('relatedRelation')

    ->where(['your_condition'])

    ->asArray();


Create a DataProvider using the Query:


$dataProvider = new \yii\data\ActiveDataProvider([

    'query' => $query,

    'pagination' => [

        'pageSize' => 20, // Set your desired page size

    ],

]);


Configure GridView to use the DataProvider:


<?= \yii\grid\GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'attribute1',
        'attribute2',
        // ... other attributes from your model ...
        'alias', // Access the added select column
        [
            'class' => 'yii\grid\ActionColumn',
            // ... configure action buttons ...
        ],
    ],
]); ?>


Make sure to replace 'your_table', 'related_table', 'alias', 'relatedRelation', and 'attribute1', 'attribute2' with actual table and attribute names from your application.

Keep in mind that this assumes you have properly set up your models, relations, and database tables in Yii2.

Remember to adjust the column names, relations, and conditions to match your specific application. This code serves as a template and may need to be customized based on your specific requirements.

Monday 31 July 2023

Number System Conversion



 Decimal to Octal




















Saturday 22 October 2022

How to Swap Two Arrays In C Language

 

#include<stdio.h>

void main()

{


  int a[5] = {5,6,7,8,9};

  int b[5] = {1,2,3,4,5};

  

  int i,swap;


  for(i=0;i<5;i++)

  {


    swap = a[i];

    a[i] = b[i];

    b[i] = swap;


  } 


  printf("A = ");

  for(i=0;i<5;i++)

    printf("%d ",a[i]);


  printf("\nB = ");

  for(i=0;i<5;i++)

    printf("%d ",b[i]); 


  printf("\n");

}







Saturday 23 April 2022

Largest of three numbers in C

 #include <stdio.h>

int main()

{

 int x,y,z;

 clrscr();

 printf("Enter three numbers :");

 scanf("%d,%d,%d",&x,&y,&z);


 if(x>y)

 {

   if(x>z)

     printf("Largest value : %d ",x);

   else

     printf("Largest value : %d ",z);

 }

 else

 {

   if(y>z)

     printf("Largest value : %d",y);

   else

     printf("Largest value : %d",z);

 }


 getch();

}

Saturday 5 March 2022

C language MCQ (Multiple Choise Questions)

Art of C Programming


1 . Address stored in the pointer variable is of type __________
 
 
 
 

 

2. "*" is called as ___________
 
 
 
 


3. Comment on the following pointer declaration?

     int *ptr, p;
 
 
 
 


4. Which of the following are correct syntaxes to send an array as a parameter to function

 
 
 
 


5. Which of the following is not possible in C?

 
 
 
 


 

Answers

1. Integer

2.  

3. ptr is a pointer to integer, p is not.

4. func(&array);

5. None of the mentioned



Wednesday 23 February 2022

Printing Pattern in C | Inverted Half Pyramid

 


#include<stdio.h>

void main()

{

int i,j,r=6;

clrscr();


  for(i=1;i<=r;i++)

  {

   for(j=r;j>=i;j--)

   {

     printf("*");

   }

   printf("\n");

  }


getch();

}