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

Relationships between Models

Hi all

The model "users" hasmany "posts".

The model "posts" belongTo "users".

 class Users extends \Phalcon\Mvc\Model{
    public $id;

    public $name;
    ---------------
    public function initialize(){
      $this->hasMany('id', 'Phalconvn\Models\Posts', 'userId', array(
            'alias' => 'posts',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because article has not for '
            )
        ));
    }

}

class Posts extends \Phalcon\Mvc\Model{
   public $id;
   public $userId;
------------
   public function initialize(){
        $this->belongsTo('userId','Phalconvn\Models\Users','id',array(
            'alias' => 'userPost'
        ));

}
}

When login users with id does not ownership id by posts, but delete post not ownership it is wordking, why is that

Can you please explain the problem in more detail? I don't understand what you're trying to say with your last sentence.

Also, you don't need to declare class variables (such as $id, $name, and $userId). Phalcon takes care of that automatically based on the structure of the database.



58.4k

@quasipickle Thank you for your soon answer.

How to prevent delete article doesn't own by author(user) via Relationships between Models

How to prevent deletion of articles by those who are not the owner as enforced by a relationship constraint? I think that is a bit out of scope as the database doesn't really know anything about your application's users--you have to enforce the constraint in your application's code.

Maybe I interpreted your comment incorrectly though.