gpt4 book ai didi

postgresql - CakePHP 3 : Migration doesn't save update data

转载 作者:行者123 更新时间:2023-11-29 11:51:45 25 4
gpt4 key购买 nike

环境:
CakePHP 3
邮政系统

我正在尝试进行迁移以添加新字段,然后更新我们 Postgres 数据库中该字段的一些数据。该实体似乎表明它已更新,但是当我查看数据库时,它并没有保存。

代码

<?php
use Cake\Cache\Cache;
use Cake\ORM\TableRegistry;
use Migrations\AbstractMigration;

class AddDisplayRouteNumberToAgencies extends AbstractMigration
{
/**
* Up Method.
*/
public function up()
{
$table = $this->table('agencies');
$table->addColumn('display_route_number', 'boolean', [
'default' => true,
'null' => false,
]);
$table->update();

// Try to clear the Model cache
Cache::clear(null, '_cake_model_');

$patchData = [
'display_route_number' => false
];

$agencies = TableRegistry::get('Agencies');

$agency = $agencies->get(25);
// And save it back to the DB
$agencies->patchEntity($agency, $patchData);
debug($agency);
// Added after comment from ndm
$agencies->save($agency);
}

/**
* Down method
*/
public function down()
{
$table = $this->table('agencies');
$table->removeColumn('display_route_number');
$table->update();

// Clear the CakePHP Model cache
Cache::clear(null, '_cake_model_');
}
}

debug()的结果

object(App\Model\Entity\Agency) {
'id' => (int) 25,
'full_name' => 'Agency',
'legacy_agency_slug' => null,
'created' => object(Cake\I18n\Time) {

'time' => '2015-11-19T10:58:51+0000',
'timezone' => 'UTC',
'fixedNowTime' => false

},
'modified' => object(Cake\I18n\Time) {

'time' => '2015-11-19T10:58:51+0000',
'timezone' => 'UTC',
'fixedNowTime' => false

},
'display_route_number' => false,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'display_route_number' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Agencies'
}

Postgres 查询和结果

SELECT id, display_route_number
FROM agencies
WHERE id = 25;

id | display_route_number
----+----------------------
25 | t
(1 row)

其他尝试

我也试过只使用 save() 而不是 patchEntities(),它返回相同的结果,除了 [dirty] 是空的.

    $agencies = TableRegistry::get('Agencies');
$agency = $agencies->get(25);
// Items to update
$agency->display_route_number = false;
// And save it back to the DB
$agencies->save($agency);

最佳答案

感谢@ndm 帮我解决了这个问题。我最终不得不更新表架构。我相信这是因为迁移表部分更新了 SQL,但没有更新架构。

这是最终代码:

    // Clear the CakePHP Model cache
Cache::clear(null, '_cake_model_');

$table = $this->table('agencies');
$table->addColumn('display_route_number', 'boolean', [
'default' => true,
'null' => false,
]);
$table->update();

$agencies = TableRegistry::get('Agencies');

// REQUIRED!! Add the field to the Table schema
$agencies->schema()->addColumn('display_route_number', [
'type' => 'boolean',
'default' => true,
'null' => false
]);

$agency = $agencies->find()
->where(['short_name' => 'bart'])
->first();
// Items to update
$agency->display_route_number = false;
// And save it back to the DB
$agencies->save($agency);

关于postgresql - CakePHP 3 : Migration doesn't save update data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com