⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Filter relation using third table



TechDaddies-Kevin
  • profile picture
  • Member

Posted 15 April 2013 - 14:02 PM

This is a bit difficult to explain. We are using Ion Auth and GC on a new CI project. We have a simple form that allows an admin to create a record by entering a name and selecting an owner. The relation currently looks like this:

 

$crud->set_relation('owner_id','users','email');

 

The problem is that this is showing ALL of the users in from the users table. We need to filter the dropdown to show only users where the user belongs to user group ID 10. The users-->groups associations are stored in users_groups which has columns for user_id and group_id;

 

So basically for the relation, we need it to show users.email where users.id is in (SELECT user_id FROM users_groups WHERE group_id=10).

 

How can we accomplish this in GC? I believe we might have to use a custom model to do this, but I'm having trouble understanding how to implement it.

 

Thanks in advance.


Christophe Conduché
  • profile picture
  • Member

Posted 15 April 2013 - 14:52 PM

same type of problem for me !

I will be pleased to read how you manage this ?


victor
  • profile picture
  • Member

Posted 15 April 2013 - 15:31 PM

do you need to have filter on the add/edit pages?


TechDaddies-Kevin
  • profile picture
  • Member

Posted 15 April 2013 - 15:31 PM

Yes. When adding or editing a record from this table, we need this filter to be active.


victor
  • profile picture
  • Member

Posted 15 April 2013 - 15:42 PM

I think the calback_add_field/calback_edit_field functions are for you


victor
  • profile picture
  • Member

Posted 15 April 2013 - 15:42 PM

http://www.grocerycrud.com/documentation/options_functions/callback_add_field


TechDaddies-Kevin
  • profile picture
  • Member

Posted 15 April 2013 - 15:44 PM

So you're saying to use callback add_field and callback_edit_field to run my own query and generate my own code to show the dropdown?


victor
  • profile picture
  • Member

Posted 15 April 2013 - 15:48 PM

yes, I mean that


davidoster
  • profile picture
  • Member

Posted 15 April 2013 - 21:19 PM

To be honest I find it easier to build a new model and just say,

/* sample code */

//mymodel.php under application/models
<?php
class mymodel extends CI_Model  {
	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

        public function get_activities_by_year($table, $year)
	{
		$this->db->select('id, year, description');
		$this->db->where('year', $year);
                $this->db->order_by('id desc');
		return $this->db->get($table);
	}


}

//controller.php
...
$this->load->model('mymodel');
$activities = $this->mymodel->get_activities_by_year("activities","2013");
foreach ($activities->result() as $row)
{
 $myarray[$row->id] .= $row->description;
}
$this->grocery_crud->field_type('interest_for','multiselect',$myarray);
...

TechDaddies-Kevin
  • profile picture
  • Member

Posted 15 April 2013 - 22:17 PM

Ah, excellent! I had no idea that you could do it like that. That workflow fits much more with the way I do things, as well.

 

Thanks so much!


Christophe Conduché
  • profile picture
  • Member

Posted 16 April 2013 - 07:48 AM

thanks too ! I keep this one in my "top hints" list ;-)


heruprambadi
  • profile picture
  • Member

Posted 16 July 2013 - 16:23 PM

i just want to correct here.

when i copied that code, i got some error.

it toke 12 hours for me to know that there is a point (.) there, before equals.

$myarray[$row->id] .= $row->description;

just delete it.