⚠ 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

GC in postgres 9.1



jcasanova

jcasanova
  • profile picture
  • Member

Posted 13 April 2012 - 13:58 PM

Hi all As GC doesn't support Postgres, I made some fixes/changes to the model and I got it working!

TO DO: not too happy with one fix, about the insert_id, maybe someone could help.

If anybody needs it just let me know.

GREAT LIBRARY!!!

nahumnp

nahumnp
  • profile picture
  • Member

Posted 16 April 2012 - 22:48 PM

Hi, I would see your change.
I start a new project with Codeigniter 2.1 and PostgreSQL 9.1.

jcasanova

jcasanova
  • profile picture
  • Member

Posted 20 April 2012 - 11:56 AM

Hi, let me packed up and upload it. I need to change some stuff of the new version released the day before yesterday.
I'll keep you up

jcasanova

jcasanova
  • profile picture
  • Member

Posted 26 April 2012 - 14:13 PM

I merged the new version into the model that works with Postgres, maybe doesn't work a 100% , but forme work for almost everything


I attached the model and the library (library has a one little modification) , you could use the load_model function of GC, if you don't want to mess with the core.

You have to do a one modification (I could not fixed from grocery model) to the postgre_driver.php of CI




function insert_id()
{
$v = $this->_version();
$v = $v['server'];

$table = func_num_args() > 0 ? func_get_arg(0) : NULL;
$column = func_num_args() > 1 ? func_get_arg(1) : NULL;

if ($table == NULL && $v >= '8.1')
{
$sql='SELECT LASTVAL() as ins_id';
}
elseif ($table != NULL && $column != NULL && $v >= '8.0')
{
$sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
$query = $this->query($sql);
$row = $query->row();
$sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
}
elseif ($table != NULL)
{
// seq_name passed in table parameter
$sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
}
else
{
return pg_last_oid($this->result_id);
}
$query = $this->query($sql);
$row = $query->row();
return $row->ins_id;
}

jcasanova

jcasanova
  • profile picture
  • Member

Posted 10 May 2012 - 14:06 PM

Here is an updated version, Fixed the relation_n_n, now its working.

this is the link,

http://uploading.com/files/5aac6e5a/crud_ultimo%2B-%2BPG.rar/

shoieb0101

shoieb0101
  • profile picture
  • Member

Posted 14 July 2012 - 20:55 PM

[quote name='jcasanova' timestamp='1336658789' post='1697']
Here is an updated version, Fixed the relation_n_n, now its working.

this is the link,

http://uploading.com...ltimo+-+PG.rar/
[/quote]

thanks a lot for this. helped me a lot.

tkalfigo

tkalfigo
  • profile picture
  • Member

Posted 30 October 2012 - 06:17 AM

Hi web-johnny and jcasanova.

I'm on CodeIgniter 2.1 and Postgres 9.1 and installed jcasanova's modified code for Postgres and after solving first couple of issues I'm stuck.
So far the following are working fine:
- table's contents are displayed
- column sorting
- delete record
- pagination
- refresh [cyclic green arrows next to pagination controls]
- search is not working properly but it has to do with missing explicit cast because LIKE doesn't implicitly cast non-alpha fields to text.

