Help writing fuel_relationship belongs_to with has_many

edited June 2015 in Share
I have a table 'navtech_projects' that has_many 'navtech_companies' that in turn has_many 'fuel_users'. When a fuel_user is associated with a company, I need to see if they are authorized to view a particular project by verifying the company they belong to also belongs_to that project.

I wrote my own Navtech_users_model that inherits from Fuel_users_model and Navtech_user_model that inherits from Fuel_user_model respectively.

To make things easier, the Navtech_users_model contains:
public $belongs_to = array('companies' => array('model' => array(NAVTECH_FOLDER => 'navtech_companies_model')));

I have code for getting the users associated with a company in the Navtech_companies_model and I can get companies associated with a project from the Navtech_project_model.

From the Navtech_projects_model, I need to get all the Users (Projects -> Companies -> Users) and then test if the currently logged in user is in the user list?

Any help would be appreciated. Thanks!

If it helps...

Here is my code from Navtech_project_model:
protected function _get_companies() { if (empty($this->_companies)) { $this->_companies = $this->_parent_model->get_related_keys('companies', array('id' => $this->id), $this->_parent_model->has_many['companies'], 'has_many', $this->_parent_model->table_name()); } return $this->_companies; } public function get_companies() { $this->_CI->load->module_model(NAVTECH_FOLDER, 'navtech_companies_model'); $companies = $this->_get_companies(); if (empty($companies)) return array(); $query_params = array('where_in' => array($this->_tables['navtech_companies'].'.id' => $companies)); $result = $this->_CI->navtech_companies_model->query($query_params); return $result->result(); }

Here is my code from Navtech_company_model:
protected function _get_users() { if (empty($this->_users)) { $this->_users = $this->_parent_model->get_related_keys('users', array('id' => $this->id), $this->_parent_model->has_many['users'], 'has_many', $this->_parent_model->table_name()); } return $this->_users; } public function get_users() { $this->_CI->load->module_model(NAVTECH_FOLDER, 'navtech_users_model'); $users = $this->_get_users(); if (empty($users)) return array(); $query_params = array('where_in' => array($this->_tables['navtech_users'].'.id' => $users)); $result = $this->_CI->navtech_users_model->query($query_params); return $result->result(); }

Comments

  • edited 9:26AM
    Are you just needing to know how to get the currently logged in fuel_users.id? If so, you can get that at $this->fuel->auth->user_data('id').
  • edited 9:26AM
    Not exactly. I'm trying to figure out if I can do the following in one step:
    select * from fuel_relationships where candidate_table='navtech_companies' and foreign_table='fuel_users' and foreign_key=1
    The foreign_key is the fueld_users.id. The second step is to use the returned candidate_key, which is the navtech_companies.id.
    select * from fuel_relationships where candidate_table='navtech_projects' and foreign_table='fuel_companies' and foreign_key=4

    Is it possible to do this in one step, and if so, how would I do it in fuel code?
  • edited 9:26AM
    I did it like this, but it's a lot of work. Just wondering if there's an easier way (from the navtech_projects_model):

    function is_authorized_user() { $CI =& get_instance(); // check to see if user is logged in if (!$CI->fuel->auth->is_logged_in()) { return FALSE; } if ($CI->fuel->auth->is_super_admin()) { return TRUE; } $user_id = $CI->fuel->auth->user_data('id'); // get associated companies $companies = $this->get_companies(); foreach($companies as $company) { $users = $company->get_users(); foreach($users as $user) { if ($user->id == $user_id) { // authorized... return TRUE; } } } // not authorized return FALSE; }
  • edited 9:26AM
    You could directly access the fuel_relationships_model to run your queries using active record. It also has some helpful methods on it such as "find_by_candidate" and "find_by_foreign"
    $CI->load->module_model(FUEL_FOLDER, 'fuel_relationships_model'); ... $CI->fuel_relationships_model->....
Sign In or Register to comment.