Eup*_*des 2 cakephp associations has-one belongs-to cakephp-3.0
所以我有 2 个模型:插件和导入。该关联是 Plugins hasOne Imports:
$this->hasOne('Imports', [
'foreignKey' => 'plugin_id'
]);
Run Code Online (Sandbox Code Playgroud)
并导入属于插件:
$this->belongsTo('Plugins', [
'foreignKey' => 'plugin_id',
'joinType' => 'INNER'
]);
Run Code Online (Sandbox Code Playgroud)
我的插件表转储如下所示:
CREATE TABLE `plugins` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(50) NOT NULL,
`pluginkey` varchar(100) NOT NULL,
`pluginname` varchar(255) DEFAULT NULL,
`description` text,
`integrationinstructions` text,
`date_available` datetime DEFAULT NULL,
`date_last_changed` datetime DEFAULT NULL,
`author` varchar(255) DEFAULT NULL,
`version` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Run Code Online (Sandbox Code Playgroud)
我的导入表是:
CREATE TABLE imports (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
plugintype VARCHAR(50) NOT NULL,
importedpluginkey VARCHAR(255) NOT NULL,
imported DATETIME NULL DEFAULT NULL,
plugin_id INT UNSIGNED NOT NULL,
FOREIGN KEY plugin_key (plugin_id) REFERENCES plugins(id)
);
Run Code Online (Sandbox Code Playgroud)
现在我烘焙了两个控制器,一切正常。我可以保存导入(稍后将用于导入 xml 文件)并将其链接到插件。
但现在我想让 Plugindata 通过我在导入中添加的数据进行更新。
例如,假设我有一个名为“sale”的插件,其类型为 Wordpress,键为“wp_sale”。现在我想通过添加导入来更新这个插件。例如更改其描述或名称。
如果我理解正确的话,我只需要在插件中传递我想要更改的数据以及 id 即可。
但是,当我检查将要保护的 patchEntity 时,插件实体始终为 '[new]'=> true,并且我传递的 id 消失了。
这就是我的控制器的样子:
public function add()
{
$import = $this->Imports->newEntity();
if ($this->request->is('post')) {
$import = $this->Imports->patchEntity($import, $this->request->data, ['associated' => ['Plugins']]);
if ($this->Imports->save($import)) {
$this->Flash->success(__('The import has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The import could not be saved. Please, try again.'));
}
}
$plugins = $this->Imports->Plugins->find('list', ['limit' => 200]);
$this->set(compact('import', 'plugins'));
$this->set('_serialize', ['import']);
}
Run Code Online (Sandbox Code Playgroud)
这就是我的表格的样子:
echo $this->Form->input('plugintype');
echo $this->Form->input('importedpluginkey');
echo $this->Form->input('imported');
echo $this->Form->input('plugin_id', ['options' => $plugins]);
echo $this->Form->input('plugin.pluginkey');
echo $this->Form->input('plugin.type');
echo $this->Form->input('plugin.id', ['value' => 1]);
Run Code Online (Sandbox Code Playgroud)
我将plugin.id 的值设置为1 只是为了测试,因为我想传递插件的id。
我的帖子数据如下所示:
[
'plugintype' => 'wordpress',
'importedpluginkey' => 'wp_sale',
'imported' => [
'year' => '2015',
'month' => '11',
'day' => '24',
'hour' => '10',
'minute' => '38'
],
'plugin_id' => '1',
'plugin' => [
'pluginkey' => 'wp_sale',
'type' => 'wordpress',
'id' => '1'
]
]
Run Code Online (Sandbox Code Playgroud)
修补后的实体如下所示:
object(App\Model\Entity\Import) {
'plugintype' => 'wordpress',
'importedpluginkey' => 'wp_sale',
'imported' => object(Cake\I18n\Time) {
'time' => '2015-11-24T11:04:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'plugin_id' => (int) 1,
'plugin' => object(App\Model\Entity\Plugin) {
'pluginkey' => 'wp_sale',
'type' => 'wordpress',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pluginkey' => true,
'type' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Plugins'
},
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'plugintype' => true,
'importedpluginkey' => true,
'imported' => true,
'plugin_id' => true,
'plugin' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Imports'
}
Run Code Online (Sandbox Code Playgroud)
该网站上的许多问题都与关联保存有关。但我想要的是在导入模型中添加新数据时更新关联数据。
其背后的原因是,我希望能够更新我的插件而无需编辑每个插件,而只需使用 xml 导入。
如果问题仍然不清楚,请随时提问,我会进一步解释。提前致谢!
编辑:要明确的是,我基本上需要使用导入控制器更改插件的数据。
我现在在另一个函数中获得了插件实体,我可以使用导入实体保存它。然而,它不会改变插件中的任何内容,所以基本上什么也不做。
The example in the linked docs seem to assume that the entities are set up in a way that they allow the primary key to be mass assigned, see
Cookbook > Database Access & ORM > Entities > Mass Assignment
By default, baked entities do not allow the primary key to be mass assigned, so you have to take care of that, for example by overriding the fields accessible state via the patchEntitity() options, like
$import = $this->Imports->patchEntity(
$import,
$this->request->data,
[
'associated' => [
'Plugins' => [
'accessibleFields' => ['id' => true]
]
]
]
);
Run Code Online (Sandbox Code Playgroud)
See also Cookbook > Database Access & ORM > Saving Data > Changing Accessible Fields
请注意,虽然实体仍将被标记为“新”,但保存过程将(只要该checkExisting选项未设置为false)测试是否存在具有给定主键的记录,并在必要时将实体标记为“非新”,然后再继续,这样您最终会得到更新而不是插入。
另请参见Cookbook > 数据库访问和 ORM > 保存数据 > 保存实体
| 归档时间: |
|
| 查看次数: |
3098 次 |
| 最近记录: |