I have a related has-many table. And there is a record needed to be created for any new record for the main table. As I read, this is the transaction-like way:
<?php
$acc = new Account();
$acc->login = 'blablabla';
$data = new Data();
$data->var = 'somevar';
$data->value = someval'';
$acc->data = [$data]; // php 5.4 array syntax
$acc->create();
and it works.
But why does don't work this?
<?php
class Account extends Phalcon\Mvc\Model
{
// ...
public function beforeCreate()
// public function beforeValidationOnCreate() // -- also have been tried
{
$data = new Data();
$data->var = 'somevar';
$data->value = someval'';
$this->data = [$data]; // php 5.4 array syntax
}
There is just nothing saved to the 'data' table. Logically I expected it would work like the fist one.
There just must always be a such record creation for any account creation and It has to be saved in transaction-like way for safety. Because the data is calculated from an account's fields, it is much easier to create an account like this "$account->create($_POST)" and data would be created automatically.
Seems like I just doing something wrong, because the second way is really very useful and simple to has the right to exist.