Jump to content


Filter relation using third table


  • Please log in to reply
11 replies to this topic

#1 TechDaddies-Kevin

TechDaddies-Kevin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 15 April 2013 - 02: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.



#2 Christophe Conduché

Christophe Conduché

    Member

  • Members
  • PipPip
  • 19 posts

Posted 15 April 2013 - 02:52 PM

same type of problem for me !

I will be pleased to read how you manage this ?



#3 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 15 April 2013 - 03:31 PM

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



#4 TechDaddies-Kevin

TechDaddies-Kevin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 15 April 2013 - 03:31 PM

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



#5 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 15 April 2013 - 03:42 PM

I think the calback_add_field/calback_edit_field functions are for you



#6 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 15 April 2013 - 03:42 PM

http://www.grocerycr...lback_add_field



#7 TechDaddies-Kevin

TechDaddies-Kevin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 15 April 2013 - 03: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?



#8 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 15 April 2013 - 03:48 PM

yes, I mean that



#9 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 15 April 2013 - 09: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);
...

Edited by davidoster, 01 October 2013 - 05:50 PM.
custom model

____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#10 TechDaddies-Kevin

TechDaddies-Kevin

    Member

  • Members
  • PipPip
  • 23 posts

Posted 15 April 2013 - 10: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!



#11 Christophe Conduché

Christophe Conduché

    Member

  • Members
  • PipPip
  • 19 posts

Posted 16 April 2013 - 07:48 AM

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



#12 heruprambadi

heruprambadi

    Advanced Member

  • Members
  • PipPipPip
  • 83 posts
  • LocationIndonesia

Posted 16 July 2013 - 04: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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users