Tổng hợp những condition thường sử dụng trong magento 2
Tổng hợp những condition thường sử dụng trong magento 2
| Condition | Notes |
|---|---|
eq |
Equals. |
finset |
A value within a set of values |
from |
The beginning of a range. Must be used with to |
gt |
Greater than |
gteq |
Greater than or equal |
in |
In. The value can contain a comma-separated list of values. |
like |
Like. The value can contain the SQL wildcard characters when like is specified. |
lt |
Less than |
lteq |
Less than or equal |
moreq |
More or equal |
neq |
Not equal |
nfinset |
A value that is not within a set of values |
nin |
Not in. The value can contain a comma-separated list of values. |
notnull |
Not null |
null |
Null |
to |
The end of a range. Must be used with from |
Cách 1 sử dụng condition trong backend, truy vấn vào db:
Ví dụ chúng ta sử dụng condition lấy ra các sản phẩm có id là: 1,2,3
1, Chúng ta inject thằng Product Collection vào để test:
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collection
2, Chúng ta sử dụng Product Collection và query với condition:
$products = $this->collection->create()->addFieldToFilter('entity_id', ['in' => [1,2,3]]);
Cách 2 sử dụng condition để truy vấn Api:
Demo chúng ta sẽ lấy một sản phẩm có SKU bắt đầu là: WSH và giá của sản phẩm đó < 30$
GET/rest/ /V1/products? searchCriteria[filter_groups][0][filters][0][field]=sku& searchCriteria[filter_groups][0][filters][0][value]=WSH%2531%25& searchCriteria[filter_groups][0][filters][0][condition_type]=like& searchCriteria[filter_groups][1][filters][0][field]=price& searchCriteria[filter_groups][1][filters][0][value]=30& searchCriteria[filter_groups][1][filters][0][condition_type]=lt
Hệ thống tạo ra cái mảng như thế này, nhìn cho dễ hình dung :
searchCriteria => [
'filterGroups' => [
0 => [
'filters' => [
0 => [
'field' => 'sku',
'value' => 'WSH%31%',
'condition_type' => 'like'
]
1 => [
'filters' => [
0 => [
'field' => 'price',
'value' => '30',
'condition_type' => 'lt'
]
]
]
]

Post a Comment