I looking-for some idea solutions for creating behavior for revisions/historic edit entities.
I prepare some behavior for this. It’s works on CakePHP 3, based on behavior beforeSave
callback.
If some field changed, then created new record (with fill parent_id
field) instead edit/update existing.
This is very simple solutions and it is satisfactory to me. I show the part of behavior code below:
public function beforeSave(Event $event, EntityInterface $entity)
{
if (!$entity->isNew()) {
if ($this->_table->hasField('id')) {
$entity->isNew(true);
// Set primary key to parent_id field
$entity->parent_id = $entity->id;
// Remove primary key
$entity->unsetProperty('id');
}
}
}
But what with relations?
I have relations (both has attached this behaviors):
Products hasMany ProductParams
ProductParams belongsTo Products
Problem #1:
Products (id = 1, parent_id = null, ...
) has ProductParams (id = 100, parent_id = null, product_id = 1, ...
)
Now, I edit Products and will create new Products (id = 2, parent_id = 1, ...
) and this is okey, but when i show Products.id = 2
it will not have a relationship with ProductParams.
Idea: I think the good will be use something like trigger to create new record of ProductParams with update field product_id
and set to 2
, but how do this?
Problem #2:
ProductsParams (id = 100, ..., product_id = 1
) belongs to Product (id = 1, ...
)
Now I edit ProductParams and will create new ProductParams (id = 101, parent_id = 100, ...
), but when I show Products.id = 1
it will has ProductParams.id = 101
, but oryginaly should be display 100
. The perfect solution of this wil be great to also create new record of Product.
This is question is only for talking about ideas:)