Subscribe: Posts / Comments / Email


RSS

PHP – RESTful Data Access Object

Sun, Jun 14, 2009

Lab

You may use this class as a REST type service or as a general class. Its great, CRUD on any database and table.

To create a table record, do the same as above just make the id field for your table 0, this is because the class does a check, to see if there is a key with the value of ( table primary key ), if there is a key, then it continues and checks the value of that key, in the array if the value is set to 0 or ‘ ‘, then it creates a new record, otherwise it updates that record in which is specified in the value of the key array.

To update a table record, simply just specify the the values for the fields in which you are wanting to update.
Make sure it is an assoc array containing array keys ( which are table columns ) and values ( which are the //updated values )

RESTful DAO

get(database, table)

URL Example:


http://localhost/DataAccessObject.php?m=get&d=test&t=tags

JSON Response:

[
	{
	  status: 200,
	  mode: "get",
	  result: [
	    {
	      id: "115",
	      tag_title: "FlexMe",
	      user_id: "0"
	    }
	  ]
	}
]

getOne(database, table, value)

URL Example:


http://localhost/DataAccessObject.php?m=getOne&d=test&t=tags&id=5

JSON Response:

[
	{
	  status: 200,
	  mode: "getOne",
	  result: [
	    {
	      id: "113",
	      tag_title: "FlexMe",
	      user_id: "0"
	    }
	  ]
	}
]

update(database, table, value)

URL Example:


http://localhost/DataAccessObject.php?m=save&d=test&t=tags&id=4&tag_title=Fuck&user_id=3

JSON Response:

[
	{
	  status: 200,
	  mode: "update",
	  result: 1
	}
]

save(database, table, value)

URL Example:


http://localhost/DataAccessObject.php?m=save&d=test&t=tags&id=0&tag_title=FuckIt&user_id=0

JSON Response:

[
	{
	  status: 200,
	  mode: "save",
	  result: 1
	}
]

remove(database, table, value)

URL Example:


http://localhost/DataAccessObject.php?m=remove&d=test&t=tags&id=106

JSON Response:

[
	{
	  status: 200,
	  mode: "remove",
	  result: 1
	}
]

Failed Query

JSON Response:

[
  {
    status: 401,
    message: "Unknown method or incorrect parameters"
  }
]

Tags: , ,

Leave a Reply