Symfony Doctrine take the most recent date in where close - php

I would like to make a query which call the most recent date in the same table in a where close.
$query = $this->createQueryBuilder('a')
->select('a.amount')
->where('a.product = :productId')
->andWhere('a.id = :id'),
$recentDate = null;
if($date === null){
$recentDate = $this->createQueryBuilder('a')
->select($query->expr()->max('a.date'))->getQuery()->getResult();
$query->andWhere('a.date = :recentDate');
}else{
$query->andWhere('a.date = :date');
}
$query->setParameters(array(
'productId' => $productId,
'id' => $id,
'date' => $date,
'recentDate' => $recentDate,
));
return $query;
but I have this issue:
Invalid parameter number: number of bound variables does not match number of tokens

Just name your "date" parameter and "recentDate" parameter with the same name, they couldn't be in your request together and pass only this unique parameter in your setParameters()
$query = $this->createQueryBuilder('a')
->select('a.amount')
->where('a.product = :productId')
->andWhere('a.id = :id')
->andWhere('a.date = :date');
if($date === null){
$date = $this->createQueryBuilder('a')
->select($query->expr()->max('a.date'))->getQuery()->getResult();
}
$query->setParameters(array(
'productId' => $productId,
'id' => $id,
'date' => $date
));
return $query;

Try this:
$query->setParameters(array(
'productId' => $productId,
'id' => $id
));
if($date === null) {
$query->setParameter(':recentDate', $recentDate);
} else {
$query->setParameter(':date', $date);
}
The recentDate parameter isn't a part of the query if $date != null and vice versa so isn't the date parameter.
Maybe there's a more elegant way to build this query?
kind regards
Joe

You're providing 4 parameters to a query that only has 3.
$query = $this->createQueryBuilder('a')
->select('a.amount')
->where('a.product = :productId')
->andWhere('a.id = :id'),
$recentDate = null;
if($date === null){
$recentDate = $this->createQueryBuilder('a')
->select($query->expr()->max('a.date'))->getQuery()->getResult();
$query->andWhere('a.date = :recentDate');
}else{
$query->andWhere('a.date = :date');
}
$query->setParameters(array(
'productId' => $productId,
'id' => $id,
));
if($date === null){
$query->setParameter('recentDate', $recentDate);
}else{
$query->setParameter('date', $date);
}

Related

Codeigniter multiple where condition

I have a problem i only want to select a specific row/s with user_id = $this->session->id , status = 4 hours, 8 hours and work from home.
EDIT:
all my status is
Work From Home
8 hours
4 hours
Vacation Leave
Sick Leave
but what happened is it only returns all the Work From Home of the specific user_id and not included the 4 hours and 8 hours status
Controller
$order_by = "id desc";
$where = ['user_id' => $this->session->id,'status' => '4 hours', 'status' => '8 hours','status' => 'Work From Home'];
$this->Crud_model->fetch('record',$where,"","",$order_by);
MODEL
public function fetch($table,$where="",$limit="",$offset="",$order=""){
if (!empty($where)) {
$this->db->where($where);
}
if (!empty($limit)) {
if (!empty($offset)) {
$this->db->limit($limit, $offset);
}else{
$this->db->limit($limit);
}
}
if (!empty($order)) {
$this->db->order_by($order);
}
$query = $this->db->get($table);
if ($query->num_rows() > 0) {
return $query->result();
}else{
return FALSE;
}
}
Try the following:
Controller:
$order_by = "id desc";
$where = ['user_id' => $this->session->id];
$where_in = ['name' => 'status', 'values' => ['4 hours', '8 hours', 'Work From Home']];
$this->Crud_model->fetch('record',$where,"","",$order_by, $where_in);
Model:
public function fetch($table,$where="",$limit="",$offset="",$order="", $where_in = false){
if (!empty($where)) {
$this->db->where($where);
}
if (!empty($limit)) {
if (!empty($offset)) {
$this->db->limit($limit, $offset);
}else{
$this->db->limit($limit);
}
}
if (!empty($order)) {
$this->db->order_by($order);
}
if($where_in)
{
$this->db->where_in($where_in['name'], $where_in['values']);
}
$query = $this->db->get($table);
if ($query->num_rows() > 0) {
return $query->result();
}else{
return FALSE;
}
}
This uses where_in functionality to get all records for those status values.
Associate Array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
Or if you want to do something other than = comparison
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);

