-
Couldn't load subscription status.
- Fork 47
Description
class Order extends \Atk4\Data\Model {
protected function init(): void
{
parent::init();
$this->hasMany('order_doc', ['model' => [OrderDoc::class
]]);
}
}
class OrderDoc extends \Atk4\Data\Model {
protected function init(): void
{
parent::init();
$this->hasOne('order_id', ['model' => [Order::class]]);
$this->onHook(\Atk4\Data\Model::HOOK_BEFORE_INSERT, function ($model, &$data) {
// Create order if not existing
if (!$model->get('order_id') && $model->get('customer_id')) {
$model->set('order_id', $model->refLink('order_id')->insert([...]);
])); }
});
}
}
// Use case 1: Create a parent order container, when a new child order doc is created
$newOrderDoc = (new OrderDoc($db))->createEntity();
$newOrderDoc->save([...]);
// Use case 2: Assign an order document to another order
$order = (new Order($db))->load(1);
$orderdoc = $order->ref('order_doc')->load(1);
$orderdoc->saveAndUnload(['order_id' => 2]);
When traversing on null value, we may allow the ID to change (to newly inserted). I will check what can be done and if it will not break other tests.
use case 2 - how should this work? Previously, the save conditions were relaxed, to me, incorrectly. What is the reasoning to relax save conditions when the entity cannot be loaded after?
A solution I can think of would be to implement Model::getModelWithoutRefConditions(). Currently, we do not store traversed reference info, so in $orderdoc, we have no info which condition to remove. Let's discuss this feature request later. If you have (or can put) your project into Github, I can code some helper methods specific to your projects.