a.ziaratban
(Abolfazl Ziaratban)
1
hi
for example my os is 64 bit. i have a table structure like :
CREATE TABLE `MyTable` (
`col_int` int(11) NOT NULL,
`col_bigint` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
when i fetch col_bigint with findOne method, yii2 return as string or int ?
i know in 32 bit os, yii2 return as string.
samdark
(Alexander Makarov)
2
String for compatibility reasons.
samdark
(Alexander Makarov)
3
You may use AttributeTypecastBehavior to change this behavior.
a.ziaratban
(Abolfazl Ziaratban)
4
http://www.yiiframework.com/doc-2.0/yii-behaviors-attributetypecastbehavior.html
thanks.
i updated my yii2 to last version and use this behavior.
but i have a question.
i want detect overflow in type casting and throw a error for prevent this action.
how i can attach a function to this behavior for run before type casting ?
like :
public function behaviors()
{
return [
'typecast' => [
'class' => AttributeTypecastBehavior::className(),
'attributeTypes' => [
'myColumn' => AttributeTypecastBehavior::TYPE_INTEGER,
],
'typecastAfterFind' => true,
'beforeTypeCast' => function($value){
if(bccomp($value,(int)$value) !== 0)
throw new \Exception('Overflow !');
return (int)$value;
},
],
];
}
samdark
(Alexander Makarov)
5
public function behaviors()
{
return [
'typecast' => [
'class' => AttributeTypecastBehavior::className(),
'attributeTypes' => [
'myColumn' => function ($value) {
if (bccomp($value, (int)$value) !== 0) {
throw new \Exception('Overflow !');
}
return (int)$value;
},
],
'typecastAfterFind' => true,
],
];
}