인보이스 발행 시스템에서 일하고 있으며 단일 양식으로 인보이스를 추가하려고합니다. 지금 당장 나는 invoices_works 테이블이 아닌 내 invoices 테이블에만 추가 할 수있는 권한을 얻고 있습니다. 여기 내가 함께 일하는 것이있다. 너무 많은 코드를 추가 사전에 죄송합니다 :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;
내 송장 모델 (HAB ™ 이상) :
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');
?>
내가 뭔가를 누락 것 같다, 나는 그것을 분명있을거야 알고 일단 내가 찾으면.
[saveAll()] (http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null- array-options-array)를 사용하십시오. – Oldskool
예, 또는'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
작업 수행을 향상시키기위한 제안. $ this-> Work-> find ('list')'...'$ this-> set (....)'코드를 액션의 * end *로 옮기십시오. 이 u 리는 양식이 제출되지 않거나 저장에 실패한 경우에만 사용됩니다. 시작시에 결과를 사용하지 않더라도 항상 실행됩니다. – thaJeztah