how to put more than 2 conditions in Query binding parameters - php

Hi Im working on parameter binding query Phalcon. Following is my code
$conditions = "client = :client: AND inv_date = :inv_date: AND date_sent = :date_sent: AND date_received = :date_received:";
$parameters = array(
"client" => $search_client,
"inv_date" => $invoice_date,
"date_sent" => $date_sent,
"date_received" => $date_received
);
$invoices = Invoices::find(
array(
$conditions,
"bind" => $parameters
)
);
Do I have use the AND correctly??
FYI its not working, if I try the following, it works:
$conditions = "client = :client: AND inv_date = :inv_date:";
$parameters = array(
"client" => $search_client,
);
$invoices = Invoices::find(
array(
$conditions,
"bind" => $parameters
)
);
All the fields exist in table, I think there is other way to use multiple AND

$conditions = "client = :client: AND inv_date = :inv_date:";
$parameters = array(
"client" => $search_client,
);
$invoices = Invoices::find(
array(
$conditions,
"bind" => $parameters
)
);
This code doesn't work because you pass just one parameter client, but have to pass 2 parameters: client and inv_date.
$parameters = array(
"client" => $search_client,
"inv_date" => $invoice_date,
);
Hence - you can use this option (because it really works) or you can use andWhere like #Niki Mihaylov advised.

Related

Laravel Eloquent get Clients which have active jobs

I'm working on a table which should show clients which have active jobs. I've got a page with all clients and this is working. I'm trying to get a query which is conditional on the client having active jobs. So here is what I have in my dataSource function:
public function dataSourcejobs(Request $request) {
$search = $request->query('search', array('value' => '', 'regex' => false));
$draw = $request->query('draw', 0);
$start = $request->query('start', 0);
$length = $request->query('length', 25);
$order = $request->query('order', array(0, 'desc'));
$filter = $search['value'];
$sortColumns = array(
0 => 'id',
1 => 'client',
2 => 'active_jobs',
3 => 'is_enabled',
4 => 'actions'
);
$query = Client::select( 'clients.*' );
if (!empty($filter)) {
$query->where( 'title', 'like', '%'.$filter.'%' );
}
$recordsTotal = $query->count();
$sortColumnName = $sortColumns[$order[0]['column']];
$query->orderBy($sortColumnName, $order[0]['dir'])
->take($length)
->skip($start);
$json = array(
'draw' => $draw,
'recordsTotal' => $recordsTotal,
'recordsFiltered' => $recordsTotal,
'data' => [],
);
$clients = $query->get();
foreach ($clients as $client) {
// Get active jobs for this client.
$jobs = Job::where( 'client_id', $client->id )->where('is_active', 1)->get();
$json['data'][] = [
$client->id,
$client->title,
'<button class="jobs">' . count($jobs) . ' Jobs</button>',
( $client->is_enabled === 1 ) ? 'Yes' : 'No',
'' . config( 'ecl.EDIT' ) . ' ' . config( 'ecl.WORK' ) . ''
];
}
return $json;
}
I've tried to do things like $query = Client::select( 'clients.*' )->count($client->jobs); but this is obviously wrong and errors. I also tried to do this in the loop (by checking the count) but this obvoiusly broke how the pagination works (but did show only clients with active jobs)
I should point out that the function above shown all clients even the ones which have no associated active jobs.
thanks
to get the count along with each client you can use withCount refer to this link https://laravel.com/docs/9.x/eloquent-relationships#counting-related-models
like the following
$query = Client::withCount('jobs');
as for getting the clients who only have jobs you can use whereHas

How to fix "Call to a member function execute() on string" on my function

