2013-03-07 51 views
0

我正在开发一个发票系统,并试图在一个表单中添加发票。现在我只能添加到我的发票表中,而不是我的invoices_works表。这是我正在与之合作。对不起事先添加了这么多代码:将数据添加到CakePHP中的多个表中

我的发票表:

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'); 

?> 

好像我失去了一些东西,我知道这将是明显一旦我找到它。

+0

看来你正在寻找[saveAll()](http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null- array-options-array)而不是save()。 – Oldskool 2013-03-07 19:46:03

+0

是或'saveAssociated()'http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array http: //book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto – thaJeztah 2013-03-07 21:01:36

+0

建议改善您行动的表现;将'$ this-> Work-> find('list')'...'$ this-> set(....)'代码的一部分移至您的操作的*结尾*处。只有在未提交表格或保存失败时才使用这些查询。通过将它们放在起点处,即使未使用它们的结果,它们也将始终执行。 – thaJeztah 2013-03-07 21:08:47

回答

0

经过几个小时粉碎我的脸对着我的键盘,我已经解决了我的问题。我最终为这两个表使用了单独的添加函数。

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 Model中调用add函数即可。

相关问题