InnoDB和cakePHP 3中的保存关联
我有这个数据库表:InnoDB和cakePHP 3中的保存关联,php,orm,innodb,cakephp-3.0,Php,Orm,Innodb,Cakephp 3.0,我有这个数据库表: CREATE TABLE IF NOT EXISTS `businesses` ( `id` int(10) unsigned NOT NULL, `record_id` int(10) unsigned DEFAULT NULL, `businesstype_id` int(10) unsigned DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCRE
CREATE TABLE IF NOT EXISTS `businesses` (
`id` int(10) unsigned NOT NULL,
`record_id` int(10) unsigned DEFAULT NULL,
`businesstype_id` int(10) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=1 ;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `businesses`
--
ALTER TABLE `businesses`
ADD PRIMARY KEY (`id`), ADD KEY `FK_businesses_records` (`record_id`), ADD KEY `FK_businesses_businesstypes` (`businesstype_id`);
它有innodb引擎并使用外键
src/controller/BusinessesController添加的控制器代码
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use App\Model\Entity\Record;
/**
* Businesses Controller
*
* @property App\Model\Table\BusinessesTable $Businesses
*/
class BusinessesController extends AppController {
/**
* Index method
*
* @return void
*/
public function index() {
$this->paginate = [
'contain' => ['Records', 'Businesstypes']
];
$this->set('businesses', $this->paginate($this->Businesses));
}
/**
* View method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function view($id = null) {
$business = $this->Businesses->get($id, [
'contain' => ['Records', 'Businesstypes']
]);
$this->set('business', $business);
}
/**
* Add method
*
* @return void
*/
public function add() {
//$business = $this->Businesses->newEntity($this->request->data);
$records = TableRegistry::get('Records');
$entity = $records->newEntity($this->request->data(),[
'associated' => ['Businesses.Businesstypes', 'Emails','Telephones.Telephonetypes','Addresses']
]);
if ($this->request->is('post')) {
if ($records->save($entity)) {
$this->Flash->success('The b usiness has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The business could not be saved. Please, try again.');
}
// if ($this->Businesses->save($business)) {
// $this->Flash->success('The business has been saved.');
// return $this->redirect(['action' => 'index']);
// } else {
// $this->Flash->error('The business could not be saved. Please, try again.');
// }
}
$telephonetypes = $this->Businesses->Records->Telephones->Telephonetypes->find('list');
$records = $this->Businesses->Records->find('list');
$businesstypes = $this->Businesses->Businesstypes->find('list');
$this->set(compact('telephonetypes','entity', 'records', 'businesstypes'));
}
但是,当我尝试保存时,我得到“无法保存业务。请重试。”错误。当我检查sql日志时,除了“开始,回滚”之外,我看不到任何内容
任何帮助都将不胜感激
更新:保存相关数据需要如下表单:
<?=$this->element('business_actions')?>
<div class="businesses form large-10 medium-9 columns">
<?= $this->Form->create($entity) ?>
<fieldset>
<legend><?= __('Add Business') ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('url');
echo $this->Form->input('description');
echo $this->Form->input('businesses.0.businesstype_id', ['options' => $businesstypes]);
echo $this->Form->input('emails.0.email');
echo $this->Form->input('telephones.0.telephonetype_id', ['options' => $telephonetypes]);
echo $this->Form->input('telephones.0.telephone');
echo $this->Form->input('addresses.0.streetAddress');
echo $this->Form->input('addresses.0.addressLocality');
echo $this->Form->input('addresses.0.postalCode');
echo $this->Form->input('addresses.0.addressCountry');
echo $this->Form->input('addresses.0.description',['label'=>__('Address Description')]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
这看起来像是验证错误,您是否在尝试保存实体后检查了它们,以查看是否有任何属性被标记为无效?这不是因为验证。关联形式是这样的。
<?=$this->element('business_actions')?>
<div class="businesses form large-10 medium-9 columns">
<?= $this->Form->create($entity) ?>
<fieldset>
<legend><?= __('Add Business') ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('url');
echo $this->Form->input('description');
echo $this->Form->input('businesses.0.businesstype_id', ['options' => $businesstypes]);
echo $this->Form->input('emails.0.email');
echo $this->Form->input('telephones.0.telephonetype_id', ['options' => $telephonetypes]);
echo $this->Form->input('telephones.0.telephone');
echo $this->Form->input('addresses.0.streetAddress');
echo $this->Form->input('addresses.0.addressLocality');
echo $this->Form->input('addresses.0.postalCode');
echo $this->Form->input('addresses.0.addressCountry');
echo $this->Form->input('addresses.0.description',['label'=>__('Address Description')]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>