cakephp query find_in_set on single column with multiple searched keywords not working - mysql

I want to find all the records from column who have any keyword match with column data:
It works fine if searching single search keyword in column having comma separated values like below code: Cakephp 3.4 version
$posts = TableRegistry::get('Posts');
$search_keywords = array_filter(explode(' ', $search_string));
$option = [
'contain' => false,
'conditions' => [
"find_in_set('New', Posts.title)",
],
'order' => ['Posts.created DESC']
];
$allpost = $posts->find('all',$option)->toArray();
Note: i want all the words from string should be search with column title, its not mandatory to have comma separated records:
$search_keyword = "New car in new delhi";
so i want code to be like below :
$search_keyword = "New car in new delhi";
$search_keywords = array_filter(explode(' ', $search_string));
$option = [
'contain' => false,
'conditions' => [
"find_in_set({$search_keywords}, `Posts`.title)",
],
'order' => ['Posts.created DESC']
];
$allpost = $posts->find('all',$option)->toArray();
Great Thanks in advance!!!

I found the solution:
First make column fulltext index then add below code:
$search_string = "New car in new delhi";
$option = [
'contain' => false,
'conditions' => [
"MATCH (title) AGAINST ('$search_string')"
],
'order' => ['Posts.created DESC']
];
$allpost = $posts->find('all',$option)->toArray();

Related

SQL JOIN select ids by string array

I have some tables.
picture
people_ids type is TEXT. No foreign keys used.
main_id - is like main parent, one from PEOPLE table;
picture
I need SQL request to get result.
SELECT * from families ...
[
'main' => [
'name' => 'John',
'surname' => 'Brown'
],
'people' => [
[
'name' => 'John',
'surname' => 'Brown'
],
[
'name' => 'Merry',
'surname' => 'Brown'
],
[
'name' => 'Lizy',
'surname' => 'Brown'
]
]
]
Thanks for all, I'm dounded answer by the time)
Now I'm understanding that... that was maximally stupid idea. Learn more about SQL, table joining process and more.

Can't Insert an array in table column, Laravel

I need to insert an array in a table column with other non array inputs, But each time it's inserting a string "Array" ,
My codes are :
$post_data['product_category'] = "Goods";
$post_data['product_profile'] = "physical-goods";
for ($i = 1; $i < count($request->package_type_id); $i++) {
$answers[] = [
$post_data['package_type_id'] =$request->package_type_id,
];
}
$update_product = DB::table('orders')
->where('transaction_id', $post_data['tran_id'])
->updateOrInsert([
'name' => $post_data['cus_name'],
'email' => $post_data['cus_email'],
'phone' => $post_data['cus_phone'],
'amount' => $post_data['total_amount'],
'status' => 'Pending',
'address' => $post_data['cus_add1'],
'transaction_id' => $post_data['tran_id'],
'currency' => $post_data['currency'],
'package_type_id' => implode($answers,',')
]);
You have gone a long way, I think you just want to make a string from the $request->package_type_id which I assume is an array.
$post_data['product_category'] = "Goods";
$post_data['product_profile'] = "physical-goods";
$post_data['package_type_id'] = implode(',', $request->package_type_id);
$update_product = DB::table('orders')
->where('transaction_id', $post_data['tran_id'])
->updateOrInsert([
// ...
'package_type_id' => $post_data['package_type_id']
]);
Also note that the syntax for implode is implode(glue, array) not the other way around.

Yii2. How to build subexpression

How can i build query with subexpression, without using yii\db\Expression and raw sql. For example this:
SELECT * FROM user WHERE archived = 3 AND ((group = 2 AND status = 3) OR (group = 3 AND status = 2));
You can build such condition using array expressions:
$users = (new Query())
->from('user')
->where(['archived' => 3])
->andWhere([
'or',
[
'group' => 2,
'status' => 3,
],
[
'group' => 3,
'status' => 2,
],
])
->all();

CakePHP 2 - Two foreign keys of a table linked to the same single table primary key

