-1

I want to search in a multi table in yii2. How to do this action?

<?php

namespace app\models;

use Yii;
use yii\db\Query;
use app\models\Article;
use app\models\Certificates;
use app\models\News;
use app\models\Pages;
use app\models\Projects;
use app\models\NewsSearch;

i want search in multi table. that table have not any relation with Together

i want write query in yii2 like this :

select *   from news , article , projects where (any column for this tables ) like %search%
Saltern
  • 1,305
  • 2
  • 16
  • 42

1 Answers1

1

You can do it using relation adding the activeRelation the your main model and then use the relation in a proper search function
eg ( just a brief suggestion) :

/* ActiveRelation */
public function getMyExtModelRelation()
{
   return $this->hasOne(MyExtModel::className(), ['id' => 'ext_id']);
}

and in main modelSearch

/* search function  */ 

 .....
 ....
// filter by MyExtModel attribute
 $query->joinWith(['myExModelRelation' => function ($q) {
     $q->where('tbl_my_ext_model.my_attribute LIKE "%' . $this->my_attribute . '%"');
}]);

Here you can find a good tutorial for common related and calculated search filter and sort http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

I don't understand what you are trynd to do and the result of your query could be huge and of little use but anyway if you want a genaric query you can use

use yii\db\Query;
.....
$connection = \Yii::$app->db;

$any_column  = "your_any_column";
$any_search  = " concat('%', '". $your_search ."', '%'); "
$sql ="select *   from news, article , projects  where " . $any_column  . $any_search ;

$yourModels  = $connection->createCommand($sql);->queryAll();

could be you must assign alias to the column that you use in select for retrive this column from models or use the complete name (tablename.columnname)

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • i want search in multi table. that table have not any relation with Together i updated this post. – Saltern Oct 09 '16 at 07:00
  • SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'concat('%', yes, '%')' at line 1 The SQL being executed was: select * from news, article where news.fTitle concat('%', yes, '%'); !!!!! – Saltern Oct 09 '16 at 08:27