⚠ 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

Columns from a n to n in a fourth table



ppalmeida

ppalmeida
  • profile picture
  • Member

Posted 07 January 2014 - 22:23 PM

Hi.

 

First of all, I really >Love< Grocerycrud. Congratulations, it us wonderful.

 

I have a doubt about showing some values from a "far away table", hehe. I did a image to help understand my goal:

 

l52u.jpg

 

I have four tables. The AccessToken table has its list on Grocery Crud. Then I have "subscriberdevice", "subscriber" and "device" tables, because subscriber and device are n to n relationship.

 

What i want is display (on accesstoken list) the "name" field of the subscriber table and the "uuid" field of the device table. But accesstoken has just the subscriberdevice foreign key.

 

But i want it displayed ONLY on list. In "add" or "edit" it should shown the normal relation 1 to 1 to subscriberdevice table. 

 

Any help is much appreciated.

Thank you.

 

Pedro Paulo Almeida

Brazil


ppalmeida

ppalmeida
  • profile picture
  • Member

Posted 13 January 2014 - 20:26 PM

Well.

After look here an there and googling a lot, I found my way: extend Grocerycrud model. And that was easy.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_Custom_model extends grocery_CRUD_Model  {

	function get_list() {
		# Run the default get_list method
		$results = parent::get_list();

		# Make my joins to get the subscriber.login and device.uuid values
		foreach ($results as $i => $item) {
			$search = $this->db
						->select('login, uuid')
						->join('subscriber', 'subscriberdevice.fk_subscriber=subscriber.id')
						->join('device', 'subscriberdevice.fk_device=device.id')
						->where('subscriberdevice.id', $item->fk_subscriberdevice)
						->limit(1)
						->get('subscriberdevice');

			$search = $this->emptySearch($search->result());
			$item->login = $search->login;
			$item->device = $search->uuid;
		}

		# Return the alter results item list
		return $results;
	}

	# If the search has no record (it shouldnt in teory), return empty strings on those fields
	private function emptySearch($search) {
		if ($search and count($search) > 0) {
			return $search[0];
		}

		$search = new stdClass();
		$search->login = "";
		$search->uuid = "";
		return $search;
	}
}

Well...

 

Before it was like this:

3wfb.png

 

 

And now it looks like this:

 

tfuf.png

 

 

Hope this can help someone.

 

Thank you.