How to combine SQL OR conditions in Product Collection ? Magento? - mysql

I want my results to come either from name match OR description match.
$custom_collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToSelect('type')
->addAttributeToSelect('price')
->addAttributeToFilter(array('attribute' => 'name', 'like' => '%' . $queryText . '%'))
->addAttributeToFilter(array('attribute' => 'description', 'like' => '%' . $queryText . '%'))
->addFieldToFilter('attribute_set_id', array(11, 4));
These are the two specific lines I need to merge
->addAttributeToFilter(array('attribute' => 'name', 'like' => '%' . $queryText . '%'))
->addAttributeToFilter(array('attribute' => 'description', 'like' => '%' . $queryText . '%'))

You can use below code for OR condition.
$custom_collection->addAttributeToFilter(
array(
array('attribute'=> 'name','like' => '%' . $queryText . '%'),
array('attribute'=> 'description','like' => '%' . $queryText . '%')
)
);

Try the following:
$collection->addAttributeToFilter(
array(
array('attribute'=> 'name','like' => ''%' . $queryText . '%''),
array('attribute'=> 'description','like' => ''%' . $queryText . '%''),
)
);
This will output a WHERE clause of the format:
WHERE ((name LIKE ''%' . $queryText . '%'') OR (description LIKE ''%' . $queryText . '%'')

Related

Laravel 8 whereJsonContains

with this code I implemented a search:
if ($request->keyword != "") {
$users = User::where('firstname', 'like', '%' . $request->keyword . '%')
->orWhere('lastname', 'like', '%' . $request->keyword . '%')
->orWhere('email', 'like', '%' . $request->keyword . '%')
->orderBy('lastname')
->whereJsonContains("out", Auth::user()->id)
->paginate($request->total);
} else {
$users = User::whereJsonContains("out", Auth::user()->id)->orderBy('lastname')->paginate($request->total);
}
Filtering for firstname, lastname and e-mail works fine, but the important where with "->whereJsonContains("out", Auth::user()->id)" not work. After typing "s" every User with "s" will be displayed in results.... what is wrong?
You ay try
if ($request->keyword != "") {
$users = User::query()
->where(function($query) use($request){
$query->where('firstname', 'like', '%' . $request->keyword . '%')
->orWhere('lastname', 'like', '%' . $request->keyword . '%')
->orWhere('email', 'like', '%' . $request->keyword . '%');
})
->whereJsonContains("out", Auth::user()->id)
->orderBy('lastname')
->paginate($request->total);
} else {
$users = User::whereJsonContains("out", Auth::user()->id)
->orderBy('lastname')
->paginate($request->total);
}
Query will return records where either the firstname, lastname or email contains anything like $request->keyword and the out column contains the currently logged in user's id.

laravel sql problem with query where and or, lost condition

im trying to write sql query in laravel:
select * from products where deleted=0 AND
(('name_ru','LIKE', '%' . $request->q_search . '%')
OR ('name_en','LIKE', '%' . $request->q_search . '%')
OR ('name_lv','LIKE', '%' . $request->q_search . '%'))
now i have:
$products = DB::table('product')
->select('id','name_lv', 'name_ru', 'name_en','price','desc')
->where('deleted',0)
->where('name_lv','LIKE', '%' . $request->q_search . '%')
->orWhere('name_ru','LIKE', '%' . $request->q_search . '%')
->orwhere('name_en','LIKE', '%' . $request->q_search . '%')
->distinct()
->orderBy('price', 'desc')
->paginate(10,['*'], 'found')
->onEachSide(6);
my problem is with condition deleted=0, my sql ignores it. Whats wrong? thanks for advice!
you can use where(function(){}) as you have where AND (.. OR ..)
$products = DB::table('product')
->select('id', 'name_lv', 'name_ru', 'name_en', 'price', 'desc')
->where(function ($q) use ($request) {
$q->where('name_lv', 'LIKE', '%' . $request->q_search . '%')
->orWhere('name_ru', 'LIKE', '%' . $request->q_search . '%')
->orwhere('name_en', 'LIKE', '%' . $request->q_search . '%');
})
->where('deleted', 0)
->distinct()
->orderBy('price', 'desc')
->paginate(10, ['*'], 'found')->onEachSide(6);

How use between cakephp

it does not return any error but neither any row but if there are fields but it does not collect them, which may be failing the query
$tarea = $this->Signs
->find('all', [
'conditions'=> array(
'date_inicio BETWEEN "' .
$horaInicio->format('Y-m-d H:i:s') .
'" and "' .
$horaFin->format('Y-m-d H:i:s') .
'"'
)
])
->where([
'user_id' => $horario->user_id,
'date_fin IS NOT' => null
]);
This works if I remove the condition between, but if I put it it doesn't give an error but it doesn't return anything.

Selecting to multiple tables in Laravel

Trying to select entries from user input $query. I would like to search the first_name, last_name, address (etc) columns that has NULL on deleted_at. What I have done so far is:
$data = DB::table('users')
->where('first_name', 'LIKE', '%' . $query . '%')
->orWhere('last_name', 'LIKE', '%' . $query . '%')
->orWhere('address', 'LIKE', '%' . $query. '%')
->whereNull('deleted_at')
->get();
My goal is to search the above mentioned columns by the user's query and show any match it finds. Problem is, it also shows entries that are NOT NULL in the column deleted_at. I wish to only see results which are NULL at deleted_at. What may I have been doing wrong?
make use of parameter grouping
$data = DB::table('users')
->whereNull('deleted_at')
->where(function ($query) {
$query->where('first_name', 'LIKE', '%' . $query . '%')
->orWhere('last_name', 'LIKE', '%' . $query . '%')
->orWhere('address', 'LIKE', '%' . $query. '%');
})
->get();

Use OR conditions on associated model in CakePHP 3

I have 3 models, the base model is Jobs and Customers, Contacts are the models associated with jobs. Here is the association.
$this->belongsTo('Customers', [
'className' => 'Customers',
'foreignKey' => 'customer_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Contacts', [
'className' => 'Contacts',
'foreignKey' => 'contact_id',
'joinType' => 'INNER'
]);
I want to search a text in all the 3 tables and return the job records which are having the search text at least any one of the tables... I want to achieve this using CakePHP's ORM...
This is the raw SQL you may want as the reference,
$searchText = 'Bikash';
$JobQ->query("SELECT *
FROM Jobs
LEFT JOIN Customer ON Jobs.CustomerID=Customers.CustomerID
LEFT JOIN Contacts ON Jobs.ContactID=Contacts.ContactID
WHERE (
Job.JobName LIKE '%" . $searchText . "%' or
Customer.Name LIKE '%" . $searchText . "%' or
Contact.FirstName LIKE '%" . $searchText . "%' or
Contact.Surname LIKE '%" . $searchText . "%');
If you are following cake conventions should be simply:
$jobs = $this->Jobs->find()
->contain(['Customers', 'Contacts'])
->where([
'OR' => [
'Jobs.JobName LIKE' => '%" . $searchText . "%',
'Customers.Name LIKE' => '%" . $searchText . "%',
'Contacts.FirstName LIKE' => '%" . $searchText . "%',
'Contacts.Surname LIKE' => '%" . $searchText . "%'
]
]);
or using query expressions
$jobs = $this->Jobs->find()
->contain(['Customers', 'Contacts'])
->where(function ($exp, $query) {
return $exp->or_([
$exp->like('Jobs.JobName', "%$searchText%"),
$exp->like('Customers.Name, "%$searchText%"),
$exp->like('Contacts.FirstName, "%$searchText%"),
$exp->like('Contacts.Surname', "%$searchText%")'
]);
});