Cakephp - 填充表单关联模型

时间:2015-02-12 10:40:54

标签: cakephp

我正在尝试使用相关模型填充表单模型。

主要模式:Pronostic

儿童模特(hasMany):Prono

控制器:

$Pronostic = $this->Pronostic->find('first',array('recursive'=>1,'conditions'=>array('Pronostic.user_id'=>$this->Auth->user('id'),'Pronostic.id'=>$this->request['named']['id'])));
[...]
$this->request->data = $Pronostic;

debug($Pronostic) :
    array(
        'Pronostic' => array(
            'user_id' => '12',
            'id' => '2',
            'name' => 'My nanme',
            'content' => '',
            'star' => '5',
            'active' => '0',
            'slug' => '',
            'created' => '2015-02-11 20:51:26',
            'modified' => '2015-02-12 07:17:57'
        ),

        'Prono' => array(
            (int) 0 => array(
                'user_id' => '12',
                'sport_id' => '0',
                'bookmaker_id' => '0',
                'event_id' => '2',
                'pronostic_id' => '2',
                'league_id' => '0',
                'id' => '20',
                'created' => '2015-02-11 20:57:56',
                'modified' => '2015-02-11 20:57:56',
                'bet' => 'bet1',
                'odd' => '12',
                'amount' => '12',
                'level' => '0'
            ),
            (int) 1 => array(
                'user_id' => '12',
                'sport_id' => '0',
                'bookmaker_id' => '0',
                'event_id' => '1',
                'pronostic_id' => '2',
                'league_id' => '0',
                'id' => '19',
                'created' => '2015-02-11 20:57:56',
                'modified' => '2015-02-11 20:57:56',
                'bet' => 'bet2',
                'odd' => '24',
                'amount' => '24',
                'level' => '0'
            )
        )
    )

查看:

    echo $this->Form->create('Pronostic', array('class'=>'form-horizontal widget-login')); 

    echo $this->Form->input('Pronostic.name',array('label'=>false,'class'=>'form-control')); 

    echo $this->Form->input('Pronostic.Prono.'.$Prono['Prono']['id'].'.id',array('value'=>$Prono['Prono']['id'])); 

echo $this->Form->input('Pronostic.Prono.'.$Prono['Prono']['id'].'.bet',array('label'=>false,'class'=>'form-control'));

Pronostic.name填写正确,但不是Pronostic

有人可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

我得到这样的$ Prono,因为Pronostic有许多Prono:

foreach($Pronostic['Prono'] as $Prono){
    ...
    }