Cakephp 在我的模型中创建任意属性
这是我的模型:Cakephp 在我的模型中创建任意属性,cakephp,model,Cakephp,Model,这是我的模型: class Persona extends AppModel { // This is just some arbitrary property I need to populate in the controller. public $TipoPersona = ''; } 这是我的控制器中的动作功能: public function details($id = null) { // Just a typical load by id.
class Persona extends AppModel {
// This is just some arbitrary property I need to populate in the controller.
public $TipoPersona = '';
}
这是我的控制器中的动作功能:
public function details($id = null) {
// Just a typical load by id.
$this->Persona->id = $id;
$this->set('persona', $this->Persona->read());
// Can I do something like?
$this->Persona->TipoPersona = "Mafa Woogly";
}
如何在此处的“模型”中设置$TipoPersona
属性?我的目的是在视图中使用该属性,如:
<tr>
<th>Tipo de Persona:</th>
<td><?php echo h($persona->TipoPersona); ?></td> // Or similar?
</tr>
个人信息:
//还是类似的?
有什么建议吗
这是可行的,但感觉不稳定,而且不是强类型的
public function details($id = null) {
$this->Persona->id = $id;
$this->set('persona', $this->Persona->read());
$this->set('tipoPersona', "Mafa woogly");
}
<tr>
<th>Tipo de Persona:</th>
<td><?php echo h($tipoPersona); ?></td>
</tr>
公共函数详细信息($id=null){
$this->Persona->id=$id;
$this->set('persona',$this->persona->read());
$this->set('tipoPersona','mafawoogly');
}
个人信息:
方法read()
返回数组,无法获取此对象的属性TipoPersona
我建议您在此表中添加一个字段以指定人员类型,然后使用read()
的结果,如:
<?php echo $persona['Persona']['tipo_persona']; ?>
您可以使用Model::afterFind()回调将其添加到结果数组中
您确定这不应该是模型中的常规字段吗?请尝试使用set直接添加它:
$this->set('tipoPersona', $this->Persona->TipoPersona);
它和模型中的其他所有人一样都是属性。与设置$this->Persona->id的方法相同(上面几行:)