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.