在 CakePHP 中获取模型关联别名的别名
Get alias of model association alias in CakePHP
我正在使用 CakePHP 2.6.3。
我对同一个模型有多个关联,我想根据它是哪个关联应用一些略有不同的 beforeValidate() 逻辑。我认为在模型中 $this->alias
会更新为协会的别名,但它似乎只是 class 名称。
使用食谱中的 Multiple Relations to Same Model 示例,我如何判断发件人(用户模型)或收件人(用户模型)是否正在验证?
function beforeValidate($options = array()) {
if ($association == 'Sender') {
// do something
} else {
// do something different
}
}
$alias 属性 仅在通过关联使用模型时使用与原始模型名称不同的名称。别名应始终在模型本身中使用。例如 $this->Blog->Author
用户模型的别名将在此处 "Author",假设是这样的:
$belongsTo = ['Author' => ['className' => 'User']];
因此,要么通过验证的一侧保存数据,要么简单地将标识符与数据一起传递。
if ($this->data[$this->alias]['is_sender'] == true) { /*...*/ } else { /*...*/ }
在你的 beforeValidate() 中。
如果不是发送者就是接收者。
我正在使用 CakePHP 2.6.3。
我对同一个模型有多个关联,我想根据它是哪个关联应用一些略有不同的 beforeValidate() 逻辑。我认为在模型中 $this->alias
会更新为协会的别名,但它似乎只是 class 名称。
使用食谱中的 Multiple Relations to Same Model 示例,我如何判断发件人(用户模型)或收件人(用户模型)是否正在验证?
function beforeValidate($options = array()) {
if ($association == 'Sender') {
// do something
} else {
// do something different
}
}
$alias 属性 仅在通过关联使用模型时使用与原始模型名称不同的名称。别名应始终在模型本身中使用。例如 $this->Blog->Author
用户模型的别名将在此处 "Author",假设是这样的:
$belongsTo = ['Author' => ['className' => 'User']];
因此,要么通过验证的一侧保存数据,要么简单地将标识符与数据一起传递。
if ($this->data[$this->alias]['is_sender'] == true) { /*...*/ } else { /*...*/ }
在你的 beforeValidate() 中。
如果不是发送者就是接收者。