serghei
serghei

Reputation: 3381

Phalcon ORM isNew

Does Phalcon's ORM have something like isNew?

Currently I'm use:

{% if user.getID() %}
    {{ 'Edit user ' ~ user.name }}
{% else %}
    {{ 'New user' }}
{% endif %}

but not sure if this is the right way.

Phalcon 1.3.x please. Thanks

Upvotes: 0

Views: 193

Answers (3)

challet
challet

Reputation: 972

The method getId seems not available in the last version : https://docs.phalconphp.com/3.4/en/api/Phalcon_Mvc_Model

It could use the "dirty state" instead :

public function isNew() {
    return $this->getDirtyState() == self::DIRTY_STATE_TRANSIENT;
}

Upvotes: 1

Ty Kroll
Ty Kroll

Reputation: 1395

I use this type of pattern on all my models and use isNew() in the same way you do.

class SomeModel extends Phalcon\Mvc\Collection {

    public static function getNew() {

        $t = new self();
        $t->someFieldToInit = 'some-val';
        return $t;
    }

    public static function findByIdOrNew($_id) {

        $t = self::findById($_id);
        return $t ?: self::getNew();
    }

    public function isNew() {

        return !$this->getId();
    }
}

Upvotes: 2

Thinh VoXuan
Thinh VoXuan

Reputation: 1

What isNew mean in your situation? If it checked anyone has just register into your site. Maybe we could use authentication to get that User

Upvotes: -1

Related Questions