2012-10-03 16 views
0

這裏是我的模型:建立在我的模型中的任意屬性

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> 

回答

0

嘗試直接與集添加:

$this->set('tipoPersona', $this->Persona->TipoPersona); 

它像其他人一樣在模型屬性。用同樣的方法設置$ this-> Persona-> id幾行以上:)

1

的方法read()返回一個數組,你不能把這個對象的屬性TipoPersona

,我建議你在這個表中添加一個字段指定類型的人,比使用read()結果,如:

<?php echo $persona['Persona']['tipo_persona']; ?> 
0

你可以把它添加到使用型號::一個afterFind結果陣列()回電話。

您確定這不應該是模型中的常規字段嗎?