MAIN ISSUE: The following code does not have the desired result even though no errors are generated (I'm doing full error logging):
$crud = new grocery_CRUD();
$this->grocery_crud->set_table('foo');
$crud->columns('name','price'); /* I still see ALL colums displayed */
$crud->display_as('name','Name');
$crud->display_as('price','Price in $'); /* The column headers are not affected */
$crud->add_fields('name','price'); //when I click "Add record" I see ALL fields e.g. the "Id" field which should be autoincremented and I don't want it user assigned*/

Because of this I get the following problems when I try to "Add record":
(minor problem) it presents the undesired ID field which should normally be autoincremented (it's "serial" in Pg and corresponds to a sequence)
(major problem as a consequence of the minor problem) I have to assign a value to all fields, including the ID, and "Save" is clicked, nothing happens in the frontend (no changes, no errors) even though the record is stored in the DB. The reason is that Firebug shows me that the POST generated a 500 error, and the Pg logs show:

statement: INSERT INTO "foo" ("id", "name", "price") VALUES ('15', 'takis', '10');
LOG: duration: 55.734 ms
LOG: statement: SELECT LASTVAL() as ins_id
ERROR: lastval is not yet defined in this session
STATEMENT: SELECT LASTVAL() as ins_id
LOG: statement: rollback
NOTICE: there is no transaction in progress

The rollback raises a Notice since apparently no BEGIN was issued to start with. But the real problem is the Error that is raised by the call to LASTVAL() because as the Pg manual explains: lastval
... it fetches the value of the last sequence that nextval was used on in the current session. It is an error to call lastval if nextval has not yet been called in the current session.
and given how the INSERT is made (with the ID value planted manually) there is no call to nextval()

To summarize: the problems starts with the "add_field()" call not working properly (tried it with array argument as well). From there, I have to fill in the ID field in the "Add record" form, and from there the INSERT has to plant the value for ID manually which means that Pg's nextval() is not called on the sequence that corresponds to the ID field which in turn results in the call to LASTVAL() not making sense.

So the question is: how do I make the "Add record" form only show the fields that should be user provided and not the ones that are supposed to be autogenerated by the DB?

TIA,
Thalis Kalfigkopoulos

jcasanova

jcasanova
  • profile picture
  • Member

Posted 30 October 2012 - 17:19 PM

Hi tkalfigo,

First of all reading at your code, I think the all the problems about displaying fields is because you are setting wrong the table
[color=#282828][font=helvetica, arial, sans-serif]you have :[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]$this->grocery_crud->set_table('foo');[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]It should be:[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]$this->crud->set_table('foo');[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]try that one, let see how it goes.[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]About the lastval() issue, maybe you installed wrong the model... because it was working for me at the end...[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]I'm gonna check the project that I was working with CI, and I'll tell you if you need new modifications...[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]regards![/font][/color]

botas_89

botas_89
  • profile picture
  • Member

Posted 30 October 2012 - 19:43 PM

Hi jcasanova


Thanks for the code and changes to postgresql ...

I introduced all the appropriate folders but I get an error:

[color=#000000][font='Times New Roman'][size=1]A PHP Error was encountered[/size][/font][/color]
[color=#000000][font='Times New Roman'][size=1]
Severity: Notice[/size][/font][/color][color=#000000][font='Times New Roman'][size=1]
Message: Undefined offset: 0[/size][/font][/color][color=#000000][font='Times New Roman'][size=1]
Filename: models/grocery_crud_model.php[/size][/font][/color][color=#000000][font='Times New Roman'][size=1]
Line Number: 328 [/size][/font][/color]


I do not understand :wacko: .. I'm new to CI and Grocery Crud ..

I would appreciate your help in advance.

greetings from VENEZUELA...

tkalfigo

tkalfigo
  • profile picture
  • Member

Posted 30 October 2012 - 22:12 PM

Thanx for the prompt reply jcasanova. I solved the issue, and it was indeed in my code and not in yours.

From reading this page from the documentation:
http://www.grocerycrud.com/documentation/tutorial_basic_methods
I copied this code:
$crud = new grocery_CRUD();
$crud->set_table('foo');


Apparently it works without explicitly instantiating the grocery_CRUD class.

All is set through $this->grocery_crud->crud_method() after of course you've loaded the library with $this->load->library('grocery_CRUD');

Regarding the "Search", it doesn't work for Pg if you search against any non-text field. I'll send the solution to this in a separate topic.

Thanx again,
Thalis K.

tkalfigo

tkalfigo
  • profile picture
  • Member

Posted 30 October 2012 - 22:14 PM

Hi botas_89

The solution is to always have a primary key in each table. So just add an "id serial" and it'll go away.


Regards,
Thalis K.

jcasanova

jcasanova
  • profile picture
  • Member

Posted 30 October 2012 - 22:29 PM

[quote name='tkalfigo' timestamp='1351635288' post='4107']
Hi botas_89

The solution is to always have a primary key in each table. So just add an "id serial" and it'll go away.


Regards,
Thalis K.
[/quote]

Thats an very IMPORTANT point, i wasted so many hour at the begining with that error... I missed the PK. So don't forget and check it very well.


I'm happy that works tkalfigo

botas_89

botas_89
  • profile picture
  • Member

Posted 09 November 2012 - 17:23 PM

Hello again, thanks for the help .... Now I have another error ...
I try to make a table with grocery crud and link through but I get error ID ... thanks for the help.


This is my Controller:

function offices_management($id_proyecto='0')
{
$codigo=$this->input->post('proyec'); //This is to bring me the ID. And yes, I accept, but when I enter a new record in my table disappears ID
$id_proyecto=$codigo;

try{

/* This is only for the autocompletion */

$this->id_proyecto=$id_proyecto;
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('objetivos_especificos');
$crud->where('id_proyecto',$id_proyecto);
$crud->set_subject('objetivos especificos');
$crud->required_fields("nombre_objetivo");
$crud->columns("id_proyecto","nombre_objetivo");
$crud->callback_add_field('id_proyecto',array($this,'add_field_callback_2'));
$crud->callback_before_insert(array($this,'add_proyecto'));
$output = $crud->render();
$this->_example_output($output);

}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}

function add_proyecto($post_array)

{
$this->id_proyecto=$id_proyecto;
//if(empty($post_array['nro_solicitud']))
//{
// $post_array['nro_solicitud'] = '5';
//}
$post_array['id_proyecto'] = $this->id_proyecto;
return $post_array;
}


function add_field_callback_2()

{


return '<input type="text" disabled="disabled" maxlength="50" value="'.$this->id_proyecto.'" name="id_proyecto" style="width:400px"> ( ID Proyecto )';
}


Alguna sugerencia? ¿Se ve que es lo que quiero hacer?

Raio Gmez

Raio Gmez
  • profile picture
  • Member

Posted 24 July 2013 - 19:29 PM

Here is an updated version, Fixed the relation_n_n, now its working.

this is the link,

http://uploading.com/files/5aac6e5a/crud_ultimo%2B-%2BPG.rar/

 

Jcasanova Good day, you have a newer version of GC, if not, could you support telling me what functions you modified? thanks


mcf

mcf
  • profile picture
  • Member

Posted 07 August 2015 - 20:20 PM

Hi everybody!

 

I'm working with Postgres 9.1 and i have trouble to apply the grocerycrud's grid. I read the message that the solution would be the modifications made jcasanova

I try download the jcasanova's file but it's infected... Someone can paste it into a post?

 

Thanks..


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 10 August 2015 - 11:59 AM

It aint corrupted .. its the way u download it.. uncheck - download the file using their download accelerator - down load using firefox - it goes through smoothly .. i downdloaded it just now - it works smooth.


blomersi

blomersi
  • profile picture
  • Member

Posted 16 September 2020 - 19:20 PM

Hello friends.

Does anyone still have this file?

thank you

 

 

Here is an updated version, Fixed the relation_n_n, now its working.

this is the link,

http://uploading.com/files/5aac6e5a/crud_ultimo%2B-%2BPG.rar/

 

Hello friends. Does anyone still have this file?