How can i use cakephp 2X model hasone or other association concept here to execute the find query.
In my Schinfo.php model is
class Schinfo extends AppModel {
public $tablePrefix = 'sko_';
public $hasOne = [
'State' => [
'className' => 'Masterlocation',
'foreignKey' => 'master_locid'
],
'City' => [
'className' => 'Masterlocation',
'foreignKey' => 'master_locid'
],
'Area' => [
'className' => 'Masterlocation',
'foreignKey' => 'master_locid'
]
];
}
With the above I got
SELECT
`Schinfo`.`skool_id`,
`Schinfo`.`skool_code`,
`Schinfo`.`skool_name`,
`Schinfo`.`skool_addr`,
`Schinfo`.`master_state_id`,
`Schinfo`.`master_city_id`,
`Schinfo`.`master_area_id`,
`Schinfo`.`skool_pin`,
`Schinfo`.`skool_board`,
`Schinfo`.`skool_type_id`,
`Schinfo`.`skool_affilated_to`,
`Schinfo`.`skool_affilated_no`,
`Schinfo`.`skool_contact_no`,
`Schinfo`.`skool_mailid`,
`Schinfo`.`skool_website`,
`Schinfo`.`skool_logo`,
`Schinfo`.`skool_delete`,
`State`.`master_locid`,
`State`.`master_parentid`,
`State`.`master_locname`,
`State`.`is_checked`,
`City`.`master_locid`,
`City`.`master_parentid`,
`City`.`master_locname`,
`City`.`is_checked`,
`Area`.`master_locid`,
`Area`.`master_parentid`,
`Area`.`master_locname`,
`Area`.`is_checked`
FROM
`skoolata`.`sko_schinfos` AS `Schinfo`
LEFT JOIN
`skoolata`.`sko_masterlocations` AS `State`
ON (`State`.`master_locid` = `Schinfo`.`id`)
LEFT JOIN
`skoolata`.`sko_masterlocations` AS `City`
ON (`City`.`master_locid` = `Schinfo`.`id`)
LEFT JOIN
`skoolata`.`sko_masterlocations` AS `Area`
ON (`Area`.`master_locid` = `Schinfo`.`id`)
WHERE
1 = 1
Now I need to change
LEFT JOIN
skoolata.sko_masterlocations AS State
ON (State.master_locid = Schinfo.id)
LEFT JOIN
skoolata.sko_masterlocations AS City
ON (City.master_locid = Schinfo.id)
LEFT JOIN
skoolata.sko_masterlocations AS Area
ON (Area.master_locid = Schinfo.id)
to
LEFT JOIN
skoolata.sko_masterlocations AS State
ON (State.master_locid = Schinfo.master_state_id)
LEFT JOIN
skoolata.sko_masterlocations AS City
ON (City.master_locid = Schinfo.master_city_id)
LEFT JOIN
skoolata.sko_masterlocations AS Area
ON (Area.master_locid = Schinfo.master_area_id)
to get my desire output
You want to define three relationships, Country, State and City and specify the className for each to be the model you are linking to, for example Location. You can then also specify the column you will be using as the foreign keys in your Student model using foreignKey:-
public $hasOne = [
'Country' => [
'className' => 'Location',
'foreignKey' => 'country_id'
],
'State' => [
'className' => 'Location',
'foreignKey' => 'state_id'
],
'City' => [
'className' => 'Location',
'foreignKey' => 'city_id'
]
];
Then when it comes to finding the results you can use contain like:-
$students = $this->Student->find('all', [
'contain' => ['Country', 'State', 'City']
]);

elastic search: Advanced Filter Query

I am Working on Elastic Search for My current Project. I need a filter for users based on their industries. please look at my code once. and mySql query as follows
SELECT U.* FROM `users` `U`
JOIN `user_industries` `UI` ON `UI`.`user_id`=`U`.`id`
WHERE `UI`.`industry_id` IN('1','3','5');
$query = array("from" => $start,
"size" => $recordslimit,
"sort" => array(array('id' => 'desc')),
"query" => array(
"filtered" => array(
"query" => array("match_all" => array()),
"filter" => array(
"bool" => array(
'must' => array(array('term' => array('user_type' => 'v')),
array('term' => array('status' => 'a')),
array('term' => array('industries.id' => 1))
),
'must_not' => array(
array('term' => array('subscription_type' => 'n'))
)
))
)));
I passed one Industry Value. how can i pass multiple values of industries
Great start !! You can achieve what you want by using a terms filter instead of a term one and specifying the values 1, 3, 5 in an array():
...
array('terms' => array('industries.id' => array(1, 3, 5)))
...