简体   繁体   English

在Yii2中获取模型属性的类型

[英]Get model attribute's type in Yii2

How can I check type (column type) of model's attribute in Yii2? 如何在Yii2中检查模型属性的类型(列类型)?

This old Yii Forum answer gave me a conclusion, that in Yii1 I can use something like that: 这个古老的Yii论坛答案给了我一个结论,那就是在Yii1中,我可以使用类似的方法:

$model->getMetaData()->columns['attribute-name']->type;

But I was unsuccessful in porting this solution to Yii2. 但是,我无法将此解决方案移植到Yii2。 Can someone help? 有人可以帮忙吗?

You can use: 您可以使用:

$model->getTableSchema()->getColumn('attr')

or: 要么:

$model->getTableSchema()->columns['attr']

and then read dbType , phpType or type , according to what... type of type you're looking for. 然后根据您要查找的类型...读取dbTypephpTypetype

For example: 例如:

$model->getTableSchema()->columns['attr']->type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM