References from here is there another way to Shorten where() and orWhere() ?
Example the code like :
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(['Status' => 'Pasif'])
->orWhere(['Status' => 'Rolling'])
->asArray()->all(), 'Id', 'Shift'
);
[UPDATE] SOLVED
Comparing same column for two values or more for the best solution in my opinion is to use IN Condition where we can clearly see the detailed code or you can also use OR or directly build it like ->where(['Status' => ['Pasif', 'Rolling']])
Code with IN :
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'IN',
'Status',['Pasif', 'Rolling']
]
)
->asArray()->all(), 'Id', 'Shift'
);
Since you are comparing same column for two values. I guess IN Condition will also wok.
HrEmployeeShift::find()
->where(['Status' => ['Pasif', 'Rolling']])
->asArray()
->all();
OR
HrEmployeeShift::find()
->where(['IN', 'Status', ['Pasif', 'Rolling']])
->asArray()
->all();
This will result in a condition Status IN ('Pasif', 'Rolling').
Posted from my mobile you are looking to do it the following way
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'OR',
['Status' => 'Pasif'],
['Status' => 'Rolling'],
]
)->asArray()->all(),
'Id',
'Shift'
);
Related
This is my data table image
my blade file
#table([
'id' => 'Persons.index',
'xhr' => route('api.manage-dup-api'),
'ns' => 'PersonsIndex',
'columns' => $columns ?? [],
'filters' => $filterTable ?? [],
'params' => [
'filters_live' => false,
'selectable' => true,
'toolbar_style' => 'fixed'
]
])
this is a query which passes data to a data table [API]
$q->with('user')
->with('user.roles')
->select(
'persons.*',
'birth->date as birthdate'
)->`enter code here`whereIn('id', $id)->orWhereIn('old_id_CONINET', $coninet_ids);
return $this->outputList($q, $request);
as shown in the picture I want to remove ["] from the CONINET_ID table
you are storing an array of strings in the DB.
you can convert the array values to int:
array_map('intval', $array);
you can also create an accessor on your eloquent model
public function getOldIdConinetAttribute($value)
{
return array_map('intval', $value);
}
It would better if you give some detailed info. As of now details mentioned above can not explain your code. As of my understanding, I suggest you to check Yajra datatable plugin which will help you solving your issue.
or you can cast coninet_id to array by adding below code in your model.
protected $casts = [
'coninet_id' => 'array'
];
I'm learning Yii2 framework. There's a geometry type column in my MySQL table. I was wondering if I could create it with a Yii2 migration table. Unfortunately, there is no such geometry() method in yii\db\SchemaBuilderTrait class so I assume the following won't work:
$this->createTable('{{%gps}}', [
...
'gps' => $this->geometry()->notNull()
...
]);
Does anyone know any workaround for this?
I haven't used for create a geometry but you can also use an hash format for create column
use yii\db\Schema;
use yii\db\Migration;
$this->createTable('Your_table ', [
'id' => 'pk',
'user_id' => 'integer not null',
'land_scope_code' => 'string(4)',
'init_lat' => 'decimal(24,20)',
'init_lng' => 'decimal(24,20)',
'init_zoom' => 'integer',
]);
could be this is useful for your
$this->createTable('{{%gps}}', [
...
'gps' => 'geometry not null';
...
]);
I have a question regarding the Paginator Component of CakePHP3 (3.0.13). I'm using the MYSQL function FIELD() to order my data, like that:
$this->paginate = array_merge_recursive([
'conditions' => [
'zip IN' => $zips
],
'order' => [
'FIELD(zip, '.rtrim(implode(',', $zips), ',').')',
'ispro' => 'desc',
],
$this->paginate
]);
When I apply this to my $this->paginate() it wouldn't be recognized as long as the query param sort is set. To avoid this I remove the sort in my request with:
if (isset($this->request->query['sort'])) {
unset($this->request->query['sort']);
}
This is working, but I was wondering if there is a better solution, maybe in the paginator component itself, which I haven't found yet.
you can sort your query before you paginate it. Your sort conditions will come before the one appended from the paginator component
so you can do
$dentists = $this->Dentists->find()
->order([
'FIELD(zip, '.rtrim(implode(',', $zips), ',').')',
'ispro' => 'desc'
]);
$this->paginate = array_merge_recursive([
'conditions' => [
'zip IN' => $zips
],
$this->paginate
]);
$dentists = $this->paginate($dentists);
I want to use a yii2 query in which I want to check a not equal to condition.
I tried like this but it didn't give the desired results. How do I do it?
$details = MovieShows::find()
->where(['movie_id'=>$id])
->andWhere(['location_id'=>$loc_id])
->andWhere(['cancel_date'=>!$date])
->all();
In this case you need to use operator format: [operator, operand1, operand2, ...]. So your code should look like this:
$details = MovieShows::find()
->where(['movie_id'=>$id])
->andWhere(['location_id'=>$loc_id])
->andWhere(['<>','cancel_date', $date])
->all();
More about using where method and operator format
You can also try this:
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['!=', 'cancel_date', $date])->all();
for Greater than
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['>', 'cancel_date', $date])->all();
for less than
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['<', 'cancel_date', $date])->all();
More check here
Maybe this one help...:)
$query->andFilterWhere([ 'or',
['<=', 'affidavit_cont', 0],
['=', 'affidavit_cont1', 0],
['>', 'affidavit_cont2', 0],
['!=', 'affidavit_cont3', $this->affidavit3],
['in', 'attempt_count', $this->attempted],
]);
$query->andFilterWhere(['like', 'job_invoice.paid_status', '',]);
$query->andFilterWhere(['in', 'status_id', $this->status_id]);
$query->andFilterWhere(['between', 'last_attempt',
strtotime(Utility::convertDate2Db($this->from_date)),
strtotime(Utility::convertDate2Db($this->to_date))
]);
The better and safe way to apply a condition.
Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all();
You can use this
$this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all();
In addiction to the #tony answer, for those who need to encapsulate a subquery here there's a quick example:
$users = User::find()
->where([
'not in',
'id',
(new Query())
->select('user_id')
->from(MyTable::tableName())
->where(['related_id'=>$myRelatedId])
->all();
<?= $form->field($model, 'first_name')->dropDownList(
ArrayHelper::map(user::find()->where([
'not in ' ,
'first_name',
patient::find()
->select([ 'patient_name' ])
->asArray()
])
->all(),'id','first_name')
Maybe it help you for whom need to use subquery .
In case anyone reading this needs to use a REGEXP or similar, this works in Yii2 (2.0.15.1):
->andWhere(['NOT REGEXP', 'column','[A-Za-z]'])
You can just use the below pasted code:
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['location_id'=>$loc_id])
->andWhere(['not in','cancel_date',$date])->all();
I'm trying to select few rows from the table and render it in multiple pages (pagination).
This is a code in the model:
return new CActiveDataProvider('Downloads',
array(
'criteria' => array(
'select' => 'download_id,title,thumb_ext',
'order' => 'download_id DESC',
'limit' => $count,
),
'pagination' => array('pageSize' => 5,),
)
);
In the view I display it using CGridView:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns' => array('download_id', 'title', 'thumb_ext'),
));
The problem is that CActiveDataProvider ignores limit of criteria and returns all rows of table...
Thanks.
I'm not positive... but I think Yii uses the LIMIT clause to do SQL result pagination, so it will overwrite/replace your LIMIT clause. You can check this by turning on the CWebLogRoute log route to see exactly what SQL is being executed.
I'm not quite sure how this is supposed to work, anyway. What is the LIMIT clause you added for? If you are paginating anyway, why not let the user paginate through all the records? The solution is probably to change your criteria to get rid of the LIMIT clause.
Are you trying to set the number of results per page? You already have the pageSize set to 5 though...
One other thing to try that might do what you want, is to check out the base Pager class, CPagination. With CLinkPager, it might let you paginate more creatively than the CListPager you are using with the CGridView.
Good luck!
I had the same problem and I found a solution as follows:
return new CActiveDataProvider('Downloads',
array(
'criteria' => array(
'select' => 'download_id,title,thumb_ext',
'order' => 'download_id DESC',
),
'pagination' => array('pageSize' => 5,),
'totalItemCount' => $count,
)
);
and set CGridView to pagination is hidden:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'enablePagination' => false,
'columns' => array('download_id', 'title', 'thumb_ext'),
));
One of the basic features of a data provider is that you can override the calculation of the amount of rows by specifying a "totalItemCount" in the config. Normally (I haven't tested it) this also works for the ActiveDataProvider:
return new CActiveDataProvider('Downloads',
array(
'criteria' => array(
'select' => 'download_id,title,thumb_ext',
'order' => 'download_id DESC',
),
'pagination' => array('pageSize' => 5,),
'totalItemCount' => $count,
)
);