We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Resultset as many to many relation value issue

I can't understand the behaviour of my code:

            $modules = Modules::findByDatatype($feed->datatype);
                if (count($modules)) {
                    $time = time();
                    $model = new Contents();
                    $model->modules = $modules;
                    $model->name = $feed->name . ' ' . date('d/m/Y H:i:s', $time);
                    $model->type = $feed->datatype;
                    $model->tags = $feed->tags;

                    if ($model->save()) {
                        echo 'Contenuto (' . $model->id . ') Correttamente Creato' . PHP_EOL;
                    } else {
                        echo 'ERRORE durante la creazione del Contenuto' . PHP_EOL;
                        print_r($model->getMessages());
                    }
                }

the new model was saved correctly but without relations.

If I replace the resultset with a new array, as below, it works correctly.

            $modules = Modules::findByDatatype($feed->datatype);
                $modulesList = [];
                foreach ($modules as $module) {
                    $modulesList[] = $module;
                }

                if (count($modules)) {
                    $time = time();
                    $model = new Contents();
                    $model->modules = $modulesList;
                    $model->name = $feed->name . ' ' . date('d/m/Y H:i:s', $time);
                    $model->type = $feed->datatype;
                    $model->tags = $feed->tags;

                    if ($model->save()) {
                        echo 'Contenuto (' . $model->id . ') Correttamente Creato' . PHP_EOL;
                    } else {
                        echo 'ERRORE durante la creazione del Contenuto' . PHP_EOL;
                        print_r($model->getMessages());
                    }
                }

Why??? A resultset is an iterable object!!! I can't use it in a relation?!?

PS I use this code in a cli Task.

edited Apr '18

I guess Phalcon just needs an array.

You can make your job a bit easier by hydrating your find() as an array, so you don't have to manually convert it to an array:

$modules = Modules::findByDataType($feed->datatype,['hydration'=>\Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS]);

Also, as an fyi, if you follow the 3 backticks with "php", you'll get nice syntax highlighting. I've updated your post to use this method.

edited Apr '18

I tried this way yesterday but this is the opposite of what I need. Your code result is a Resultset of arrays, but I need an array of Modules.

My current solution is to apply an empty filter to the result set to get a series of models:

$modules = Modules::findByDatatype($feed->datatype)->filter(function($e){return $e;});

However I believe that accepting a resultset as an iterable object of a many-to-many relationship is desirable!!!