bros. Got a strange behaviour. I'm writing tests for my Laravel 4 ClosureTable package.
All tests create new Entity
(or Entitites
) for further use. For example, one of the first tests: ClosureTableTestCase::testInsertSingle
. When I run it, I got this:
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', ))
And var_dump
says me the following:
array(1) { [0] => array(6) { 'ancestor' => string(1) "1" [0] => string(1) "1" 'descendant' => string(1) "1" [1] => string(1) "1" 'depth' => string(1) "0" [2] => string(1) "0" } }
However when I create an Entity via web interface, all's fine!
Errors come from DB::select($selectQuery);
call when I run tests via terminal. As you can see, I somehow got [0], [1], and [2] extra array keys. However when I create an Entity via web interface, all's fine!
Error is in this method that's run internally:
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);
});
}
Do you can point me out a mistake?
Hmm, seems like fetch setting from app/config/database.php is PDO::FETCH_BOTH
, if is not that.. you can try to force the settings to get PDO::FETCH_ASSOC
before DB::select
with:
Config::set('database.fetch', PDO::FETCH_ASSOC);