what is hapenning with my foreach at the moment to insert data to db?

I want to insert my data in to my database when I try to added as my product_id is an array then show me an error like this , i need to add it one by one, but I want to add them altogether if I Have 2 , 3 , 4 , 10+ etc etc , how can I iterate on my foreach in my model to add them?
Operand should contain 1 column(s)
INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES('low stock', 'low stock', ('4170','4171'), 1, 1500579824)
My Controller:
<?php
public function tests() {
$array = $this->sale->get_all_cart($this->session->carrito);
foreach ($array as $key => $value) {
$product_id[] = $value['id'];
}
$entryData = array(
'message' => 'low stock',
'product_id' => $product_id,
'user_id' => 1,
'type' => 'low stock',
'timestamp' => time()
);
$this->notification->addNotification($entryData);
}
?>
My Model
<?php
function addNotification(Array $data) {
$types = array(
'new' => 0,
'pending' => 1,
'low stock' => 2
);
if (isset($types[$data['type']]) === false) {
throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}
$type = $types[$data['type']];
$query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN ? AND type = ? ";
$previousNotification = $this->db->query($query, array(
$data['product_id'],
$data['type']
))->result_array();
if ($previousNotification[0]['notificationCount'] >= 0) {
$sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
try {
/*foreach ($data as $value) {
print_r($value['product_id']);
}*/
if (!$this->db->query($sql, array(
$data['message'],
$data['type'],
$data['product_id'],
$data['user_id'],
$data['timestamp']
))) {
return false;
}
return true;
}
catch (Exception $e) {
}
} else {
return true;
}
}
?>

PayPal does not show order summary-Omnipay

I have a function for Paypal redirection for that I pass some values in params..
It only pass the Description field doesn't shows the amount to be paid,and rest of my order summaries..
Here is my function
public function postPayment()
{
$hot_id = \Session::get('hot_id');
$user_id = \Session::get('uid');
$check_in = \Session::get('check_in');
$check_out = \Session::get('check_out');
$mem_count = \Session::get('mem_count');
$rm_no = \Session::get('rm_no');
$check_in = strtotime($check_in);
$check_in = date('Y-m-d',$check_in);
$check_out = strtotime($check_out);
$check_out = date('Y-m-d',$check_out);
$datediff = strtotime($check_out) - strtotime($check_in);
$diff = floor($datediff/(86400));
$room_prize = \DB::select("SELECT `room_prize` FROM `abserve_hotel_rooms` WHERE `room_id` = ".$_POST['room_id']);
$arr = array();
foreach ($room_prize as $key => $value) {
$arr[]= (get_object_vars($value));
}
$room_prize = array();
foreach ($arr as $key => $value) {
$room_prize[]=($value['room_prize']);
}
if($rm_no == 1)
{
$room_prize = (int) $room_prize[0] * (int) $diff;
}
else
{
$room_prize = (int) $room_prize[0] * (int) $diff * (int) $rm_no;
}
$room_prize = number_format((float)$room_prize, 2, '.', '');
$room_id = $_POST['room_id'];
$today = new DateTime();
$created_at = $today->format('Y-m-d');
$values = array('user_id' => $user_id,'hotel_id' => $hot_id,'room_id'=>$room_id,'room_prize'=>$room_prize,'check_in'=>$check_in,'check_out'=>$check_out,'created_at'=>$created_at);
\DB::table('abserve_orders')->insert($values);
$order_id = DB::select("SELECT `id` FROM `abserve_orders` WHERE `room_id` = ".$_POST['room_id']." AND `user_id` = ".$user_id);
$arr = array();
foreach ($order_id as $key => $value) {
$arr[]= (get_object_vars($value));
}
$order_id = array();
foreach ($arr as $key => $value) {
$order_id[]=($value['id']);
}
$order_id = $order_id[0];
$params = array(
'cancelUrl' => url().'/cancel_order',
'returnUrl' => url().'/payment_success',
'name' => 'new order',
'description' => 'description1',
'amount' => $room_prize,
'currency' => 'USD',
'hot_id' => $hot_id,
'room_id' => $room_id,
'user_id' => $user_id,
'check_in' => $check_in,
'check_out' => $check_out,
'order_id' => $order_id,
'nights' => $diff,
);
// print_r($params);exit;
\Session::put('params', $params);
\Session::save();
$gateway = Omnipay::create('PayPal_Express');
$gateway->setUsername('googley555_api1.yahoo.com');
$gateway->setPassword('3EQ6S6PB68A52JKZ');
$gateway->setSignature('AFcWxV21C7fd0v3bYYYRCpSSRl31AHaEiWOLAf5jQQ3-A9hLlhypSz9h');
$gateway->setTestMode(true);
$response = $gateway->purchase($params)->send();
if ($response->isSuccessful()) {
// payment was successful: update database
print_r($response);
} elseif ($response->isRedirect()) {
// redirect to offsite payment gateway
$response->redirect();
} else {
// payment failed: display message to customer
echo $response->getMessage();
}
}
I don't know what are missing in it..
Someone please help me..
You can't pass in arbitrary parameters such as hot_id, room_id, etc to PayPal (or omnipay) because it won't know what to do with them. PayPal is only intended to take certain parameters and the ones that PayPal does not take are ignored by Omnipay.
You may have to provide all of these extra parameters as part of the description parameter, which PayPal does understand.
Ideally these should be displayed by your application in some kind of useful format to the user before redirecting the user to the PayPal checkout page.

