Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int ID is not type casting as integer in the REST API #6797

Closed
Giancarlo74 opened this issue Jan 8, 2015 · 6 comments
Closed

int ID is not type casting as integer in the REST API #6797

Giancarlo74 opened this issue Jan 8, 2015 · 6 comments

Comments

@Giancarlo74
Copy link

int ID is not type casting as integer in the REST API

This is the JSON response:

[{"ID":"48","CODICE_SPEEDY":"58","DESCRIZIONE":"ATTESA RISPOSTA CLIENTE"},      {"ID":"49","CODICE_SPEEDY":"59","DESCRIZIONE":"ATTIVITÀ A PREVENTIVO"}]

but ID is integer and not string

this is the mysql schema:

SET FOREIGN_KEY_CHECKS=0;


-- Table structure for interventi


DROP TABLE IF EXISTS interventi;
CREATE TABLE interventi (
ID int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
SERVIZIO varchar(15) CHARACTER SET latin1 NOT NULL,
CODICE_CLIENTE varchar(20) CHARACTER SET latin1 NOT NULL,
CODICE_SPEEDY char(5) CHARACTER SET latin1 NOT NULL DEFAULT '',
DESCRIZIONE varchar(100) CHARACTER SET latin1 NOT NULL DEFAULT '',
DESCRIZIONE_COMPLETATO varchar(100) CHARACTER SET latin1 NOT NULL,
QUALIFICAZIONE char(1) CHARACTER SET latin1 NOT NULL,
ASSEGNAZIONE char(1) CHARACTER SET latin1 NOT NULL,
PIANIFICAZIONE char(1) CHARACTER SET latin1 NOT NULL,
ESECUZIONE char(1) CHARACTER SET latin1 NOT NULL,
ATTESA_CLIENTE char(1) CHARACTER SET latin1 NOT NULL DEFAULT '',
ATTESA_PARTE char(1) CHARACTER SET latin1 NOT NULL DEFAULT '',
PRESTITO_USO char(1) CHARACTER SET latin1 NOT NULL DEFAULT '',
RISOLTO char(1) CHARACTER SET latin1 NOT NULL,
COMPLETATO char(1) CHARACTER SET latin1 NOT NULL DEFAULT '',
COMPLETATO_CON_PROBLEMI char(1) CHARACTER SET latin1 NOT NULL,
ANNULLATO char(1) CHARACTER SET latin1 NOT NULL DEFAULT '',
RICHIESTA_INFORMAZIONI char(1) CHARACTER SET latin1 NOT NULL DEFAULT '',
PRIMARY KEY (ID),
KEY ID (ID) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=398 DEFAULT CHARSET=utf8;


-- Records of interventi


INSERT INTO interventi VALUES ('00000000048', '', '', '58', 'ATTESA RISPOSTA CLIENTE', '', '', '', 'S', 'S', 'S', '', '', '', '', '', '', 'S');
INSERT INTO interventi VALUES ('00000000049', '', '', '59', 'ATTIVITÀ A PREVENTIVO', 'ATTIVITÀ A PREVENTIVO', '', '', 'S', 'S', 'S', 'S', '', '', 'S', 'S', '', 'S');

This is the controller InterventiController.php:

false, 'query' => Interventi::elencoDescrizioniInterventi(), ]); } } ?>

This is the model Interventi.php

