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

Finding a way to know if a Phalcon\Mvc\Model instance is new or not

Hello Phalcon,

I'm looking for a method to know from a model instance if the model is new or not.

Fo me a new model is:

$car = new Car(); \\this is a new car.
if ($car->save() == true) {
    \\this is not a new car, because i saved it
} else {
    \\this is already a new car, becasue i dont save it successfully
}

$car = Car::findFirst(1); \\and this is a not new car.

I'm looking for a method or something similar to this:

$car->isNew() : true | false

And $car is new until i dont save it successfully on the database.



125.8k
Accepted
answer

If your model has an ID you can check that.

You could also have a base model that all other models extend: That base model would have afterFetch() and afterSave() methods that could set a $new flag. Your isNew() method could then check the status of $new

edited Oct '15

First thing, $car->save() will return true if the validation has passed, not if a row was inserted. There are two other methods: create() and update().

Also, the ORM only handles PRIMARY keys, so for eg UNIQUE keys will return an exception triggered by the PDO layer, not the ORM. You'll have to manually set up the proper validation with Uniqueness https://docs.phalcon.io/en/latest/reference/models.html#validating-data-integrity

As for your question, there is no such method, exactly because these arbitrary parameters. You'll have to manually check with count for eg. Or create a base Model with the required functionality and inherit your instances from there.

One lame trick could be to check if the primary property is set, like

/** @var $model SomeModel */
if($model->getId()>0) {
    // exists
} else {
    // no primary key yet, probably just an instance without db record
}
edited Oct '15

Thank you for the responses...

@quasipickle i already use a base Model extended from my models and a own method to check the identity field:

abstract class ModelBase extends Model
{
    /**
     * @return string
     * @throws Exception
     */
    public function getIdentityField()
    {
        $metaData = $this->getModelsMetaData();
        $identityField = $metaData->getIdentityField($this);
        ...
        return $identityField;
    }
    public function isNew() {
        $identity = $this->getIdentityField();
        return empty($this->{$identity});
    }
}

But with the update to Phalcon 2 i found a new beahviour during the save of the model:


$car = new Car(); //id, name
$car->name = 'pippo';

$color = new Color(); //id, name
$color->name = null;

$car->Color = $color;

if ($car->save()) {
    \\inserted
} else {
    \\some error
}

Imagine that the model car pass the validation, but the model color dont pass it because the name is null.

But $car now has the ID valorized, so my method isNew() now return an incorrect value: false instead of true. This is my trouble!

Hello,

with this method i think that i solved my problem. Thank you

/**
     * @return bool
     */
    public function isNew() {

        return $this->isNew;
    }

    protected function afterSave() {
        $this->isNew = false;
    }

    protected function afterFetch() {
        $this->isNew = false;
    }