Jump to content


custom model

set_model custom model

  • Please log in to reply
11 replies to this topic

#1 Clone Kihiato

Clone Kihiato

    Newbie

  • Members
  • Pip
  • 6 posts

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



#2 heruprambadi

heruprambadi

    Advanced Member

  • Members
  • PipPipPip
  • 83 posts
  • LocationIndonesia

Posted 17 July 2013 - 03:51 AM

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



#3 Clone Kihiato

Clone Kihiato

    Newbie

  • Members
  • Pip
  • 6 posts

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



#4 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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.



#5 Clone Kihiato

Clone Kihiato

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 17 July 2013 - 01: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



#6 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 17 July 2013 - 01: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 :)



#7 heruprambadi

heruprambadi

    Advanced Member

  • Members
  • PipPipPip
  • 83 posts
  • LocationIndonesia

Posted 17 July 2013 - 01: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.



#8 Clone Kihiato

Clone Kihiato

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 17 July 2013 - 02:02 PM

Thanks Amit,

 

How do I get my model working?

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



#9 Clone Kihiato

Clone Kihiato

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 17 July 2013 - 02: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.



#10 davidoster

davidoster

    Grocery CRUD Ninja

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

Posted 17 July 2013 - 03:32 PM

Hello and welcome to the forums 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/sa...mers_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.


____________________________________________________________

 

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

____________________________________________________________

 

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


#11 Clone Kihiato

Clone Kihiato

    Newbie

  • Members
  • Pip
  • 6 posts

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.



#12 OOAK

OOAK

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 27 July 2016 - 04:40 AM

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

Plz help.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users