1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <!--php defined( 'ABSPATH' ) or die ( "Your are not welcome" ); class GenericDao{ /** * $metadefintion is an array that contains pairs * key:colName val: data type (s,d,f) * Note 1.data Type must be accepted by wordpress * 2.Firdst item is Id and it is autoincrement * @var type */ private $metaDefinition ; /** * Name of the table * @var type */ private $tableName ; function __construct ( $metaDefinition , $tableName ){ $this --->metaDefinition = $metaDefinition ; $this ->tableName = $tableName ; } /** * Insert a new row * @global type $wpdb * @param type $pairs * @return type last inserted id */ function insertRow( $pairs ){ global $wpdb ; $prototypes = array (); foreach ( $pairs as $colName => $dataType ) { $prototypes [] = $this ->metaDefinition[ $colName ]; } $wpdb ->insert( $this ->tableName, $pairs , $prototypes ); return $wpdb ->insert_id; } /** * Select all rows * @global type $wpdb * @param type $orderingExpr * @param type $limit * @return type */ function selectAllRows( $orderingExpr = " " , $limit = "" ){ global $wpdb ; $sql = "select * from $this->tableName " . $orderingExpr . $limit ; return $wpdb ->get_results( $sql ); } /** * Query table * @global type $wpdb * @param type $where * @param type $orderingExpr * @param type $limit * @return type */ function selectRows( $where , $orderingExpr = " " , $limit = "" ){ global $wpdb ; $sql = "select * from $this->tableName where " . $where . ' ' . $orderingExpr . ' ' . $limit ; return $wpdb ->get_results( $sql ); } /** * Select rows count * @global type $wpdb * @return type */ function selectRowsCount(){ global $wpdb ; $sql = "select count(*)as count from $this->tableName " ; return $wpdb ->get_results( $sql ); } /** * Delete row * @global type $wpdb * @param type $pairs array of paris (columns, new values) * @param type $where * @param type $whereFormat * @return type */ function updateRow( $pairs , $where , $whereFormat = array ( "%d" )){ global $wpdb ; $prototypes = array (); foreach ( $pairs as $colName => $dataType ) { $prototypes [] = $this ->metaDefinition[ $colName ]; } return $wpdb ->update( $this ->tableName, $pairs , $where , $prototypes , $whereFormat ); } function deleteRow( $where , $whereFormat = array ( "%d" )){ global $wpdb ; return $wpdb -> delete ( $this ->tableName, $where , $whereFormat ); } } ?> |
Supposing that we have this table :
1 2 3 4 5 | CREATE TABLE IF NOT EXISTS `wordpress_categ_type_poste` ( `ctgtp_id` INT NOT NULL AUTO_INCREMENT , `ctgtp_lib` VARCHAR (45) NOT NULL , PRIMARY KEY (`ctgtp_id`) ) ENGINE = InnoDB; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!--php defined( 'ABSPATH' ) or die ( "Your are not welcome" ); require_once (dirname( __FILE__ ). '/generic-dao.php' ); class CategTypePosteDao extends GenericDao{ function __construct(){ global $wpdb ; parent::__construct( array ( 'ctgtp_id' =--> '%d' , 'ctgtp_lib' => '%s' ), $wpdb ->prefix . "categ_type_poste" ); } } ?> |