Jump to content

Abstracting DB in php class

- - - - -

  • Please log in to reply
2 replies to this topic

#1
rhossis

rhossis

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Hi, I am re factoring an application to use a data driven design. The db is MySQL and does not change frequently. Is it correct to abstract the tables in such a manner? The proposal is that Controller functions will convert the PHP arrays to JSON for use clientside. I would like assistance on the advantages/disadvantages of such an approach.

class AdministrationContactList extends Administration

{

	private $table_name;

        private $base_system_level;

	private $object_fields = array();

	private $keys = array();

	

	function AdministrationContactList()

	{

		parent::Administration();

		

	       $this->table_name = "administration_contactlist";

                //defines minimum rights user level allowed to access info

               $this->base_system_level = "oper";

		//object_fields mirrors the fields in the contact_list table in MySQL

                $this->object_fields=array(

				"id"=>array("type"=>"MEDIUMINT(20)","level"=>""),

				"name_of_contact"=>array("type"=>"VARCHAR(255)","level"=>""),

				"institution"=>array("type"=>"VARCHAR(255)","level"=>""),

				"job_title"=>array("type"=>"VARCHAR(255)","level"=>""),

				"phone_number"=>array("type"=>"VARCHAR(255)","level"=>""),

				"email"=>array("type"=>"VARCHAR(255)","level"=>""),

				"fax"=>array("type"=>"VARCHAR(255)","level"=>""),

				"other"=>array("type"=>"LONGTEXT","level"=>""),

				"send_trade_info"=>array("type"=>"VARCHAR(255)","level"=>""),

				"date"=>array("type"=>"DATE","level"=>""),

				"statuss"=>array("type"=>"TINYINT(4)","level"=>"")

			);

		        //define keys

                        $this->keys=array(

				"primary"=>"id",

				"unique"=>array("name_of_contact")	

			);						

       }

}

Edited by rhossis, 13 February 2011 - 11:24 AM.


#2
xetama

xetama

    Newbie

  • Members
  • Pip
  • 9 posts
It's been awhile since I've programmed in PHP so I'm not quite sure what your aim is in abstracting the database calls. Generally it's wise to keep the database structure information as far away from your logic as possible. Unless you're writing a PHP system to actually manage the database like PHPMySQL you should probably just stick to wrapper functions that escape the input and pass them to a stored procedure, or query string. JSON is an easy format to serialise information into... so I'm not sure how this layer of complexity helps simplify your calls.

#3
rhossis

rhossis

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Hi xetama. The main point of our proposed abstraction is that the design requires a lot of logical access control given that the system has quite a number of api functions to other modules which require different levels of data access, which are issued per mysql table and by field. I felt this would be an alternative to granting various rights on mysql, and creating views and triggers. Also, the table I have posted is not an example, but some of our data is schemaless e.g. serialized arrays which get converted to json when loaded on application, and I would like to bring the description on to logic level to give it some structure. Our back end programmer has suggested we use ORM and we will probably go through this way. I was doing some prototyping because I am really a business logic level programmer and not back end expert. I would like to do it without possibly adding the ORM which would add another layer of application into the stack.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users