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

_postSaveRelated record error when saving related model.

Using magic property to store related records has a problem when the related record fails to vaidate, it out put _postSaveRelalated bla bla error.

So the doc example below,

$artist = new Artists();
$artist->country = 'Japan';

$album = new Albums();
$album->name = 'The One';
$album->artist = $artist; //Assign the artist
$album->year = 2008;

//Save both records
$album->save();

If the Artist validation fails, the save operation in album model would produce

Fatal error: Phalcon\Mvc\Model::_postSaveRelatedRecords(): Call to method setmodel() on a non object in ....

My question is how to store related record like the example docs without producing this fatal error



368

What version of phalcon you have installd ? Thats fixed in 1.0.0

0.9.1, I have to stick to 0.9.1 because skipAttribute does not work in 1.x.x

I've tried version 1.1.0 and the fatal error still exist. it happened when all model is not descended directly from Phalcon\MVC\Model but from my ModelBase class. Below is my base model class.

namespace BI\Models;

class ModelBase extends \Phalcon\MVC\Model {
    protected $fieldLabel = array();

    public function setValues($data, $keys){
        foreach($keys as $key=>$default){
            $val = isset($data[$key]) ? $data[$key] : $default;
            $this->{$key} = $data[$key];
        }
    }

    public function loadMsgs(&$msgs){
        $listmsg = $this->getMessages();
        foreach ($listmsg as $message) {
            $msgs[] = $message;
        }
        return count($listmsg)==0;
    }

    public function valid(&$errors){
        if(empty($errors))
            $errors = array();
        if(!$this->validation()){ 
            $this->loadMsgs($errors);
            return false;
        }
        return true;      
    }

    public function validation(){
        return true;
    }

    public function getMessages(){
        $messages = array();
        if(count(parent::getMessages()))
            foreach (parent::getMessages() as $message) {
                $field = isset($this->fieldLabel[$message->getField()])?
                    $this->fieldLabel[$message->getField()] : $message->getField(); 
                switch ($message->getType()) {
                    case 'InvalidCreateAttempt':
                        $messages[] = 'Data tidak dapat disimpan karena sudah ada.';
                        break;
                    case 'InvalidUpdateAttempt':
                        $messages[] = 'Data tidak dapat disimpan karena sudah ada.';
                        break;
                    case 'ConstraintViolation' :
                        $messages[] =  $field . ' yang Anda pilih tidak ada.';
                        break;          
                    case 'PresenceOf':
                        $messages[] = 'Kolom '. $field . ' wajib diisi.';
                        break;
                    case 'Email' : 
                        $messages[] = $field . " tidak valid.";
                        break;
                    case 'Unique':
                        $messages[] = $field . " yang Anda masukkan sudah terpakai. ".
                            " Silakan masukkan ".$field." yang lain.";
                        break;  
                    default:
                        $messages[] = /* $message->getType() . '::' .*/ $message;               
                }
            }
        return $messages;
    }
}

I reproduce the error in the github Issue #584,