Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Filter data from table based on user login

I am very new to phalcon and OOP programming in general. I am trying build a simple application using the vokuro example as a starting point. I only need to add a few tables. I need to limit output of a dataset to just the user that is logged in.

Table I: have added a "contract" table (that has many contracts from many users)

  • id
  • usersid
  • description

I want to link it to the id in the users table already built into the vokuro example.

Table II: "workorder" table linked to "id" in"contract" table via contract id (1 contract will have many work orders)

  • id
  • contractid
  • contractor name

How do I limit the response for a user so they can only "workorder" entries that belongs thier user and not other users in that "workorder" table. I have tried making the relationships in the model but have not had any luck limiting the outputbased on user. Any help is greatly appreciated!



4.0k
Accepted
answer
edited 28d ago

This would be my approach.

Store the USER ID in a session on login.

$this->session->set('auth-identity', [
    'id' => $user->id,
]);

Create an action where you get the results based on the USER ID

    /**
     * The start action, it shows the "index" view
     */
    public function indexAction()
    {
        $timeregistration = TimeRegistration::findByUserId($this->session->get('auth-identity')['id']); 

        $this->view->timeregistration = $timeregistration;
    }

Make a relation between the tables

// Model user
  $this->belongsTo('user_id', __NAMESPACE__ . '\User', 'id', [
      'alias' => 'User'
  ]);

  // Model TimeRegistration
  $this->hasMany('id', __NAMESPACE__ . '\TimeRegistration', 'user_id', [
      'alias' => 'TimeRegistration',
       'foreignKey' => [
           'message' => 'Cannot delete user because of related registered time'
        ]
  ]);

I hope this will give you a general idea. However it might not be the best approach since I just nearly finished my first Phalcon application aswell. Ofcourse replace TimeRegistration for workorder



186

ill give it a try this mornig and see what i can come up with.



4.0k

ill give it a try this mornig and see what i can come up with.

It worked for you?