⚠ 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

Set a relation on another relation ?



Asmoth

Asmoth
  • profile picture
  • Member

Posted 19 September 2013 - 11:53 AM

Hello there,

 

I've been testing and using GroceryCrud for a month now. It works well, i'm still discovering stuff.

 

I'm stuck with some relationships. See my database structure :

 

[attachment=682:dbstructure.png]

 

I have a controller that displays "articles" for a given "numero". That works well.

I set a relation on fk_redacteur with the "member" table, and then i can get users' names.

 

What i want to do is : set a relation on the "member" table, so i can get users' names, but when i edit my "article" and choose my user related to it, i want that user to belong to a specific group (example : group identifiant : 3). Then when I edit my article, i get to choose only a user that belongs to group 3 and not every user in the member table. Do you see what I mean ?

 

Well, then, my controller :

	function articles($id)
	{
		echo "Affichage des articles";
    	
	    $this->grocery_crud->set_theme('datatables');
		$this->grocery_crud->set_table('magp3_article');
		$this->grocery_crud->display_as('fk_numero','Numero');
		$this->grocery_crud->display_as('fk_page','Page');
		$this->grocery_crud->display_as('fk_redacteur','Rédacteur');

		$this->grocery_crud->unset_read();
		
		//relation a mettre pour le add article
		$this->grocery_crud->set_relation('fk_page', 'magp3_page', 'MagP3 n°{fk_numero} - {description}', array('fk_numero' => $id));
		

		$this->grocery_crud->set_primary_key('id_redacteur','magp3_groupe_redacteur');
		$this->grocery_crud->set_relation('fk_redacteur','phpboost_member','login'); // relation A
		$this->grocery_crud->set_relation('fk_redacteur','magp3_groupe_redacteur','{id_redacteur}',array('id_groupe' => 3)); // relation B

		//Ce where sert pour filtrer la liste
		$this->grocery_crud->where('fk_numero',$id);
		

		//validation
		$this->grocery_crud->required_fields('description', 'fk_page');

		//affichage
		$output = $this->grocery_crud->render();
		$this->_example_output($output); 
	}

Seen the code ? So :

 

  • when I set "relation A" only, I get only the user name, and i have to choose between all users in the member table
  • when I set "relation B" only, I get only the user ID, but I have to choose between users of the group 3

I'm trying to find a way to combine the two relations, but how ? That's not a n to n relation (in my opinion, which you can discuss), and when I set both relations, only the last survives.

 

I've been searching the web for a couple of weeks now, whitout any success.

 

Do you have any idea ? I'm trying to use GroceryCrud the cleanest way possible. Do I have to use callbacks ? Is there a simple way to do this ?

 

Thanks for your help ;)


davidoster

davidoster
  • profile picture
  • Member

Posted 20 September 2013 - 19:32 PM

Hello and welcome to the forums [member=asmoth].

Well as I've written many times on these forums and I just express my own opinion,

Grocery CRUD library is a brilliant library that does a lot of magnificent things but some times we need to incorporate out own uniqueness.

By saying that I mean that on tidious situations like this one you describe I prefer to make my own set_relation function using the help of a custom model and combine the dataset result to a field. This way

- I don't need to think some complex combination of the GC API 

- I get to have my own SQL code

- I totally control the results and how they show up

 

So, instead of using the set_relation or even set_relation_n_n, by using a custome model and the field_type I get the exact output I need.

I hope this helps you.