Reputation: 754
I'm always getting syntax errors when I'm trying to perform mysql functions in my Yii2 query.
What I need to do is select a third column updated_at
and do the mysql function, which is written in the variable
Here's the code:
$contractExpiryDate = 'DATE_ADD(FROM_UNIXTIME(updated_at), '
. 'INTERVAL term_months MONTH) AS contract_expiry_date';
$query = self::find()
->select([
self::tableName() . '.*',
Client::tableName() . '.company_name AS company_name',
ClientService::tableName() . '.agreement_no AS agreement_no',
ClientService::tableName() . $contractExpiryDate,
])->joinWith('client')
->joinWith('loans')
->orderBy('company_name');
Error message:
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 ') AS contract_expiry_date FROM
client_profit_loss_report
LEFT JOINclient
ON' at line 1 The SQL being executed was: SELECTclient_profit_loss_report
.*,client
.company_name
AScompany_name
,client_service
.agreement_no
ASagreement_no
, client_serviceDATE_ADD(FROM_UNIXTIME(updated_at), INTERVAL term_months MONTH) AS contract_expiry_date FROMclient_profit_loss_report
LEFT JOINclient
ONclient_profit_loss_report
.client_id
=client
.id
LEFT JOINclient_profit_loss_report_loan
ONclient_profit_loss_report
.id
=client_profit_loss_report_loan
.report_id
LEFT JOINclient_service
ONclient_profit_loss_report_loan
.client_service_id
=client_service
.id
ORDER BYcompany_name
Could someone explain what I'm doing wrong?
Upvotes: 0
Views: 1354
Reputation: 1492
This should be the code that works for you. You need to reference the table name on each field:
$query = self::find()
->select([
self::tableName() . '.*',
Client::tableName() . '.company_name AS company_name',
ClientService::tableName() . '.agreement_no AS agreement_no',
'DATE_ADD(
FROM_UNIXTIME(' . ClientService::tableName() . '.updated_at),
INTERVAL ' . ClientService::tableName() . '.term_months MONTH
) AS contract_expiry_date',
])->joinWith('client')
->joinWith('loans')
->orderBy('company_name');
If you wish to format the date as per you other comment, you simply need to wrap DATE_ADD
function call in a DATE_FORMAT
function call, like so:
DATE_FORMAT(DATE_ADD('2017-09-07', INTERVAL 1 MONTH), '%Y-%m-%d');
So with your query, like this:
$query = self::find()
->select([
self::tableName() . '.*',
Client::tableName() . '.company_name AS company_name',
ClientService::tableName() . '.agreement_no AS agreement_no',
'DATE_FORMAT(
DATE_ADD(
FROM_UNIXTIME(' . ClientService::tableName() . '.updated_at),
INTERVAL ' . ClientService::tableName() . '.term_months MONTH
),
'%Y-%m-%d'
) AS contract_expiry_date',
])->joinWith('client')
->joinWith('loans')
->orderBy('company_name');
Upvotes: 2
Reputation: 900
Not sure about to which table each column goes, but try this...
$contractExpiryDate = 'DATE_ADD(FROM_UNIXTIME('. ClientService::tableName() .'.updated_at), '
. 'INTERVAL '. ClientService::tableName() .'.term_months MONTH) AS contract_expiry_date';
$query = self::find()
->select([
self::tableName() . '.*',
Client::tableName() . '.company_name AS company_name',
ClientService::tableName() . '.agreement_no AS agreement_no',
$contractExpiryDate,
])->joinWith('client')
->joinWith('loans')
->orderBy('company_name');
Upvotes: 1