best match result query in yii2 - php

i have table named books and want to display "related books" in books view.
books table
`book_id`
`isbn`
`book_name`
`author_name`
`Date`
`number`
`language`
`Translator`
`abstract`
`picture`
`created_at`
controller code
public function actionView($id)
{
$model=$this->findModel($id);
$dataProvider=$this->related($model);
return $this->render('view', [
'model' => $model,
'dataProvider' => $dataProvider,
]);
}
protected function related($model)
{
$query = Books::find();
$query->select('book_id,picture, book_name,author_name,(( author_name LIKE \'%'.$model->author_name.'%\')+( book_name LIKE \'%'.$model->book_name.'%\')) as total')
->orFilterwhere(['or like','author_name' , explode(' ',$model->author_name)])
->orFilterwhere(['or like','book_name' , explode(' ',$model->book_name)])
->orderBy('total')
->limit(25);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
problom :
book_name LIKE \'%'.$model->book_name.'%\' did not accept array.
something like LIKE \'%'.explode(' ',$model->book_name).'%\'
its must be an array to work correctly.
how can i use array in select condition .
or show me some other way to get best match result on top.
tnx

First If you want Total no need to write where condition in select
$query->select('book_id,picture, book_name,author_name,(book_name+author_name) as total');
$model->author_name And $model->book_name is Array So you can do as below.
protected function related($model)
{
$query = Books::find();
$query->select('book_id,picture, book_name,author_name,(book_name+author_name) as total');
$author_names= explode(' ',$model->author_name);
foreach($author_names as $author_name)
{
$query->orFilterwhere(['like','author_name' , $author_name]);
}
$book_names=explode(' ',$model->book_name);
foreach($book_names as $book_name)
{
$query->orFilterwhere(['like','book_name' , $book_name]);
}
->orderBy('total')
->limit(25);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
}

You need to use MySQL match() against() feature (or substitute if you're using different DB).
$query = Books::find();
$query->select([
'book_id,picture', 'book_name', 'author_name',
new Expression('MATCH (author_name) AGAINST (:author_name) + MATCH (book_name) AGAINST (:book_name) AS total', [
'author_name' => $this->author_name,
'book_name' => $this->book_name,
]),
]);
if (!empty($this->author_name)) {
$query->andWhere(new Expression('MATCH (author_name) AGAINST (:author_name)', [
'author_name' => $this->author_name,
]));
}
if (!empty($this->book_name)) {
$query->andWhere(new Expression('MATCH (book_name) AGAINST (:book_name)', [
'book_name' => $this->book_name,
]));
}
$query->orderBy('total DESC')
->limit(25);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

Related

Yii2: checkboxList doesn't show ArrayDataProvider

I want to use a checkboxList to show data from a data provider.
My view file:
$offices = Offices::findMyOffices();
echo Html::checkboxList('name', [], $offices);
My model file:
public static function findMyOffices()
{
$dataProvider = new ArrayDataProvider([
'allModels' => 'SELECT id_office ...'
]);
return $dataProvider;
}
But the view shows me the checkbox list with the sql query instead of the sql query's results:
I solve it using sqlDataProvider:
View:
$offices = Offices::findMyOffices();
echo Html::checkboxList('name', [], ArrayHelper::map($offices, 'id_office', 'name_office'));
Model:
public static function findMyOffices()
{
$dataProvider = new sqlDataProvider([
'sql' => 'SELECT id_office ...'
]);
return $dataProvider->getModels();
}
ArrayDataProvider needs an array of items. you can add ->asArray() to your activequery.
$dataProvider = new ArrayDataProvider([
'allModels' => [['id' => 1, 'title' => 'xxx, ...], ...],
]);
my favorite for fetching data for a dropdown is:
MyModel::find()->select('name', 'id')->indexBy('id')->column()

How to write a mysql query in yii2 search model

I want to write a mysql query in Yii2 search model but when performing searching criteria it gives the errors on joins.
This is my search model.
class StudentRegistrationSearch extends StudentRegistration {
/**
* #inheritdoc
*/
public function rules() {
return [
[['id', 'student_id', 'recordstatus', 'addedbyuserid'], 'integer'],
[[ 'registration_date', 'dateadded', 'let'], 'safe'],
];
}
/**
* #inheritdoc
*/
public function scenarios() {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* #param array $params
*
* #return ActiveDataProvider
*/
public function search($params) {
$query = StudentRegistration::find()->where(['recordstatus' => 1]);
$query = <<<EOD
SELECT
students.student_name,
students.`id`,
students.`reg_no`,
reg.`registration_date`,
exam.`exam_year`,
exam.`exam_title`
FROM students
LEFT JOIN student_registration reg ON (reg.`student_id` = students.`id`)
LEFT JOIN student_reg_detail detail ON(detail.`student_register_id` = reg.`id`)
LEFT JOIN def_exams exam ON(exam.`id` = detail.reg_exam_id)
WHERE students.`recordstatus` = 1 AND reg.`recordstatus` = 1 AND detail.`recordstatus` = 1
ORDER BY exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name; EOD;
$query = Yii::$app->db->createCommand($query);
$query = $query->queryAll();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'student_id' => $this->student_id,
'registration_date' => $this->registration_date,
'recordstatus' => $this->recordstatus,
'dateadded' => $this->dateadded,
'addedbyuserid' => $this->addedbyuserid,
'let' => $this->let,
]);
$query->orderBy('student_id');
return $dataProvider;
}}
I want to show the data from multiple table in the single grid and then perform filter operation but simple query is not working.
Can you please help me someone.
Thanks in advance.
Try something like following
$query = (new yii\db\Query())
->from(['s' => 'students'])
->select(['s.student_name', 's.id', 's.reg_no', 'reg.registration_date', 'exam.exam_year', 'exam.exam_title'])
->leftJoin(['reg' => 'student_registration', 'stu.student_id = s.id'])
->leftJoin(['detail' => 'student_reg_detail', 'stu.student_id = s.id'])
->leftJoin(['exam' => 'def_exams ', 'exam.id = detail.reg_exam_id'])
->where(['s.recordstatus' => 1, 'reg.recordstatus' => 1, 'detail.recordstatus' => 1])
->orderBy('exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name')
;
OR
$query = Students::find()
->from(['s' => Students::tablename()])
->select(['s.student_name', 's.id', 's.reg_no', 'reg.registration_date', 'exam.exam_year', 'exam.exam_title'])
->leftJoin(['reg' => 'student_registration', 'stu.student_id = s.id'])
->leftJoin(['detail' => 'student_reg_detail', 'stu.student_id = s.id'])
->leftJoin(['exam' => 'def_exams ', 'exam.id = detail.reg_exam_id'])
->where(['s.recordstatus' => 1, 'reg.recordstatus' => 1, 'detail.recordstatus' => 1])
->orderBy('exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name')
;
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

Yii2 Querybuilder with searchmodel

My searchmodel
$query = (new \yii\db\Query())
->select(['monthsubmit',"DATE_FORMAT(monthsubmit, '%m-%Y') as c_date", 'modeler', 'count(sku) as count'])
->from('sku3d')
->groupBy(['monthsubmit', 'modeler'])
->orderBy(['monthsubmit'=>SORT_DESC, 'modeler'=>SORT_DESC]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
My controller
$searchModel = new Sku3dSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
My gridview
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel'=>$searchModel,
'pjax'=>true,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-user"></i>Submitted SKU by Month</h3>',
],
'columns' => [
[
'attribute'=>'monthsubmit',
'width'=>'310px',
'filter'=>ArrayHelper::map(Sku3d::find()->orderBy('monthsubmit')->asArray()->all(), 'monthsubmit', 'monthsubmit'),
'group'=>true,
],
[
'attribute'=>'modeler',
'width'=>'180px',
'group'=>true,
],
'count:text:Total Sku',
]
]);
?>
At first I didn't use querybuilder in searchmodel but in controller and everything worked fine. But I need to filter it also so i moved my querybuilder into searchmodel.
When I did this, it had error "Getting unknown property:app\models\Sku3d::count".
How can I call the 'count(sku) as count' to my gridview. And also it look like my groupBy(['monthsubmit', 'modeler']) not working also.
Please tell me where I'm wrong.
Thank you.
In your searchmodel add a publica var
class Sku3dSearch extends Sku3d
{
public $count;
/**
* #inheritdoc
*/
public function rules()
......
$query = (new \yii\db\Query())
->select(['monthsubmit',"DATE_FORMAT(monthsubmit, '%m-%Y') as c_date", 'modeler', 'count(sku) as count'])
->from('sku3d')
->groupBy(['monthsubmit', 'modeler'])
->orderBy(['monthsubmit'=>SORT_DESC, 'modeler'=>SORT_DESC]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
}

Complex Database Queries in yii2 with Active Record

TL;DR
I have a query that works in RAW SQL but i have had little success recreating it with query builder or active record.
I am working on a web application based off of the yii2 advanced application template. I have written a database query and implemented it with findbysql() returning the correct records but am having trouble translating this into active record.
I originally wanted to allow the user to modify (filter) the results by means of a search form(user & date), however i have since realized that implementing filters on the gridview with active records would be smoother.
I have gotten simple queries to work however am unsure of how to implement one with this many joins. Many examples used sub queries but my attempts failed to return any records at all. I figured before I attempt filters i need to transcribe this query first.
videoController.php
public function actionIndex()
{
$sql = 'SELECT videos.idvideo, videos.filelocation, events.event_type, events.event_timestamp
FROM (((ispy.videos videos
INNER JOIN ispy.cameras cameras
ON (videos.cameras_idcameras = cameras.idcameras))
INNER JOIN ispy.host_machines host_machines
ON (cameras.host_machines_idhost_machines =
host_machines.idhost_machines))
INNER JOIN ispy.events events
ON (events.host_machines_idhost_machines =
host_machines.idhost_machines))
INNER JOIN ispy.staff staff
ON (events.staff_idreceptionist = staff.idreceptionist)
WHERE (staff.idreceptionist = 182)
AND (events.event_type IN (23, 24))
AND (events.event_timestamp BETWEEN videos.start_time
AND videos.end_time)';
$query = Videos::findBySql($sql);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
Failed Attempt
public function actionIndex()
{
$query = Videos::find()
->innerJoin('cameras', 'videos.cameras_idcameras = cameras.idcameras')
->innerJoin('host_machines', 'cameras.host_machines_idhost_machines = host_machines.idhost_machines')
->innerJoin('events', 'events.host_machines_idhost_machines = host_machines.idhost_machines')
->innerJoin('staff', 'events.staff_idreceptionist = staff.idreceptionist')
->where('staff.idreceptionist = 182')
->andWhere(['events.event_type' => [23,24]])
->andwhere(['between', 'events.event_timestamp', 'videos.start_time', 'videos.end_time']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
Portion of View
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'idvideo',
'event_type',
'event_timestamp',
'filelocation',
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Please let me know if i need to be more specific or include any additional information.
Thanks ahead
i will assume, based on the question you asked here you liked in comments that you provided the entire query
(no other fields, that you took out just to show sample code)
therefore, if you only need only the fields specified in SELECT statement, you can optimize your query quite a bit:
first off, you're joining with host_machines only to link cameras and events, but have the same key host_machines_idhost_machines on both, so that's not needed, you can directly:
INNER JOIN events events
ON (events.host_machines_idhost_machines =
cameras.host_machines_idhost_machines))
secondly, the join with ispy.staff, the only used field is idreceptionist in WHERE clause, that field exists in events as well so we can drop it completly
the final query here:
SELECT videos.idvideo, videos.filelocation, events.event_type, events.event_timestamp
FROM videos videos
INNER JOIN cameras cameras
ON videos.cameras_idcameras = cameras.idcameras
INNER JOIN events events
ON events.host_machines_idhost_machines =
cameras.host_machines_idhost_machines
WHERE (events.staff_idreceptionist = 182)
AND (events.event_type IN (23, 24))
AND (events.event_timestamp BETWEEN videos.start_time
AND videos.end_time)
should output the same records as the one in your question, without any identitcal rows
some video duplicates will still exists due to one to many relation between cameras and events
now to the yii side of things,
you have to define some relations on the Videos model
// this is pretty straight forward, `videos`.`cameras_idcameras` links to a
// single camera (one-to-one)
public function getCamera(){
return $this->hasOne(Camera::className(), ['idcameras' => 'cameras_idcameras']);
}
// link the events table using `cameras` as a pivot table (one-to-many)
public function getEvents(){
return $this->hasMany(Event::className(), [
// host machine of event => host machine of camera (from via call)
'host_machines_idhost_machines' => 'host_machines_idhost_machines'
])->via('camera');
}
the VideoController and the search function itself
public function actionIndex() {
// this will be the query used to create the ActiveDataProvider
$query =Video::find()
->joinWith(['camera', 'events'], true, 'INNER JOIN')
->where(['event_type' => [23, 24], 'staff_idreceptionist' => 182])
->andWhere('event_timestamp BETWEEN videos.start_time AND videos.end_time');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
yii will treat each video as a single record (based on pk), that means that all video duplicates are
removed. you will have single videos, each with multiple events so you wont be able to use 'event_type'
and 'event_timestamp' in the view but you can declare some getters inside Video model to show that info:
public function getEventTypes(){
return implode(', ', ArrayHelper::getColumn($this->events, 'event_type'));
}
public function getEventTimestamps(){
return implode(', ', ArrayHelper::getColumn($this->events, 'event_timestamp'));
}
and the view use:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'idvideo',
'eventTypes',
'eventTimestamps',
'filelocation',
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
edit:
if you want to keep the video duplicates, declare the two columns from events inside Video model
public $event_type, $event_timestamp;
keep the original GridView setup, and add a select and indexBy this to the query inside VideoController:
$q = Video::find()
// spcify fields
->addSelect(['videos.idvideo', 'videos.filelocation', 'events.event_type', 'events.event_timestamp'])
->joinWith(['camera', 'events'], true, 'INNER JOIN')
->where(['event_type' => [23, 24], 'staff_idreceptionist' => 182])
->andWhere('event_timestamp BETWEEN videos.start_time AND videos.end_time')
// force yii to treat each row as distinct
->indexBy(function () {
static $count;
return ($count++);
});
update
a direct staff relation to Video is currently somewhat problematic since that is more than one table away from it.
there's an issue about it here
however, you add the staff table by linking it to the Event model,
public function getStaff() {
return $this->hasOne(Staff::className(), ['idreceptionist' => 'staff_idreceptionist']);
}
that will allow you to query like this:
->joinWith(['camera', 'events', 'events.staff'], true, 'INNER JOIN')
Filtering will require some small updates on the controller, view and a SarchModel
here's a minimal implementation:
class VideoSearch extends Video
{
public $eventType;
public $eventTimestamp;
public $username;
public function rules() {
return array_merge(parent::rules(), [
[['eventType', 'eventTimestamp', 'username'], 'safe']
]);
}
public function search($params) {
// add/adjust only conditions that ALWAYS apply here:
$q = parent::find()
->joinWith(['camera', 'events', 'events.staff'], true, 'INNER JOIN')
->where([
'event_type' => [23, 24],
// 'staff_idreceptionist' => 182
// im guessing this would be the username we want to filter by
])
->andWhere('event_timestamp BETWEEN videos.start_time AND videos.end_time');
$dataProvider = new ActiveDataProvider(['query' => $q]);
if (!$this->validate())
return $dataProvider;
$this->load($params);
$q->andFilterWhere([
'idvideo' => $this->idvideo,
'events.event_type' => $this->eventType,
'events.event_timestamp' => $this->eventTimestamp,
'staff.username' => $this->username,
]);
return $dataProvider;
}
}
controller:
public function actionIndex() {
$searchModel = new VideoSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('test', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
and the view
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'idvideo',
'filelocation',
[
'attribute' => 'eventType', // from VideoSearch::$eventType (this is the one you filter by)
'value' => 'eventTypes' // from Video::getEventTypes() that i suggested yesterday
// in hindsight, this could have been named better, like Video::formatEventTypes or smth
],
[
'attribute' => 'eventTimestamp',
'value' => 'eventTimestamps'
],
[
'attribute' => 'username',
'value' => function($video){
return implode(', ', ArrayHelper::map($video->events, 'idevent', 'staff.username'));
}
],
//['class' => 'yii\grid\ActionColumn'],
],
]);
My recommendation would be to have 2 queries. The first one to get the ids of the videos that fit your search, the second query theone that uses those ids and feeds your $dataProvider.
use yii\helpers\ArrayHelper;
...
public function actionIndex()
{
// This is basically the same query you had before
$searchResults = Videos::find()
// change 'id' for the name of your primary key
->select('id')
// we don't really need ActiveRecord instances, better use array
->asArray()
->innerJoin('cameras', 'videos.cameras_idcameras = cameras.idcameras')
->innerJoin('host_machines', 'cameras.host_machines_idhost_machines = host_machines.idhost_machines')
->innerJoin('events', 'events.host_machines_idhost_machines = host_machines.idhost_machines')
->innerJoin('staff', 'events.staff_idreceptionist = staff.idreceptionist')
->where('staff.idreceptionist = 182')
->andWhere(['events.event_type' => [23,24]])
->andwhere(['between', 'events.event_timestamp', 'videos.start_time', 'videos.end_time'])
// query the results
->all();
// this will be the query used to create the ActiveDataProvider
$query = Videos::find()
// and we use the results of the previous query to filter this one
->where(['id' => ArrayHelper::getColumn($searchResults, 'id')]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}

Yii2 : how to cache active data provider?

In my PostSearch model I have this code :
public function search($params)
{
$query = Post::find()->where(['status' => 1]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['id' => SORT_DESC]],
'pagination' => [
'pageSize' => 10,
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'status' => $this->status,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'text', $this->text]);
return $dataProvider;
my try, instead of above line return $dataProvider, would be this block of code:
$dependency = [
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT MAX(updated_at) FROM post',
];
$result = self::getDb()->cache(function ($db) {
return $dataProvider;
}, 3600, $dependency);
return $result
I would like to cache the result returned by ADP, based on the updated_at field. I mean I want to serve data from cache until some change is made. My code does not work, I mean caching is not applied at all. What I am doing wrong, and is it possible to do this on ADP ? Thanks
It has little use caching the data provider after instantiating, since it's not actually doing any selecting on the database until it has been prepared. So you would actually be caching an empty object instance like it is now.
If you have a very large set of records, call the dataProviders' prepare() in advance in the cache:
self::getDb()->cache(function ($db) use ($dataProvider) {
$dataProvider->prepare();
}, 3600, $dependency);
return $dataProvider;
This will actually cache whatever queries the dataProvider runs ,so the next time they will be fetched from the query cache. This should result in what you are looking for.
If you have a finite amount of records, caching them all at once could also work:
$key = 'MyCachedData'; // + Data uniquely referring to your search parameters
$cache = \Yii::$app->cache;
$dataProvider = $cache->get($key);
if (!$dataProvider) {
$dependency = \Yii::createObject([
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT MAX(updated_at) FROM post',
]);
$dataProvider = new \yii\data\ArrayDataProvider;
$dataProvider->allModels = $query->all();
$cache->set($key, $dataProvider, 3600, $dependency)
}
return $dataProvider;
Obviously this is less than ideal for larger datasets, but it depends on what you are looking for.

Categories