I'm modifying a Wordpress API witch is currently getting learnpress course and lessons infos from the database using leanrpress functions only. I need to modify the API to get the scores from the newly implemented plugin h5p, therefor i need to make a new function to access the Database and get the scores from the right table, then put then into an array with the according lesson. The array here is only an exemple i'm using to see if i can get the datas, ut i'm stuck with the error
"Call to a member function execute() on string"
when i try and run it on postman. Could someone lighten me on this issue please ?
function ilp_api_get_progress_by_mail($data){
$mail=$_GET['mail']; //$data->get_param["mail"];
$course_id=$_GET['course'];
global $wpdb;
$user=get_user_by("email",$mail);
if($user !== false){
$lp_user=learn_press_get_user( $user->ID );
if($course_id==NULL){
$all_courses=ilp_api_get_all_courses($data);
}else{
$all_courses=array('courses' => array(array('id' => intval($course_id))));
}
$progress=array();
$i=0;
if($all_courses!=NULL && $all_courses['courses']!=null){
foreach($all_courses['courses'] as $course){
if($lp_user->has_enrolled_course($course['id'])){
$lp_course=learn_press_get_course( $course['id'] );
$course_data = $lp_user->get_course_data($course['id']);
$course_results = $course_data->get_results( false );
$progress[$i]=array(
'id' => $course['id'],
'name' => $lp_course->get_title(), //$course['name'],
'condition' => $lp_course->get_passing_condition(),
'completed' => $course_results['completed_items'],
'total' => $course_results['count_items'],
'progress' => absint( $course_results['completed_items'] / $course_results['count_items'] * 100 ),
'permalink' => $lp_course->get_permalink(),
);
$i++;
}
}
}
$result = array(
'userfound' => true,
'user_id' => $user->ID,
'connect' => get_h5p_grades(),
'courses_progress' => $progress,
'course_id' => $all_courses,
);
function get_h5p_grades(){
global $wpdb;
$ID = 1;
$ID_use = 1;
$result = $wpdb->prepare('SELECT score FROM mci_h5p_results WHERE id = %d AND user_id = %d', $ID, $ID_use);
$result->execute();
$donnees = $result->fetch();
return $donnees;
}

Transmission php class (How to get info of torrent)

I'm using the php transmission rpc class (https://github.com/brycied00d/PHP-Transmission-Class) to retrieve torrent info. But I don't know how to retrieve data from an array object (level 5).
I retrieve data like
$torrent_name = $result['arguments']['torrents'][0]['name'];
The Info I want is like :
$torrent_client_name = $result['arguments']['torrents'][0]['peers']['clientName'];
and here is the function,
public function get ( $ids = array(), $fields = array() )
{
if ( !is_array( $ids ) ) $ids = array( $ids );
if ( count( $fields ) == 0 ) $fields = array( "id", "name"); //this array
$request = array(
"fields" => $fields,
"ids" => $ids
);
return $this->request( "torrent-get", $request );
}
I tried to make something like this but no result:
$fields = array( "id", "name", array("peers"=>"clientName"));
or
$fields = array( "id", "name", array("clientName"));
or
$fields = array( "id", "name", 'peers' => array('clientName'));
The error I get :
Notice: Undefined index: peers
and here the var_dump($result);

sorting mongo regex query in php

I have a mongo collection and I'd like to obtain all the document whose names start with a given letter on PHP. My code:
$letter = "c";
$client = new MongoDB\Client();
$pme = $client->selectCollection("belgium", "pme");
$regex = new MongoDB\BSON\Regex ("^$letter", "i");
$query = array('name' => $regex); // 1
$query = array('name' => $regex, array( 'sort' => array( 'OrderBy' => 1 ) )); // 2
$query = new MongoDB\Driver\Query( array('name' => $regex), array( 'sort' => array( 'OrderBy' => 1 ) ) ); // 3
$cursor = $pme->find($query);
Whe I use query 1. I got all documents starting with letter c but not ordered. When I use query 2, I got nothing. And finally when I use query 3 I get almost every document, not just those starting with with 'c'. What I am doing wrong here?
In mongo method sort should be applied on cursor obtained by find:
$letter = "c";
$client = new MongoDB\Client();
$pme = $client->selectCollection("belgium", "pme");
$regex = new MongoDB\BSON\Regex ("^$letter", "i");
$query = array('name' => $regex);
// sort by field `name` happens here
$options = array("sort" => array("name" => 1), );
$cursor = $pme->find($query, $options);

Solr FieldCollapsing for More Like This queries

I want to use a "More Like This" query to find similar documents and collapse those that have the same value for the field 'image'. I tried to use the Field Collapsing parameters however they do not seem to work for "More like this".
Below is a snippet of my code. Can you tell me how to collapse results using the "More Like This" query?
$url = "http://{$host}:{$port}/solr/{$core}/mlt";
$data = [
'stream.body' => $content,
'fl' => 'image,content,title,signature',
'start' => 0,
'order' => "score desc",
'wt' => 'json',
'mlt.fl' => 'content,title',
// these lines do nothing ---v
'group' => 'true',
'group.field' => 'image',
'group.sort' => 'impressions desc',
'group.main' => 'true'
];
$curlHandle = curl_init($url);
$options = array (
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($curlHandle , $options);
$result = json_decode(curl_exec($curlHandle));
General answer
I could not collapse results using Field Collapsing paramaters. However, I was able to achieve the desired result using CollapsingQParserPlugin.
The following filter query collapses documents on the field 'image' and selects the one with the highest value for the field 'impressions': {!collapse field=image max=impressions}
Implementation
For some reason I was not able to combine this filter query with my other filter queries under a single key as follows:
$filterQueries = [
"-signature:{$signature}",
...
"{!collapse field=image max=impressions}"
];
$data = [
...
'fq' => implode(' AND ', $filterQueries),
...
];
This produced the error: Query does not implement createWeight
My fix was to do a GET request (instead of a POST, which was done in the question above). With the GET request it is possible to have a key for each filter query: http://solr-url/mtl?...&fq=-signature%3A0&...&fq=%7B!collapse+field%3Dimage+max%3Dimpressions%7D
Below is the php solution for the snippet in the question:
$url = "http://{$host}:{$port}/solr/{$core}/mlt?"; // Note the added question mark
$data = [
'stream.body' => $content,
'fl' => 'image,content,title,signature',
'fq' => $filterQueries,
'start' => 0,
'order' => "score desc",
'wt' => 'json',
'mlt.fl' => 'content,title'
];
$params = [];
foreach ($data as $key=>$value) {
if (is_array($value)) {
foreach ($value as $subvalue) {
$subvalue = urlencode($subvalue);
$params[] = "{$key}={$subvalue}";
}
} else {
$value = urlencode($value);
$params[] = "{$key}={$value}";
}
}
$url .= implode('&', $params);
$curlHandle = curl_init($url);
$options = array ();
curl_setopt_array($curlHandle , $options);
$result = json_decode(curl_exec($curlHandle));

Categories