Cakephp 3,更新关联的所属数据

标签 cakephp associations cakephp-3.0 belongs-to has-one

所以我有 2 个模型:插件和导入。 该关联是 Plugins hasOne Imports:

$this->hasOne('Imports', [
    'foreignKey' => 'plugin_id'
]);

并导入属于插件:

$this->belongsTo('Plugins', [
    'foreignKey' => 'plugin_id',
    'joinType' => 'INNER'
]);

我的插件表转储如下所示:

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;

我的导入表是:

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

现在我烘焙了两个 Controller ,一切正常。我可以保存导入(稍后将用于导入 xml 文件)并将其链接到插件。

但现在我想让 Plugindata 通过我在导入中添加的数据进行更新。

例如,假设我有一个名为“sale”的插件,其类型为 Wordpress,键为“wp_sale”。现在我想通过添加导入来更新这个插件。例如更改其描述或名称。

如果我明白this是的,我只需要在插件中传递我想要更改的数据以及 id 即可。

但是当我检查将要保护的 patchEntity 时,插件实体始终为 '[new]'=> true,并且我传递的 id 消失了。

这就是我的 Controller 的样子:

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

这就是我的表单的样子:

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

为了测试,我给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'
    ]
]

修补后的实体如下所示:

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'

}

此网站上的许多问题都与关联保存有关。但我想要的是在导入模型中添加新数据时更新关联数据。

其背后的原因是,我希望能够更新我的插件而无需编辑每个插件,而只需使用 xml 导入。

如果问题仍然不清楚,请随时提问,我会进一步解释。提前致谢!

编辑:明确地说,我基本上需要使用导入 Controller 更改插件的数据。

我现在在另一个函数中获得了插件实体,我可以使用导入实体保存它。但是,它不会改变插件中的任何内容,因此基本上什么也不做。

最佳答案

链接文档中的示例似乎假设实体的设置方式允许批量分配主键,请参阅

<强> Cookbook > Database Access & ORM > Entities > Mass Assignment

主键需要可访问

默认情况下,烘焙实体不允许批量分配主键,因此您必须注意这一点,例如通过 patchEntitity() 选项覆盖字段可访问状态,喜欢

$import = $this->Imports->patchEntity(
    $import,
    $this->request->data,
    [
        'associated' => [
            'Plugins' => [
                'accessibleFields' => ['id' => true]
            ]
        ]
    ]
);

另请参阅 Cookbook > Database Access & ORM > Saving Data > Changing Accessible Fields

请注意,虽然实体仍将被标记为"new",但保存过程将(只要 checkExisting 选项未设置为 false)进行测试具有给定主键的记录是否存在,并在必要时将实体标记为“非新”,然后再继续,以便您最终得到更新而不是插入。

另请参阅 Cookbook > Database Access & ORM > Saving Data > Saving Entities

关于Cakephp 3,更新关联的所属数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892071/

相关文章:

php - cakePHP 3 在模型中使用请求/授权数据的最佳实践(基于帐户的云服务)

mysql - 调试套件显示重复的查询

CakePHP 3 $this->模型->有条件获取?

php - 如何获取 Cakephp 的完整当前 url

php - 无法在 CakePHP 应用程序中存储 01/01/1600 之前的日期

ruby-on-rails - 选择类别 : Rails 4 many to many association

ruby-on-rails - 外键 : belongs_to & has_one difference

php - CakePHP: Controller 或模型中的查询?

php - CSV 导出,包括其他表中的行

ruby-on-rails - 努力理解模型和关联是如何保存在 Rails 中的