PDA

View Full Version : Indicating CRUD action to a backend script



jmozley
14 Mar 2012, 1:19 AM
I am looking for what I expected to be a neater way to indicate to a store's URL script the type of operation it was called to perform. If I use the following in a proxy config within a store I can pass a parameter to the script as below:



api : {
create : 'cgi-bin/crud_script.pl?cmd=create',
read : 'cgi-bin/crud_script.pl?cmd=read',
update : 'cgi-bin/crud_script.pl?cmd=update',
destroy : 'cgi-bin/crud_script.pl?cmd=destroy'
}


The above may be OK for one store, but it would make sense to have this as a standard for all stores. That way I could just use the url config item and know all scripts would be called the same way.

mitchellsimoens
14 Mar 2012, 10:04 AM
So you want just one place to specify this api but more importantly the urls?

jmozley
28 Mar 2012, 6:30 AM
I'm not sure I asked the right question to be honest. What I was trying to work out is how a CGI script knows whether it should, for example, fetch or delete data if the same script is used for all CRUD operations. For instance if the ExtJS makes a call and passes a parameter ID=1 how does it indicate to the backend to delete row 1 or get the data of row 1 in a database table.

SkyBlader
29 Mar 2012, 1:39 AM
You defined in the CRUD-Api section the same script with an cmd parameter to the script.

Simply check in your script, which value the parameter "cmd" is and do the right action:

In PHP:


if('create' == $cmd) {
// do create record stuff }
if('read' == $cmd) {
// read record stuff
}
if('update' == $cmd) {
// create record stuff
}
if ('delete' == $cmd) {
// delete record stuff}


In your example the cmd parameter is an GET paramenter => $_GET['cmd'] in PHP.

jmozley
29 Mar 2012, 2:40 AM
Many thanks for the reply SkyBlader. I understand that the config below will pass the parameters as you said.



api : {
create : 'cgi-bin/crud_script.pl?cmd=create',
read : 'cgi-bin/crud_script.pl?cmd=read',
update : 'cgi-bin/crud_script.pl?cmd=update',
destroy : 'cgi-bin/crud_script.pl?cmd=destroy'
}


What I'm puzzled by is my expectation that ExtJS would set a parameter automatically indicating create, read, update or destroy. I was looking for this in the documentation and couldn't find it. So I've either missed something or there is no automatically passed parameter. In the latter case the above would work, but I wonder if this the the best approach.