PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

[FIXED] How do you deal with aliases when using Yii's ORM?

 March 19, 2022     yii     No comments   

Issue

$sql = "SELECT  `prod_id` AS  `id` , LOWER(  `prod_iso_code_5` ) AS  `prod_code` FROM `prod_data`";

I have this code and while the documentation tells you what to do in most cases they don't tell you what to do when you have a SQL function with a string as a parameter.

I did the following thinking this will work, but because the documentation doesn't cover all cases I am not 100% sure. Will the following command work?

$result =  $this->mydb->createcommand()
            ->select(array('prod_id AS id', 'LOWER("prod_iso_code_5") as prod_code')
            ->from('prod_data')
            ->queryAll();

Solution

It will not output what you are expecting. Because in your query builder you've used "prod_iso_code_5" as a string. Which is evaluated as string for LOWER() function. If you want to lower case the prod_data.product_iso_code_5 column's value as given in the query, you've to use either just product_iso_code_5 or wrap column name within ` (backtick) characters

$result =  $this->mydb->createcommand()
            ->select(array('prod_id AS id', 'LOWER(`prod_iso_code_5`) as prod_code')
            ->from('prod_data')
            ->queryAll();


Answered By - Gihan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing