Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. <?php
  2. /*
  3. This file is part of Mkframework.
  4.  
  5. Mkframework is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation, either version 3 of the License.
  8.  
  9. Mkframework is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. */
  18. /**
  19. * classe
  20. * @author Mika
  21. * @link http://mkf.mkdevs.com/
  22. */
  23. abstract class abstract_model {
  24.  
  25. private $_sClassSgbd=null;
  26. private $_oSgbd=null;
  27. private static $_tInstance=array();
  28.  
  29. protected $_tAssoc=array();
  30.  
  31. public function getConfig(){
  32. return $this->sConfig;
  33. }
  34. public function getTable(){
  35. return $this->sTable;
  36. }
  37.  
  38. /**
  39. * @access public static
  40. * @return object sgbd
  41. */
  42. public function getSgbd(){
  43. $bFirst=false;
  44. if($this->_sClassSgbd==null){
  45. $sVarIniConfig=_root::getConfigVar('model.ini.var','db');
  46. $tClassSgbd=_root::getConfigVar($sVarIniConfig);
  47. if(!$tClassSgbd){
  48. $sMsg='Il vous manque un fichier de configuration';
  49. $sMsg.=' ou le bloc de configuration ['.$sVarIniConfig.'] concernant la connexion'."\n";
  50. $sMsg.='
  51. Exemple:
  52. ['.$sVarIniConfig.']
  53. mysql.dsn="mysql:dbname=blog;host=localhost"
  54. mysql.sgbd=pdo_mysql
  55. mysql.hostname=localhost
  56. mysql.database=blog
  57. mysql.username=root
  58. mysql.password=pass
  59. ';
  60. throw new Exception($sMsg);
  61. }
  62. $this->_sClassSgbd='sgbd_'.$tClassSgbd[$this->sConfig.'.sgbd'];
  63. $bFirst=true;
  64. if(substr($this->_sClassSgbd,0,8)=='sgbd_pdo'){
  65. $sClassPath=_root::getConfigVar('path.lib').'sgbd/pdo/'.$this->_sClassSgbd.'.php';
  66. }elseif(substr($this->_sClassSgbd,0,5)=='sgbd_'){
  67. $sClassPath=_root::getConfigVar('path.lib').'sgbd/'.$this->_sClassSgbd.'.php';
  68. }
  69. if(!file_exists($sClassPath)){
  70. $oDirPdo=new _dir( _root::getConfigVar('path.lib').'sgbd/pdo/');
  71. $tListPdo=$oDirPdo->getListFile();
  72. $tPlus=array('Liste driver pdo:');
  73. foreach($tListPdo as $oFile){
  74. $tPlus[]='-'.$oFile->getName();
  75. }
  76.  
  77. $sListePdo=implode("\n",$tPlus);
  78.  
  79. $oDir=new _dir( _root::getConfigVar('path.lib').'sgbd/');
  80. $tList=$oDir->getListFile();
  81. $tPlus=array('Liste driver autre:');
  82. foreach($tList as $oFile){
  83. $tPlus[]='-'.$oFile->getName();
  84. }
  85. $sListeAutre=implode("\n",$tPlus);
  86.  
  87. throw new Exception('Pas de driver '.$this->_sClassSgbd.' ('.$sClassPath.')'."\n".$sListePdo."\n".$sListeAutre);
  88. }
  89. }
  90.  
  91. $this->_oSgbd=call_user_func( array($this->_sClassSgbd,'getInstance'),$this->sConfig);
  92.  
  93. if($bFirst){
  94. $this->_oSgbd->setConfig($tClassSgbd);
  95. }
  96.  
  97. return $this->_oSgbd;
  98. }
  99. /**
  100. * retourne un tableau d'enregistrement d'objet simple (plus rapide)
  101. * @access public
  102. * @param string $sRequete
  103. * @param undefined $uParam
  104. * @return un object
  105. */
  106. public function findOneSimple(){
  107. $tSql=func_get_args();
  108. $oObj= $this->getSgbd()->findOneSimple($tSql,$this->sClassRow);
  109. /*LOG*/_root::getLog()->info('sql select:'.$this->getSgbd()->getRequete());
  110. return $oObj;
  111. }
  112. /**
  113. * retourne un tableau d'enregistrement
  114. * @access public
  115. * @param string $sRequete
  116. * @param undefined $uParam
  117. * @return un object
  118. */
  119. public function findOne(){
  120. $tSql=func_get_args();
  121. $oObj= $this->getSgbd()->findOne($tSql,$this->sClassRow);
  122. /*LOG*/_root::getLog()->info('sql select:'.$this->getSgbd()->getRequete());
  123. return $oObj;
  124. }
  125. /**
  126. * retourne un tableau d'enregistrement
  127. * @access public
  128. * @param string $sRequete
  129. * @param undefined $uParam
  130. * @return un tableau d'object
  131. */
  132. public function findMany(){
  133. $tSql=func_get_args();
  134. $tObj= $this->getSgbd()->findMany($tSql,$this->sClassRow);
  135. /*LOG*/_root::getLog()->info('sql select:'.$this->getSgbd()->getRequete());
  136. return $tObj;
  137. }
  138. /**
  139. * retourne un tableau d'enregistrement d'objet simple (plus rapide)
  140. * @access public
  141. * @param string $sRequete
  142. * @param undefined $uParam
  143. * @return un tableau d'object
  144. */
  145. public function findManySimple(){
  146. $tSql=func_get_args();
  147. $tObj= $this->getSgbd()->findManySimple($tSql,$this->sClassRow);
  148. /*LOG*/_root::getLog()->info('sql select:'.$this->getSgbd()->getRequete());
  149. return $tObj;
  150. }
  151. /**
  152. * execute une requete
  153. * @access public
  154. * @param string $sRequete
  155. * @param undefined $uParam
  156. */
  157. public function execute(){
  158. $tSql=func_get_args();
  159. $handle=$this->getSgbd()->execute($tSql);
  160. /*LOG*/_root::getLog()->info('sql execute:'.$this->getSgbd()->getRequete());
  161. return $handle;
  162. }
  163. /**
  164. * met a jour un enregistrement
  165. * @access public
  166. * @param object $oRow
  167. */
  168. public function update($oRow){
  169. $this->getSgbd()->update($this->sTable,$oRow->getToUpdate(),$oRow->getWhere());
  170. /*LOG*/_root::getLog()->info('sql update:'.$this->getSgbd()->getRequete());
  171. }
  172. /**
  173. * ajoute un enregistrement
  174. * @access public
  175. * @param object $oRow
  176. */
  177. public function insert($oRow){
  178. $oInsert= $this->getSgbd()->insert($this->sTable,$oRow->getToUpdate());
  179. /*LOG*/_root::getLog()->info('sql insert:'.$this->getSgbd()->getRequete());
  180. return $oInsert;
  181. }
  182. /**
  183. * supprime un enregistrement
  184. * @access public
  185. * @param object $oRow
  186. */
  187. public function delete($oRow){
  188. $this->getSgbd()->delete($this->sTable,$oRow->getWhere());
  189. /*LOG*/_root::getLog()->info('sql delete:'.$this->getSgbd()->getRequete());
  190. }
  191. /**
  192. * retourne la requete
  193. * @access public
  194. * @return string requete
  195. */
  196. public function getRequete(){
  197. return $this->getSgbd()->getRequete();
  198. }
  199. public function getIdTab(){
  200. return $this->tId;
  201. }
  202. /**
  203. * retourne un tableau contenant les colonnes d'une table
  204. * @access public
  205. * @return array
  206. */
  207. public function getListColumn(){
  208. return $this->getSgbd()->getListColumn($this->sTable);
  209. }
  210. /**
  211. * retourne un tableau contenant les tables
  212. * @access public
  213. * @return array
  214. */
  215. public function getListTable(){
  216. return $this->getSgbd()->getListTable();
  217. }
  218.  
  219. public function getWhereFromTab($tWhere=null){
  220.  
  221. if($tWhere==null){
  222. return null;
  223. }
  224.  
  225. $sWhere='';
  226.  
  227. if(is_array($tWhere)){
  228. foreach($tWhere as $sVar => $sVal){
  229. $sWhere.=$sVar.'='.$this->quote($sVal).' AND';
  230. }
  231. return substr($sWhere,0,-3);
  232. }else{
  233. return $this->tId[0].'='.$tWhere;
  234. }
  235. }
  236.  
  237. protected static function _getInstance($class) {
  238. if (array_key_exists($class, self::$_tInstance) === false){
  239. self::$_tInstance[$class] = new $class();
  240. }
  241. return self::$_tInstance[$class];
  242. }
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement