Jump to content


How to create an extension for grocery CRUD (real example included)

extension patch plugin

  • Please log in to reply
15 replies to this topic

#1 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

Posted 15 January 2012 - 10:49 PM

Hello all,

Well this is just a recommendation for how to create an extension and add it to the forum. This is very useful if you want to share your code and make it updatable with newer versions of grocery CRUD.

The below example works fine and you can use it to your project also if you like it.

First of all you have to describe what this patch/extension is doing and how much it helps you to solve some problems you have.

For example:

This extension logs the date - time that someone insert or update the database with the CRUD. If the fields "created" and "updated" exists at the table then automatically add the datetime to log the date and the time updated/created. It really helped me as I don't write the same auto queries again and again and I wanted to share it with you. It works fine with DATETIME type and TIMESTAMP type.


Second thing that you have to do is to share all the code to the forum with the code tags. Even if you have attached the file into the post, you have to show to the topic the code (or if there are lot of changes a big part of the code).

For example:
class MY_grocery_Model  extends grocery_CRUD_Model{
  
	function db_update($post_array, $primary_key_value)
	{
		if ($this->field_exists('updated'))
		{
			$this->load->helper('date');
			$post_array['updated'] = date('Y-m-d H:i:s',now());
		}
	
		return parent::db_update($post_array, $primary_key_value);
	}  
  
	function db_insert($post_array)
	{
		if ($this->field_exists('updated') && $this->field_exists('created'))
		{
			$this->load->helper('date');
			$post_array['created'] = date('Y-m-d H:i:s',now());
			$post_array['updated'] = date('Y-m-d H:i:s',now());
		}
		return parent::db_insert($post_array);
	}
  
}

So as you see from the example we created a class named MY_grocery_Model that extends grocery_CRUD_Model. This is very useful for the library to be updated with a new version of grocery CRUD. The same thing of course is a good practice to do with the basic library so for example if we want to create an extension for grocery CRUD we can just create an extension with name MY_grocery_CRUD that extends grocery_CRUD.

The next step is to explain how to install the patch so in our case we have:

Save this code in a file named : my_grocery_model.php at the folder application/models/ or download the attached file and copy it to application/models/ . The only thing now that you have to do is everytime that you want to use this model just add this line of code to your crud function:

$crud->set_model('MY_grocery_Model');
and that's it.


You can either copy the code or download the Attached File  my_grocery_model.php   767bytes   1401 downloads attached file.
Posted Image

#2 steveoc

steveoc

    Member

  • Members
  • PipPip
  • 22 posts

Posted 16 January 2012 - 05:49 AM

Good writeup - thanks for that.

Since I have a lot of new custom field types that I want to add, I am looking at ways of doings this as a sort of plug-in library that can be re-used by people without having to hack the internals of GC for each new field type.

I will keep the above guidelines in line when documenting the extension.

Cheers

#3 duangsin.k

duangsin.k

    Member

  • Members
  • PipPip
  • 10 posts

Posted 25 May 2012 - 08:41 AM

Good Skill

#4 richardsy

richardsy

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 11 September 2012 - 09:31 PM

No doubt it works.
I however want to be able to display Created and Updated in datatable. When I click Create I don't want to see Created and Updated. When I View/Edit I want to be able to see the Created and Edited fields ideally although not essential. However when I click save have the Updated automatically set to now(). Ideally I should be able to do this by setting properties and not writing customer classes, etc. I should ideally be able to specify what the names of my Created and Updated fields are, e.g. CreatedDateTime, UpdatedDateTime.
This would be very powerful.

#5 DREON

DREON

    Advanced Member

  • Members
  • PipPipPip
  • 118 posts

Posted 05 December 2012 - 12:52 PM

thank you for this extension sir johnny,, but one question why is that when i update my data the date is correct but the time is incorrect, my computer time is 8:46 but on my updated fiels is 13:46:28 its like 01:46:28 how can i set my timestamp correct.. sorry for bad english..

