How related records are saving ?

If I have 2 models Str and StrTranslations, where StrTranslations belongsTo Str. And if I i add new parent record (Str) with StrTranslations encapsulated as related Like

$myStr = new Str();
//$myStr->id = null; //want new ID from DB
$_related[0] = new StrTranslations();
//$_related->strid = null; //want to use $myStr->id assigned after DB INSERT
$_related[0]->value = "blabla";
$myStr->StrTranslations = $_related;

As you could notice i wanna to have myStr->id to be assigned by by DB insert, not by PHP code and after that wanna to reuse same id in related myStr->StrTranslations

Diggin the Mozel.zep it looks like first Phalcon saves related records ($myStr->StrTranslations), not parent record ($myStr) by calling preSaveRelatedRecords() then saves parent record by calling _doLowInsert()/doLowUpdate() then re-saves related records _postSaveRelatedRecords() So related records actually are saved twice with same parent->save() call.

am I right ? It is not clear to me why related are saved first if anyway all saving happens withing a transation automatically ?

First you have to save Str so that the database assign an autoincrement id for the new record and then get the id to store it to StrTranslations.

$myStr = new Str();

//set data to $myStr

$myStr->loadFromJson($data); // I have made this function for load data from json..


$_related = new StrTranslations();

$related->idstr = $myStr->id; // this will assign the newly added record id to the record for $_related.