How to make sure that the link is created, how it correctly to organize?

The documentation says:

$customer = Customer::findOne(123);
$order = new Order();
/// ....
$order->save();
$order->link('customer', $customer);


$order->save(); to check what had worked not hard enough to wrap in an if
and here is how to make sure that $order->link('customer', $customer); have worked?
And how to make these two requests are fulfilled, and not one of them?
Transaction?
June 10th 19 at 15:22
2 answers
June 10th 19 at 15:24
how to make sure that $order->link('customer', $customer); have worked?

If link cannot link for some reason two models it generates an exception. Accordingly, if no exception - the method worked
And how to make these two requests are fulfilled, and not one of them?

Of transaction...
how to make sure not to duplicate communication? - tad_Cartwrig commented on June 10th 19 at 15:27
to write a unique validator and add to the unique database key - Henderson_Beat commented on June 10th 19 at 15:30
maybe , but it's not a flexible solution, let's say I have converted the contents before adding to the database, so the uniqueness question. - tad_Cartwrig commented on June 10th 19 at 15:33
let's say I have converted the contents before adding to the database

convert in beforeValidate - Henderson_Beat commented on June 10th 19 at 15:36
June 10th 19 at 15:26
If one of the models has been (a customer in your example), then explicitly order you not to save - link will do it automatically.
ie $order->save(); unnecessary? - tad_Cartwrig commented on June 10th 19 at 15:29
as you have to post redundant. But if both models will be new, it will throw an exception https://github.com/yiisoft/yii2/blob/master/framew... - Henderson_Beat commented on June 10th 19 at 15:32
and I have one new and still the exception is popping up
"Unable to link models: the models being linked cannot be newly created."


$user = User::findOne(Yii::$app->user->id);

 $truck = new Truck();
 $truck->load(Yii::$app->getRequest()->getBodyParams(), ");

 $truck->link('users', $user);


ie he thinks otherwise, for your reference:
if ($this->getIsNewRecord() && $model->getIsNewRecord()) {
 throw new InvalidCallException('Unable to link models: at most one model can be newly created.');
 }
- tad_Cartwrig commented on June 10th 19 at 15:35
You have a connection via ref-table built? Example-the other :)
If so, it is necessary both models to keep. In the method just above the corresponding check - Henderson_Beat commented on June 10th 19 at 15:38

Find more questions by tags Yii