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

How to define has one or zero relationship between two models

class Users extends \Phalcon\Mvc\Model {
  public function initialize() {}
    //has one or zero
    $this->hasOne('id', 'UserPictures', 'users_id', ['alias' => 'picture']);
  }
}

class UserPictures extends \Phalcon\Mvc\Model {
  public function initialize() {
    $this->hasOne('users_id', 'Users', 'id', ['alias' => 'user']);
  }
}

controller action:


$user = Users::findFirst();
$picture = $user->picture;
$this->view->picture = $picture;

in volt template:

{%if picture is defined%}
.
.
.
{%endif%}


34.6k
Accepted
answer

Just evaluate if the variable is false is enough:

{% if picture %}