#6 DREON

DREON

    Advanced Member

  • Members
  • PipPipPip
  • 118 posts

Posted 06 December 2012 - 01:54 AM

i got this error sir..


Severity: Warning

Message: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead

Filename: libraries/grocery_crud.php

Line Number: 2006


#7 goFrendiAsgard

goFrendiAsgard

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 24 February 2013 - 01:04 PM

i got this error sir..


Severity: Warning
Message: mktime() [function.mktime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead
Filename: libraries/grocery_crud.php
Line Number: 2006

Yeah, I got that too.
It is because you use PHP 5.3
However, adding these lines in module constructor solve the problem:

        $timezone = @date_default_timezone_get();
        if (!isset($timezone) || $timezone == '') {
            $timezone = @ini_get('date.timezone');
        }
        if (!isset($timezone) || $timezone == '') {
            $timezone = 'UTC';
        }
        date_default_timezone_set($timezone); 

Basically we need to set the default timezone. I wonder why the hell PHP 5.3 doesn't detect it automatically


 


No-CMS homepage : http://www.getnocms.com
No-CMS forum : http://www.getnocms.com/forum
No-CMS Github page : https://github.com/g...diAsgard/No-CMS

 

Please don't mix up No-CMS and groceryCRUD issues.
(eg: user authentication etc is No-CMS specific issue and has nothing to do with groceryCRUD)

 


#8 davidoster

davidoster

    Grocery CRUD Ninja

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

Posted 25 February 2013 - 12:20 AM

It's not that it doesn't detect it! From 5.3 onwards is required to be set!


____________________________________________________________

 

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

____________________________________________________________

 

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


#9 streeksoft

streeksoft

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 14 March 2013 - 10:45 AM

Excellent explanation, thanks! 



#10 maxxe

maxxe

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 27 August 2013 - 08:04 PM

Class 'grocery_CRUD_Model' not found in..... on line 3

thanks



#11 davidoster

davidoster

    Grocery CRUD Ninja

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

Posted 28 August 2013 - 02:38 AM

Class 'grocery_CRUD_Model' not found in..... on line 3

thanks

 

Try Grocery_CRUD_Model


____________________________________________________________

 

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

____________________________________________________________

 

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


#12 maxxe

maxxe

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 28 August 2013 - 06:43 AM

Thanks Davidoster, same error after correcting:

 

MODEL: tracciabilita_crud_model.php

class Tracciabilita_CRUD_Model extends Grocery_CRUD_Model { .....

CONTROLLER/construct:

 

$this->load->library('grocery_CRUD');
$this->load->model('Tracciabilita_CRUD_Model');
METHOD:
$crud = new grocery_CRUD();

$crud->set_model('Tracciabilita_CRUD_Model');
$crud->set_table('ps_orders'); //Change to your table name
$crud->basic_model->set_query_str("SELECT * from ps_orders"); //Query text here
$output = $crud->render();
 
Fatal error: Class 'Grocery_CRUD_Model' not found in C:\easyphp\codeigniter_2_1_4\application\models\tracciabilita_crud_model.php on line 3

 

 
Thanks

 

 


Edited by maxxe, 28 August 2013 - 06:53 AM.


#13 davidoster

davidoster

    Grocery CRUD Ninja

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

Posted 28 August 2013 - 01:03 PM

Hmm. Probably wrong advice there maxxe. I was thinking CI 3!

And you're certain that the model file exists under application/models?


____________________________________________________________

 

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

____________________________________________________________

 

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


#14 maxxe

maxxe

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 28 August 2013 - 01:30 PM

sure! i'm using CI 2.1.4 a CI 3 is not already here



#15 hieunk

hieunk

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 25 October 2015 - 09:46 PM

Try Grocery_CRUD_Model



#16 woof

woof

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 02 December 2015 - 09:05 PM

Sorry, wrong topic, could you please delete?







Also tagged with one or more of these keywords: extension, patch, plugin

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users