CAKEphp Virtual field not working - php

Because i have two price columns in my table (subscription_discount and subscription_price). I need to find a way to find the lowest of the two and use it. The problem is that this only has to occur when subscription_discount is lower then subscription_price and when subscription_discount is not 0.00.
This is my code to create the virtual field in my model:
public $virtualFields = array(
'end_price' => "IF(Abonnement.subscription_discount IS NOT NULL OR Abonnement.subscription_discount < Abonnement.subscription_price, Abonnement.subscription_price), Abonnement.subscription_discount)"
);
Somehow this ends up in an error in my view:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '),
Abonnement.subscription_discount)) AS Abonnement__end_price FROM
`cake' at line 1
I don't understand what is going wrong here.

public $virtualFields = array(
'end_price' => "IF(Abonnement.subscription_discount IS NOT NULL OR Abonnement.subscription_discount < Abonnement.subscription_price, Abonnement.subscription_price>>>**)**<<<, Abonnement.subscription_discount)"
);
remove it

Related

PHP SQL get unique values from column

I want to create to modify sql query so it will choose all unique values (it's integer) from column traffic_type. There is a part of code that should be edited:
$statistic_query = array(
'select' => array(
'SUM(installs) AS installs_count',
'SUM(fake_installs) AS fake_installs_count',
'SUM(searchs) AS searchs_count',
'SUM(clicks) AS clicks_count',
'AVG(bid) AS bid_avg',
'SUM(user_profit) AS user_profit_sum',
'SUM(system_profit) AS system_profit_sum',
'SUM(visits) AS visits_count',
'SUM(downloads) AS downloads_count',
'SUM(DISTINCT traffic_type) AS traffic_type_count'
),
'from' => ' statistic'
);
It's working but I don't need to sum unique values I want to get all unique values separated with commas like 0, 2. When I'm changing 'SUM(DISTINCT traffic_type) AS traffic_type_count' to 'DISTINCT traffic_type AS traffic_type_count' - there is an error:
Error 500
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT traffic_type) AS traffic_type_count, `dt_add` FROM `statistic` WHERE (d' at line 1
It can be because it gets an array, not number. So is there a way to get all unique values separated with commas like one variable, not an array?
When you want to get comma separated list of values use GROUP_CONCAT
GROUP_CONCAT(traffic_type) AS traffic_type_count
Also you can use DISTINCT within for filter only unique values
GROUP_CONCAT(DISTINCT traffic_type) AS traffic_type_count
online SQL editor

How to fix column name is not in a groupBy laravel?

I am facing this error for the past 2-3 days. I hold this task to check this later in my free time. But still, I am getting the same error. Please help me to find the error. I am tired to check all solutions on google. But nothing works.
Error
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 'dating-app.messages.id' isn't in GROUP BY (SQL: select * from messages group by room_id having is_read = 0) in file C:\xampp\htdocs\dating-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 692
Here is my query
$messages = Message::groupBy('room_id')->having('is_read', 0)->get();
return response()->json(['status' => true, 'message' => $messages]);`
Any solution appreciated!
That's because any column you use in a select statement must appear in the group by clause. So select * and group by don't get along. Remove groupBy('room_id') in your relationship and see how that goes.
Alternatively, disable MySQL strict mode by going into config/database.php and setting strict => false for MySQL, if you are using MySQL version >5.7.
source

Laravel whereJsonContains query not working in 5.7

I have been facing a problem in my project. I want to search my json data. But could not achieve to do this. Here is my query
$variations = User::whereJsonContains('addon->ram', '>', 0)->count();
return $variations;
And My json data is stored in the addon column and the data is
{"ram":"87","base-driven":"89"}
In this query I am getting error says
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; (SQL: select count(*) as aggregate from
users where 0 json_contains(addon->'$."ram"', ">")
I google it but could not solve this
Thanks in advance
Just try this
$variations = User::where('addon->ram', '>', 0)->count();
return $variations;
and let time know, if you get some error.
As you are saving it as string, you may need to do something like
WHERE JSON_EXTRACT(addon, "$[ram]") > 0; in Laravel
It's because of you are providing "87" and "89" as strings. Try to replace it with numbers:
{"ram":87,"base-driven":89}

Error with Find query in cakephp

CakePHP Code
$data = $this->DropDownMultiple->find('all',array(
'conditions'=>array('FIND_IN_SET(?,DropDownMultiple.interest)' => array('football')),
'order'=>'created_on desc'
)
);
SQL Query
SELECT DropDownMultiple.*
FROM `cakephp_tutorial`.`drop_down_multiples` AS `DropDownMultiple`
WHERE FIND_IN_SET('football',`DropDownMultiple`.`interest`) =
ORDER BY `created_on` DESC
Syntax Error
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY created_on desc' at line 1
Problem is insert = sign at the end of where condition. Why this happen? Where am I wrong? help me.
Why don't you use the code that I've posted in the comments of the original questions answer?
You seem to have managed to remove the hidden chars that I've mentioned, at least they are not present in the code you are showing here anymore, not sure about the code that you are actually using though.
However, you are not using the format that I've shown you:
$data = $this->DropDownMultiple->find('all', array(
'conditions' => array(
'FIND_IN_SET(?, `DropDownMultiple`.`interest`)' => 'football'
),
'order' => 'created_on desc'
)
);
Note the quotes around the model and field name, and most important, the space after the ,.
Requiring that space might be a bug in CakePHP, not sure.
Also note that there's no need to wrap the value into an array, even though it works in case it contains only a single entry.

Codeigniter $this->db->select() not work with Round(Max()) or FORMAT(MAX())

I write following code
$this->db->select('SUM(qty) as total_qty,(FORMAT(SUM(amount),2)) as total_amount');
$this->db->where('Invoice_Rec_No',$Invoice_Rec_No);
$result=$this->db->get($this->invoice_products_tbl);
$total_data=$result->row();
but getting error
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (tbl_invoice_products) WHERE Invoice_Rec_No = 7' at line 2
SELECT SUM(qty) as total_qty, (FORMAT(SUM(amount), 2)) as total_amount FROM (tbl_invoice_products) WHERE Invoice_Rec_No = 7
Filename: C:\wamp\www\admin_followme247_master\system\database\DB_driver.php
I want to execute this query with codeigniter ActiveRecord.
User second parameter FALSE in db select method
$this->db->select('SUM(qty) as total_qty,
(FORMAT(SUM(amount),2)) as total_amount', FALSE);
CI db class is automatic add (apostrophe) when manipulate sql query, if you pass send parameterFALSEinselect` method then it is keep same as input.
You can format your code like this
$select = array(
'SUM(qty) as total_qty',
'(FORMAT(SUM(amount),2)) as total_amount'
);
$this->db->select($select);
$this->db->where('Invoice_Rec_No',$Invoice_Rec_No);
$result=$this->db->get($this->invoice_products_tbl);
$total_data=$result->row();
For simple column selection it is fine to use string but for complex selection like this one you can either use an array or select each column separately

Categories