获取模型名,以便在CakePHP的AppModel中实现可重用的方法


Get the model name to implement a reusable method in AppModel in CakePHP

如何在AppModel中获得当前模型名称?

我有这个代码验证密码和确认密码。但我想把它放在AppModelp:

function isSameAs($check, $field) {
  if( $check === $this->data['User'][$field] ) {
    return true;
  }
  else {
    return false;
  }
}

我用的是CakePHP 2

您最好使用$this->alias(参见API的模型)。使用@Ben Lee的建议,这将是:

function isSameAs($check, $field) {
    return $check === $this->data[$this->alias][$field];
}