select_max not working properly.. it always results to 1

This is in my model.. it says Object of class CI_DB_mysql_result could not be converted to int.. the problem is in the select_max('id');
and it always result to 1. Please help i'm new to codeigniter
public function addEstablishment()
{
$capacity = $this->input->post('capacity');
$name = $this->input->post('name');
$curfew = $this->input->post('curfew');
$price = $this->input->post('price');
$gender = $this->input->post('gender');
$type = $this->input->post('type');
$this->db->select_max('id');
$result = $this->db->get('establishment');
$query = $result + 1;
$owner = $this->session->userdata('id');
$data = array(
'id' => $query,
'name' => $name ,
'capacity' => $capacity ,
'curfew' => $curfew ,
'gender' => $gender ,
'type' => $type ,
'owner' => $owner
);
if($this->db->insert('establishment', $data))
{
echo "Successfully added";
}
}
You have not get max ID in result, please try this way
change
$this->db->select_max('id');
$result = $this->db->get('establishment');
to
$this->db->select_max('id');
$result = $this->db->get('establishment')->row()->id;

Update Query in codeigniter model is not working

In the function update_status the 2nd update query is not working. The values in array are properly getting and correct. Can somebody please help me with that?
Here my code:
function update_status($appno,$value,$user_note,$date)
{
$status = array(
'app_status' => $value, );
$this->db->where('app_no',$appno);
$this->db->update('tbl_application', $status);
$today = date('Y-m-d');
$emp = $this->session->userdata('username');
$this->db->select('*');
$this->db->from('tbl_employee');
$this->db->where('Name', $emp);
$query = $this->db->get();
foreach($query->result() as $row)
{ $emp_code = $row->EmployeeCd; }
$data = array(
'app_no' => $appno,
'office_code' => 'KL08',
'verifi_status' => $value,
'view_status' => 1,
'view_by' => $emp_code,
'first_view' => $date,
'last_view' => $today,
'modification' => $user_note,
'modifi_status' => '1',
'notes' => 'NA'
);
$this->db->update('tbl_appverification', $data);
$this->db->where('app_no',$appno);
}
Where clause must be written before udpate.
$this->db->where('app_no',$appno);
$this->db->update('tbl_appverification', $data);
Please write the Where clause before the Update query, as you have written in your first update.

Categories