gpt4 book ai didi

mysql - 有没有办法插入没有数据的行

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

使用 CakePHP 3.3,我有一个仅包含主键的表。这用作序列。在 CakePHP 中,我插入一行没有数据的行,它会生成一个新行。

蛋糕2.7

class Identity extends AppModel {

function nextVal() {
$data = [];
$this->create();
$this->save($data);
return $this->id;
}
}

我试图在 CakePHP 3.3 中复制此行为,但它没有达到我的预期。

CakePHP 3.3

class IdentityTable extends Table
{
public function generate() {
$identity = $this->newEntity();
if ( $this->save($identity) ) {
// The $ccn entity contains the id now
\Cake\Log\Log::debug(__METHOD__. ' success');
return $identity->id;
}
\Cake\Log\Log::debug(__METHOD__. ' failed');
return false;
}
//
public function initialize(array $config)
{
parent::initialize($config);

$this->table('identity');
$this->displayField('identityId');
$this->primaryKey('identityId');
}
}

MySQL 对此非常满意:

INSERT INTO `identity` () VALUES()

我认为 CakePHP 3.x ORM 发现我没有插入任何内容,并且它会在保存时放弃。

CakePHP 3 中有没有办法插入没有数据的行?

最佳答案

你必须告诉 cake 实体中出现了某些问题,并将其标记为“脏”。即

$identity ->dirty('id', true);

因此查询结果将是

INSERT INTO sic_suppliers (id) VALUES (NULL)

我想这对你来说没问题

PS不知道这是否是实现这一目标的最佳方法,但它似乎有效。

编辑:按照@ndm建议,您也可以直接插入记录(参见cookbook)

$this->query()->insert(['id'])
->values([null])
->execute();

关于mysql - 有没有办法插入没有数据的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987723/

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