如何检查cakephp2中是否存在model列?

kpbpu008  于 2022-11-11  发布在  PHP
关注(0)|答案(2)|浏览(106)
public function beforSave($option = array()) {
   if($this->columnName) {
      // Statement
   }
}

差不多吧

if(!$this->loadModel($type)) {
   // Statement
}
cngwdvgl

cngwdvgl1#

获取模型架构

$this->modelName->schema();

检查我的字段是否可用

if (!empty($this->modelName->schema('date_created'))) {
    // statement
}

或者

public function beforeSave($options = array()) {  
   if ($this->hasField('date_created')) {
       $this->data[$this->alias]['date_created'] = date('Y-m-d H:i:s');
   }
}
tp5buhyn

tp5buhyn2#

请尝试使用getColumnType数组键存在;
getColumnTypes()返回一个由字段名和列类型组成的关联数组。

$fileds = $this->YourModelName->getColumnTypes();

 if(array_key_exists('columnName', $fileds)) {
    // Statement
 }

相关问题