[solved: kind of ] Related records disappearing

Hi all,

I've got a model, Transaction, that has hasMany relationships with an Extra, and an Item model.

My controller is creating a list of new Extra models and Item models to be associated with a new Transaction model - essentially it's converting $_POST data into models.

In my controller I have this code:

    $extra = [];
    foreach($this->request->getPost('extra') as $name=>$value){
        $Extra         = new \Pay\Model\Extra();
        $Extra->name   = $name;
        $Extra->value  = $value;

        $extra[] = $Extra;
    $Transaction->extra = $extra;

    $items = [];
    foreach($this->request->getPost('items') as $key=>$item){
        $Item              = new \Pay\Model\Item();
        $Item->name        = (isset($item['name']))         ? $item['name']         : '';
        $Item->price       = (isset($item['price']))        ? $item['price']        : '';
        $Item->tax         = (isset($item['tax']))          ? $item['tax']          : '';
        $Item->count       = (isset($item['count']))        ? $item['count']        : '';
        $Item->speedcode   = (isset($item['speedcode']))    ? $item['speedcode']    : NULL;
        $Item->accountcode = (isset($item['accountcode']))  ? $item['accountcode']  : NULL;

        $items[] = $Item;
    $Transaction->items = $items;

As you can see, the process for populating $Transaction->extra and $Transaction->items is pretty much identical.

However, I have 2 problems: 1) When doing my validation (not with \Phalcon validation, but my own code), $Transaction->items is set, but calling $Transaction->countItems() returns 0. Therefore I can't loop through $Transaction->items to see if they're all valid. For all intents and purposes, $Transaction->items is essentially not set.

If I put calls to count() in:

echo count($items);
$Transaction->items = $items;
echo count($Transaction->items);

I get "20" output - "2" for the count of $items, and 0 for the count $Transaction->items.

What the hell is happening to $items?

2) While $Transaction->extra is exhibiting all the same problems, the models do get saved in the database properly. Why, when the Transaction->Extra relationship is exactly the same as the Transaction->Item relationship, does one get saved but not the other?

Also, I'm using v1.3.0

After sleeping on the problem, and doing more research, I realize I can't set related models (Extra, Item) without creating the main model (Transaction) first. Once Transaction is create()d, the related models work fine. This highlights 2 problems:

1) Why must the behaviour be this way? When creating, the Transaction model would retrieve the database's ID number for the transaction. That ID number can then be applied to all the related models before they are set. I don't think it should be a requirement that the main model be saved first.

2) The documentation says nothing to this effect. Yes, the example shows the main model being retrieved from the database, but there's nothing saying you can't add related models to a brand new main model.


I am facing this problem too. I have a main object, with some related objects, and when I want to perform a custom validation for the main object I cannot retrieve related objects.

As mentioned by @quasipickle, objects don't have any ID while not saved in database, but Phalcon manage correctly the saving order of all entities http://docs.phalcon.io/en/latest/reference/models.html#storing-related-records . It means that the framework keep an internal reference to these unsaved objects, and we cannot access to these references...

An other remark, there is a useful function Phalcon\Mvc\Model::getRealted function to avoid magic getters (http://docs.phalcon.io/en/latest/reference/models.html#magic-getters-vs-explicit-methods) but there is no corresponding setter (something like setRelated(...))


Just for information, I found the 'secret' array '_related' in my model instance, storing all related data to save before. IMHO, we should not use this array... but I will, waiting an official 'get' function for them.

Does this have an official method under Phalcon version 2.0.3?