3

HABTM 関連付けがあり、データを結合テーブルに保存できません。

これが私のコントローラーです:

    public function create($id)
    {   

     if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
     $this->Invoice->id = $id;
     if (!$this->Invoice->exists()) throw new NotFoundException('Invalid ID');

    $this->set('invoice_id',$id);

    $names = $this->Invoice->find('list',array(
    'fields'=>array('template_id'),
    'conditions'=>array('id'=>$id)));

    $fields = $this->Field->find('all', array(
     'conditions'=>array(
     'template_id'=>$names)));

    $this->set(compact('fields'));

    $this->set('name',$names);
    $this->Invoice->create();
    if(empty($this->data)){
        $this->data= $this->Field->read($fields);
    } 
    else{
        if(($this->Invoice->saveAll($this->data)))
        {       
            $this->Session->setFlash('The field has been updated');
            $this->redirect(array('controller'=>'invoices', 'action'=>'index'));

        }
        }
}

フォームは次のとおりです。

<?php echo $this->Form->create('Invoice'); ?>
    <?php foreach ($fields as $field): ?>
    <?php echo debug($field);?>
    <?php echo $this->Form->Input($field['Field']['name'], array('default' =>$field['Field']['default_value'])); ?>
    <?php endforeach ;?>
    <?php echo $this->Form->End('Submit');?>

デバッグすると、次の$fieldようになります。

プレート

Employees Create New Pay Invoice Invoices Sent
\app\View\Invoices\create.ctp (line 21)
array(
    'Field' => array(
        'id' => '9',
        'name' => 'amount',
        'description' => 'amount of invoice',
        'field_type' => 'int',
        'field_range' => '10',
        'active' => true,
        'template_id' => '3',
        'default_value' => ''
    ),
    'Template' => array(
        'id' => '3',
        'name' => 'MGDKiallaConsulting',
        'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
        'account_id' => '3',
        'active' => '1'
    ),
    'Invoice' => array(
        (int) 0 => array(
            'id' => '1',
            'active' => true,
            'sender_id' => '2',
            'receiver_id' => '3',
            'template_id' => '1',
            'created' => '2012-08-05 00:00:00',
            'FieldsInvoice' => array(
                'id' => '1',
                'field_id' => '9',
                'invoice_id' => '1',
                'entered_value' => '1000.00'
            )
        ),
        (int) 1 => array(
            'id' => '2',
            'active' => true,
            'sender_id' => '2',
            'receiver_id' => '4',
            'template_id' => '1',
            'created' => '2012-08-05 00:00:00',
            'FieldsInvoice' => array(
                'id' => '2',
                'field_id' => '9',
                'invoice_id' => '2',
                'entered_value' => '2000.00'
            )
        )
    )
)
Amount
\app\View\Invoices\create.ctp (line 21)
array(
    'Field' => array(
        'id' => '10',
        'name' => 'description',
        'description' => 'description of invoice charges',
        'field_type' => 'text',
        'field_range' => null,
        'active' => true,
        'template_id' => '3',
        'default_value' => ''
    ),
    'Template' => array(
        'id' => '3',
        'name' => 'MGDKiallaConsulting',
        'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
        'account_id' => '3',
        'active' => '1'
    ),
    'Invoice' => array()
)
Description
\app\View\Invoices\create.ctp (line 21)
array(
    'Field' => array(
        'id' => '11',
        'name' => 'totalacctowing',
        'description' => 'total account amount owing',
        'field_type' => 'int',
        'field_range' => null,
        'active' => true,
        'template_id' => '3',
        'default_value' => ''
    ),
    'Template' => array(
        'id' => '3',
        'name' => 'MGDKiallaConsulting',
        'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
        'account_id' => '3',
        'active' => '1'
    ),
    'Invoice' => array()
)
Totalacctowing
\app\View\Invoices\create.ctp (line 21)
array(
    'Field' => array(
        'id' => '12',
        'name' => 'pmtinfo',
        'description' => 'the payment information',
        'field_type' => 'text',
        'field_range' => null,
        'active' => true,
        'template_id' => '3',
        'default_value' => 'hi'
    ),
    'Template' => array(
        'id' => '3',
        'name' => 'MGDKiallaConsulting',
        'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
        'account_id' => '3',
        'active' => '1'
    ),
    'Invoice' => array()
)

fields次を含むテーブルがあります-

id, name, default_value, template_id, active

次を含むテーブルがありinvoicesます -

id、sender_id、receiver_id、template_id、アクティブ

次を含むfields_invoicesテーブルがあります -id, invoice_id, field_id, entered_value

関数とビューから、invoice_id、field_id、および入力した値をフォームに保存する必要があります。

4

1 に答える 1

0

これは、以下を使用する代わりに達成することもできません$this->Invoice->saveAll(..),:

if(($this->Invoice->saveAssociated($this->data, array('deep'=> true))))

    {       
        $this->Session->setFlash('The field has been updated');
        $this->redirect(array('controller'=>'invoices', 'action'=>'index'));

    }

確かに紛らわしいドキュメントからのsaveAssociatedに関する情報。

于 2013-03-27T03:11:02.820 に答える