alexgr
(Alex)
1
Hi,
I would like to use MySQL Native Driver with Yii2 for all database connections, whether I use ActiveRecord or write "raw" queries.
Right now I am noticing that the types I get back are not the ones I had defined in the database.
I am using PHP 7 and MySql 5.7, so this should be possible.
When I do php -i | grep mysqlnd it’s enabled.
Do I need to change some sort of Yii2 setting?
Thanks for your help anyone, this type juggling is driving me bonkers.
alexgr
(Alex)
3
So e.g. I have this MySQL table, with these columns (abbreviated for clarity):
consolidated tinyint(1)
esl_id int(11)
amount float
unit varchar(200)
Then when I do this from Yii2:
$get_ingredients = "SELECT consolidated, esl_id, amount, unit
FROM `earlier_shopping_list_details` WHERE esl_id = 10030;";
$command = \Yii::$app->db->createCommand($get_ingredients);
$test= $command->queryAll();
var_dump($test); exit;
I get to see this, all strings as you can see:
array (size=2)
0 =>
array (size=4)
'consolidated' => string '1' (length=1)
'esl_id' => string '10030' (length=5)
'amount' => string '255' (length=3)
'unit' => string 'gram' (length=4)
1 =>
array (size=4)
'consolidated' => string '1' (length=1)
'esl_id' => string '10030' (length=5)
'amount' => string '128' (length=3)
'unit' => string 'gram' (length=4)
I really need consolidated to be boolean, esl_id to be integer, amount to be float and unit to be a string.
Thanks for your help,
Alex
jacmoe
(Jacob Moen)
4
alexgr
(Alex)
5
Thanks for that pointer.
Could be me, but in order to preserve precision, I would not have them represented as strings, but as they are in the database.
Since the Yii Db Connection class is based on PDO, is this something configurable or would I have to hack the base classes?
Alex