Closed paulchubatyy closed 14 years ago
Basically calling save leads to the data integrity corruption. The parent is saved with id 0 for the first time, but afterwards other parent's cannot be saved. Though the parent id should be as passed to set method.
I'm struggling to understand your problem here.
I think you have this right but for clarity: an ID in a foreign key should never be set to 0 that is bad practice. At the top level of the hierarchy (or anywhere alse a relation doesn't exist) the foreign key value should be NULL. I think that is what you are trying but it is a little unclear from your explanation exactly where you think the problem is occuring.
Are you saying the read from the DB is wrong? Or is the problem when setting the relationship up? or is it when saving something?
The problem is when saving category with the parent. Follow this logic:
I've created the category A with no parent. Everything works fine. I've creating another category B and setting the parent A. Before saving the category B the result is as shown here:
array(9) (
"id" => NULL
"title" => string(4) "test"
"level" => NULL
"materialized_path" => NULL
"parent" => object Model_Category(9) {
protected _original => array(9) (
"id" => NULL
"title" => string(8) "Bamboola"
"level" => integer 1
"materialized_path" => string(8) "Bamboola"
"parent" => NULL
"children" => NULL
"created_at" => integer 1271169097
"updated_at" => integer 1271169097
)
..... and so on
After saving the category A is updated (!!!!) and it's Id is set to 0.
I hope I was more clear this time.
OK, thanks for that. It looks like the problem is in the set()
logic. Can I just confirm how you were setting the parent of B?
Options are:
//1
$b->parent = $a; // Set an instance
//2
$b->parent = 1; // Set by key
Option 2: Set the key.
Since parent is acting as a foreign key field, shouldn't it be a BelongsTo?
'parent' => new Field_BelongsTo(array(
'foreign' => 'category.id',
'column' => 'parent_id',
)),
Tried that one either. Didn't work for me.
Parent needs to be BelongsTo I didn't spot that. HasOne and HasMany won't work as there is no foreign key between them! Hasmany always relates to belongsTo and HasOne also always relates to BelongsTo.
If the belongsto change doesn't fix it there is probably still an issue in set() but you won't get far with a HasOne field!
I've changed the model to use BelongsTo. This shouldn't affect the logic setting the parent, right? I'll test with BelongsTo and post my results in a while.
Didn't solved the issue. Seems there is really a bug in set method.
Sorry guys, that's not your bug. I've figured out the bug in my code. Sorry for taking your time.
Hey,
I was trying to implement the Hierarchical model with Jelly, but I've got the issue, when parent record didn't have the ID loaded.
Here is the model:
}
So I'm posting:
Please note that category with id 1 is already in the database. So I set model values procedure:
but than I get the following when echo Kohana::debug($this->entity->as_array()):
Please note that parent id is NULL while it is present in the database and was set to 1.
Any ideas how can that be fixed?