15], [['CODICE_CLIENTE'], 'string', 'max' => 20], // [['CODICE_SPEEDY'], 'string', 'max' => 5], [['CODICE_SPEEDY'], 'integer'], [['DESCRIZIONE', 'DESCRIZIONE_COMPLETATO'], 'string', 'max' => 100], [['QUALIFICAZIONE', 'ASSEGNAZIONE', 'PIANIFICAZIONE', 'ESECUZIONE', 'ATTESA_CLIENTE', 'ATTESA_PARTE', 'PRESTITO_USO', 'RISOLTO', 'COMPLETATO', 'COMPLETATO_CON_PROBLEMI', 'ANNULLATO', 'RICHIESTA_INFORMAZIONI'], 'string', 'max' => 1] ]; } /** - @inheritdoc */ public function attributeLabels() { return [ 'ID' => Yii::t('app', 'ID'), 'SERVIZIO' => Yii::t('app', 'Servizio'), 'CODICE_CLIENTE' => Yii::t('app', 'Codice Cliente'), 'CODICE_SPEEDY' => Yii::t('app', 'Codice Speedy'), 'DESCRIZIONE' => Yii::t('app', 'Descrizione'), 'DESCRIZIONE_COMPLETATO' => Yii::t('app', 'Descrizione Completato'), 'QUALIFICAZIONE' => Yii::t('app', 'Qualificazione'), 'ASSEGNAZIONE' => Yii::t('app', 'Assegnazione'), 'PIANIFICAZIONE' => Yii::t('app', 'Pianificazione'), 'ESECUZIONE' => Yii::t('app', 'Esecuzione'), 'ATTESA_CLIENTE' => Yii::t('app', 'Attesa Cliente'), 'ATTESA_PARTE' => Yii::t('app', 'Attesa Parte'), 'PRESTITO_USO' => Yii::t('app', 'Prestito Uso'), 'RISOLTO' => Yii::t('app', 'Risolto'), 'COMPLETATO' => Yii::t('app', 'Completato'), 'COMPLETATO_CON_PROBLEMI' => Yii::t('app', 'Completato Con Problemi'), 'ANNULLATO' => Yii::t('app', 'Annullato'), 'RICHIESTA_INFORMAZIONI' => Yii::t('app', 'Richiesta Informazioni'), ]; } private function elencoInterventiZZ($St, $Servizio, $Type) { $St = preg_replace('/\ /','_',$St); $Descr = $St != 'COMPLETATO' ? 'DESCRIZIONE' : 'DESCRIZIONE_COMPLETATO as DESCRIZIONE'; $sql = 'SELECT ID*1 as ID, if(CODICE_SPEEDY = "",ID *1,CODICE_SPEEDY) as CODICE_SPEEDY, ' . $Descr . ' FROM interventi '; $sql .= ' order by DESCRIZIONE '; return Interventi::findBySql($sql); } public function elencoDescrizioniInterventi($chiamata) { $St = ''; $Servizio = ''; $Type = ''; $results = Interventi::elencoInterventiZZ($St, $Servizio, $Type); return $results; } } ?>

and this is the configuration into config/web.php

        'rules' => [            
            [
                'class' => 'yii\rest\UrlRule',
                'controller' => 'interventi',   // our country api rule,
                'pluralize'=>false,
                'tokens' => [
                    '{id}' => '<id:\\w+>'
                ]
            ],                
        ],
@qiangxue
Copy link
Member

qiangxue commented Jan 8, 2015

Your ID column is unsigned int. If your PHP is running in 32bit mode, only string can accurately represent its values. You may override fields() to do typecasting if you are sure using int won't cause problem.

@qiangxue qiangxue closed this as completed Jan 8, 2015
@Giancarlo74
Copy link
Author

How can i override fields() to do typecasting ?

public function fields()
{
$fields = parent::fields();
$fields = [
'ID',
'CODICE_SPEEDY',
'DESCRIZIONE',
'DESCRIZIONE_COMPLETATO',
];
return $fields;
}

@qiangxue
Copy link
Member

qiangxue commented Jan 8, 2015

@Giancarlo74
Copy link
Author

I have read the documentation, I also tried to ask for help on StackOverflow,

http://stackoverflow.com/questions/27857574/yii2-how-ovverride-integer-fields-into-model/27871942?iemail=1&noredirect=1#27871942

but it seems that it is impossible to force the ID field to be managed as integer, using the typecasting of the function fields ()

@qiangxue
Copy link
Member

Please read the doc carefully. Your fields() method should be declared as

[
    'ID' => function ($model) {
         return intval($model->ID);
    }
]

@enkr1
Copy link

enkr1 commented Apr 7, 2021

Please read the doc carefully. Your fields() method should be declared as

[
    'ID' => function ($model) {
         return intval($model->ID);
    }
]

It works for me! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants