我在使用 Phinx(版本 0.10.8)时遇到了一个特殊问题:我必须迁移一个表,以便将该行插入到一个表中,然后将 AUTO_INCREMENTed last-insert-ID 插入到另一个表中。
由于我处于 for 循环中,因此我只想继续为第一个表中的插入回收相同的插入查询构建器;而不是重建整个插入构建器。但我不知道如何重置VALUES
数据。
一个例子来说明这个问题:
// insert-builder I hope to re-use.
$builder = $this->getQueryBuilder()->insert(array(
'note',
))->into('test_table');
// cache this empty state for reset attempt #2.
$empty = $builder;
// insert one row of values.
$builder->values(array(
'note' => "Please don't copy me. Please don't copy me. Please don't copy me ...",
))->execute();
// dump info.
var_dump($this->getAdapter()->getConnection()->lastInsertId());
$rows = $this->fetchAll("SELECT COUNT(*) FROM test_table");
var_dump($rows);
// reset attempts.
//$builder->getValueBinder()->reset(); // (1)
$builder = $empty; // (2)
//$builder->getQuery()->getValueBinder()->reset(); // (3)
// insert second row.
$builder->values(array(
'note' => "Second insert.",
))->execute();
// dump info.
var_dump($this->getAdapter()->getConnection()->lastInsertId());
$rows = $this->fetchAll("SELECT COUNT(*) FROM test_table");
var_dump($rows);
数字 (3) 给了我一个例外,并且 (1) 和 (2) 给了我相同的输出,即在 2 次插入后我有 3 行:
string(1) "1"
array(1) {
[0]=>
array(2) {
["COUNT(*)"]=>
string(1) "1"
[0]=>
string(1) "1"
}
}
string(1) "2"
array(1) {
[0]=>
array(2) {
["COUNT(*)"]=>
string(1) "3"
[0]=>
string(1) "3"
}
}
反正我是在黑暗中钓鱼。我真的找不到任何好的文档。
/vendor/cakephp/database/ValueBinder.php
似乎确实有一个公共重置方法。但我不确定如何到达那个 ValueBinder。
这个线程建议使用闭包,现在我考虑一下这实际上是一个好主意。在本文档中顺便提到了它们。但它是如何工作的?我哑了。
// not like this.
$values = array(
'note' => "Please don't copy me. Please don't copy me. Please don't copy me ...",
);
$this->execute(function() use ($builder, $values) {
return $builder->values($values)->execute();
});
// not like this.
$this->execute(function($builder) use ($values) {
return $builder->values($values)->sql();
});
// not like this.
$builder->values(function($builder) use ($values) {
return $builder->values($values);
})->execute();