0

私は請求システムに取り組んでおり、単一のフォームに請求書を追加しようとしています。現在、invoices_works テーブルではなく、invoices テーブルに追加する機能しかありません。これが私が取り組んでいるものです。非常に多くのコードを追加して申し訳ありません:

私の請求表:

CREATE TABLE `invoices` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`commission` double NOT NULL DEFAULT '30',
`location_id` int(11) unsigned NOT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `location_id` (`location_id`),
CONSTRAINT `invoices_ibfk_1` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

私のinvoices_worksテーブル:

CREATE TABLE `invoices_works` (
`invoice_id` int(11) unsigned NOT NULL,
`work_id` int(11) unsigned NOT NULL,
`count` int(11) NOT NULL DEFAULT '1',
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `invoice_id` (`invoice_id`),
KEY `work_id` (`work_id`),
CONSTRAINT `invoices_works_ibfk_2` FOREIGN KEY (`work_id`) REFERENCES `works` (`id`),
CONSTRAINT `invoices_works_ibfk_1` FOREIGN KEY (`invoice_id`) REFERENCES `invoices` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

私のワークテーブル:

CREATE TABLE `works` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`cost` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

私の請求書モデル (少なくとも HABTM):

public $hasAndBelongsToMany = array(
        'Work' => array(
                    'className' => 'Work',
                    'joinTable' => 'invoices_works',
                    'foreignKey' => 'invoice_id',
                    'associationForeignKey' => 'work_id'
                )
        );

コントローラーに追加する方法:

public function add() {
    if($this->request->is('post')) {
        $this->Invoice->create();
        if ($this->Invoice->saveAssociated($this->request->data)) {
            $this->Session->setFlash(_('The invoice has been saved.'), true);
            echo var_dump($this->data);
            $this->redirect(array('action' => 'index'));
        }
        else {
            $this->Session->setFlash(_('The invoice could not be saved.', true));
        }
    }
    $work = $this->Work->find('list');
    $location = $this->Location->find('list', array('conditions' => array('Location.id !=' => 0)));
    $this->set('works', $work);
    $this->set('locations', $location);
}

そして最後に、ビュー自体:

<?php

 echo $this->Form->create('Invoice');
echo "<fieldset>";
    echo $this->Form->input('location_id');
    echo $this->Form->input('commission', array('default' => 30));
echo "</fieldset>";
    //echo $this->Form->hidden('InvoiceWork.invoices_id', array('default' => 1));
echo "<fieldset>";
    echo $this->Form->input('InvoicesWork.work_id');
    echo $this->Form->input('InvoicesWork.count', array('default' => 1));
echo "</fieldset>";

 echo $this->Form->end('Save Invoice');

?>

何かが欠けているように思えます。

4

1 に答える 1

0

キーボードに顔をぶつけて数時間後、問題は解決しました。最終的に、2つのテーブルに別々の追加関数を使用しました。

public function add() {
    if($this->request->is('post')) {
        $this->Invoice->create();
        if ($invoice = $this->Invoice->saveAll($this->request->data)) {
            $this->Session->setFlash(_('The invoice has been saved.'), true);
            if(!empty($invoice)) {
                $this->request->data['InvoicesWork']['invoice_id'] = $this->Invoice->id;

                $this->Invoice->InvoicesWork->save($this->request->data);
                echo "InvoicesWork save completed?<br/>";
                echo var_dump($this->request->data);
            }
            $this->redirect(array('action' => 'index'));
        }
        else {
            $this->Session->setFlash(_('The invoice could not be saved.', true));
        }
    }
    $work = $this->Work->find('list');
    $location = $this->Location->find('list', array('conditions' => array('Location.id !=' => 0)));
    $this->set('works', $work);
    $this->set('locations', $location);
}

これは最も洗練されたソリューションではありませんが、請求書テーブルにデータを挿入した後、そこから新しいIDを取得して、配列に格納します。次に、InvoicesWorkモデルからadd関数を呼び出すだけです。

于 2013-03-08T03:04:38.207 に答える