兄弟。有一个奇怪的行为。我正在为我的 Laravel 4 ClosureTable包编写测试。
所有测试都会创建新的Entity
(或Entitites
)以供进一步使用。例如,第一个测试之一:ClosureTableTestCase::testInsertSingle
. 当我运行它时,我得到了这个:
Exception: SQLSTATE[HY000]: General error: 1 table pages_closure has no column named 0 (SQL: insert into "pages_closure" ("ancestor", "depth", "descendant", "0", "1", "2") values (?, ?, ?, ?, ?, ?)) (Bindings: array (
0 => '1',
1 => '0',
2 => '1',
3 => '1',
4 => '1',
5 => '0',
))
并var_dump
告诉我以下内容:
数组(1){ [0] => 数组(6){ '祖先' => 字符串(1)“1” [0] => 字符串(1)“1” '后裔' => 字符串(1)“1” [1] => 字符串(1)“1” '深度' => 字符串(1)“0” [2] => 字符串(1)“0” } }
但是,当我通过 Web 界面创建实体时,一切都很好!
DB::select($selectQuery);
当我通过终端运行测试时,错误来自调用。如您所见,我以某种方式获得了 [0]、[1] 和 [2] 额外的数组键。但是,当我通过 Web 界面创建实体时,一切都很好!
错误出现在内部运行的此方法中:
protected function performInsertNode($descendant, $ancestor)
{
$table = $this->closure;
$ak = static::ANCESTOR;
$dk = static::DESCENDANT;
$dpk = static::DEPTH;
DB::transaction(function() use($table, $ak, $dk, $dpk, $descendant, $ancestor){
$selectQuery = "
SELECT tbl.{$ak} as {$ak}, {$descendant} as {$dk}, tbl.{$dpk}+1 as {$dpk}
FROM {$table} AS tbl
WHERE tbl.{$dk} = {$ancestor}
UNION ALL
SELECT {$descendant}, {$descendant}, 0
";
$results = DB::select($selectQuery);
array_walk($results, function(&$item){ $item = (array)$item; });
var_dump($results);
DB::table($table)->insert($results);
});
}
你能指出我的错误吗?