⚠ 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

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



web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 15 January 2012 - 22: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:
[quote]
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.
[/quote]

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 [color=#2C575B][font=Arial, Tahoma, Helvetica, sans-serif]grocery_CRUD_Model[/font][/color]. 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:
[quote]
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.
[/quote]

You can either copy the code or download the [attachment=69:my_grocery_model.php] attached file.

steveoc

steveoc
  • profile picture
  • Member

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

duangsin.k

duangsin.k
  • profile picture
  • Member

Posted 25 May 2012 - 08:41 AM

Good Skill

richardsy

richardsy
  • profile picture
  • Member

Posted 11 September 2012 - 21: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.

DREON

DREON
  • profile picture
  • Member

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..

DREON

DREON
  • profile picture
  • Member

Posted 06 December 2012 - 01:54 AM

i got this error sir..

[color=#000000][font=Arial][background=rgb(255, 182, 193)]
Severity: Warning[/background][/font][/color][color=#000000][font=Arial][background=rgb(255, 182, 193)]
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[/background][/font][/color][color=#000000][font=Arial][background=rgb(255, 182, 193)]
Filename: libraries/grocery_crud.php[/background][/font][/color][color=#000000][font=Arial][background=rgb(255, 182, 193)]
Line Number: 2006[/background][/font][/color]

goFrendiAsgard

goFrendiAsgard
  • profile picture
  • Member

Posted 24 February 2013 - 13: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


 


davidoster

davidoster
  • profile picture
  • Member

Posted 25 February 2013 - 00:20 AM

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


streeksoft

streeksoft
  • profile picture
  • Member

Posted 14 March 2013 - 10:45 AM

Excellent explanation, thanks! 


maxxe

maxxe
  • profile picture
  • Member

Posted 27 August 2013 - 20:04 PM

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

thanks


davidoster

davidoster
  • profile picture
  • Member

Posted 28 August 2013 - 02:38 AM

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

thanks

 

Try Grocery_CRUD_Model


maxxe

maxxe
  • profile picture
  • Member

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

 

 


davidoster

davidoster
  • profile picture
  • Member

Posted 28 August 2013 - 13:03 PM

Hmm. Probably wrong advice there [member=maxxe]. I was thinking CI 3!

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


maxxe

maxxe
  • profile picture
  • Member

Posted 28 August 2013 - 13:30 PM

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


hieunk

hieunk
  • profile picture
  • Member

Posted 25 October 2015 - 21:46 PM

Try Grocery_CRUD_Model


woof

woof
  • profile picture
  • Member

Posted 02 December 2015 - 21:05 PM

Sorry, wrong topic, could you please delete?