【问题标题】:Adding data to multiple tables in CakePHP在 CakePHP 中向多个表中添加数据
【发布时间】:2013-03-07 18:52:04
【问题描述】:

我正在开发一个发票系统,我正在尝试以单一形式添加发票。现在我只能添加到我的 invoices 表,而不是 invoices_works 表。这是我正在使用的。提前抱歉添加了这么多代码:

我的发票表:

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

Controller中的添加方法:

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

?>

我好像遗漏了什么,我知道一旦找到它就会很明显。

【问题讨论】:

  • 您似乎在寻找 saveAll() 而不仅仅是 save()。
  • 提高行动绩效的建议;将代码的 $this-&gt;Work-&gt;find('list') ... $this-&gt;set(....) 部分移动到操作的 end。仅当未提交表单或保存失败时才使用这些查询。通过将它们放在开头,它们将始终被执行,即使它们的结果未被使用。
  • 而且,当我在做的时候,你使用的是 CakePHP 2.x 吗?如果是这样, $this->data 仅用于向后兼容。请改用$this-&gt;request-&gt;data,并检查是否已通过if($this-&gt;request-&gt;is('post'){....}提交表单
  • 我已经进行了这些更改,但仍然没有将任何内容保存到 invoices_works 表中。我需要那个表的控制器和模型吗? (我也尝试了 saveAll() 和 saveAssociated()。我使用 var_dump 来获取我正在使用表单构建的数组。array(2) { ["Invoice"]=> array(2) { [ "location_id"]=> string(1) "3" ["commission"]=> string(2) "30" } ["InvoicesWork"]=> array(2) { ["work_id"]=> string(1 ) "3" ["count"]=> 字符串(1) "1" } }

标签: mysql cakephp formhelper


【解决方案1】:

在我的脸用键盘砸了几个小时后,我解决了我的问题。我最终为这两个表使用了单独的添加函数。

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 函数。

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多