⚠ 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

custom model



Clone Kihiato
  • profile picture
  • Member

Posted 16 July 2013 - 16:44 PM

From the various examples i find online its required that i over-ride an existing grocerycrud method with my own method, in this case im not sure which one to over-ride.

 

I have 2 tables

tblstudent: id, names, countryid, identification, dob.....status

tblcountries: id, country, code

 

what i would like to display in the columns is all relevant fields however, i would like the identification and country.code fields on the same column as "identification[country.code]".

 

The above modification should not interfere with the adding and editing of the fields i.e identification field remains a text field and country field is a dropdown with countries.

 

I hope to have explained my dilemma properly.


heruprambadi
  • profile picture
  • Member

Posted 17 July 2013 - 03:51 AM

you don't need custom model. just use set_relation.


Clone Kihiato
  • profile picture
  • Member

Posted 17 July 2013 - 09:22 AM

thanks heruprambadi,

set_relation from my point of understanding creates a dropdown on the related field im not sure how it will apply in the

$crud->columns method, could you please elaborate


Amit Shah
  • profile picture
  • Member

Posted 17 July 2013 - 11:57 AM

From the various examples i find online its required that i over-ride an existing grocerycrud method with my own method, in this case im not sure which one to over-ride.

 

I have 2 tables

tblstudent: id, names, countryid, identification, dob.....status

tblcountries: id, country, code

 

what i would like to display in the columns is all relevant fields however, i would like the identification and country.code fields on the same column as "identification[country.code]".

 

The above modification should not interfere with the adding and editing of the fields i.e identification field remains a text field and country field is a dropdown with countries.

 

I hope to have explained my dilemma properly.

 

 

Hi Kihiato, first of all  welcome to Grocerycrud forum.

The solution to your issue is a column callback to be used

 

$crud->callback_column('country_id', array($this, 'getCountryCode');

 

add a function getCountryCode($value, $row) ... in the same controller

and there -- using the $row->country_id, retrieve the country row and populate a string

return $row->country_id . "[" . $country->code . "]";

 

This should solve your issue.


Clone Kihiato
  • profile picture
  • Member

Posted 17 July 2013 - 13:16 PM

Thanks Amit Shah,

 

While the solution does work, it will in the long run have a premium on system resources assuming i get to 1000 records those will be 1000 extra queries over and above any other query required on that particular interface.

 

isn't there another shorter way to get the same results?

 

Thanks


Amit Shah
  • profile picture
  • Member

Posted 17 July 2013 - 13:21 PM

well.. technically the other way around is you can create your own custom model where you can have your custom query for the same and that should give it a fix instantly..

 

Rest apart, at a time, you may display 25-100 records.. yes thats an overhead but thats the cost for such simplicity of a complex system :)


heruprambadi
  • profile picture
  • Member

Posted 17 July 2013 - 13:49 PM

whoops ! i'm sorry. i think i misunderstood here.

maybe you want somethink like this. but you have to modified the example according what you need.


Clone Kihiato
  • profile picture
  • Member

Posted 17 July 2013 - 14:02 PM

Thanks Amit,

 

How do I get my model working?

The example provided in the documentation over rides an existing method.


Clone Kihiato
  • profile picture
  • Member

Posted 17 July 2013 - 14:07 PM

thanks heruprambadi,

 

the only problem with your suggestion is that i'm dealing with $crud->columns, im not sure how to insert "custom" data to a column.


davidoster
  • profile picture
  • Member

Posted 17 July 2013 - 15:32 PM

Hello and welcome to the forums [member=clone kihiato].

Let me see if I get this right,

During add/edit you want at the table tblstudent the fields identification and countryid to be present.

So we do this by using the fields function.

But we don't want to display these two fields while listing the records, so by using the columns function.

By doing this we eliminate two fields but we still need to display, while in listing (grid operation) the identification[country.code].

We can do this by doing the undocumented feature of a fake column explaind here.

 

Using the fake column (via the callback_column) you can generate this $row->identification . "[" . $row->countryid . "]";

But the problem with this is that you get the code but not the actual value (name) of the country.

If we assume that you have a set_relation about the countryid so the users during add/edit select the country name you can use an unofficial hack to display this.

 

1. Make sure at the top of your controller function you put this line: $this->output->enable_profiler(TRUE); This will enable the profiling display that CodeIgniter supplies

2. Look for the DATABASE Queries and press SHOW

3. Look for the SELECT query in order to find the relation you built and look for the AS part

4. Use the AS part as the name within your callback_column e.g. $row->s3f168119

 

 

Check this example,

 

http://eletter.gr/samples/gc_ic/index.php/gc_examples/customers_management

 

s85l.png

 

 

and the code is

 

function customers_management()
{
	$this->output->enable_profiler(TRUE);
	
	$crud = new grocery_CRUD();
	$crud->set_table('customers');
	$crud->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit', 'time_col', 'fake_column');
	$crud->fields('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit', 'true_col', 'time_col');
	$crud->display_as('salesRepEmployeeNumber','from Employee')
		->display_as('customerName','Name')
		->display_as('contactLastName','Last Name')
		->display_as('time_col', 'Time Column test');
	$crud->set_subject('Customer');
	$crud->set_relation('salesRepEmployeeNumber','employees','lastName');
	$crud->set_relation('true_col','film','{title} {release_year}');
	$crud->callback_column('fake_column',array($this,'_cb_fake_column'));						
	$output = $crud->render();
	$this->_gc_output($output);
}	
	
public function _cb_fake_column($value, $row)
{
	return $row->country . "[" . $row->s3f168119 . "]";
}

I hope this makes sense.


Clone Kihiato
  • profile picture
  • Member

Posted 22 July 2013 - 12:45 PM

Thank you davidoster

 

I did try your proposal and it worked. Had to make a few minor adjustments, added if clauses to allow for different joins ie in add or edit the countries are listed in full but in list mode, the country code are used.


OOAK
  • profile picture
  • Member

Posted 27 July 2016 - 04:40 AM

I have created my custom model. The model example here is not so clear http://www.grocerycrud.com/documentation/options_functions/set_model. I dont know where i can put my own query.

Plz help.