baum
baum copied to clipboard
Issue with custom primary keys
When using a custom primary key in a model
protected $primaryKey = 'reference';
and the following migration
$table->string('reference', 20)->primary();
$table->string('parent_id', 20)->nullable();
$table->integer('lft')->nullable();
$table->integer('rgt')->nullable();
$table->integer('depth')->nullable();
The following:
$things = [
['reference' => 'One'],
[
'reference' => 'Two',
'children' => [
['reference' => 'TwoDotOne'],
['reference' => 'TwoDotTwo']
]
],
['reference' => 'Three']
];
App\Thing::buildTree($things);
results in a "No query results for model" Error, triggered by the $self->reload()
in Node@setDepth.
I guess this is because we're reloading before the new model is persisted?
You're right about the call to reload, but I'm not sure why this only affects models with a custom primary key.
Could you write a failing unit test + migration to demonstrate the issue please? If you fork the repo and make a pull request, I can then take a look. Thanks.
I'm having similar problem with similar migration.
When I want to save a model that value of custom primary key is float number, then results in a "No query results for model" Error, triggered by the $self->reload() in Node@setDepth, but model is saved in database.
When the value of custom primary key has at least one non-numeric character, then it works well.
Hey, I found the solution.
try setting $incrementing = false in your model.
That worked for me.