MkFramework
 All Data Structures Functions
abstract_model.php
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 */
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 
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  }
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  }
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  }
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  }
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  }
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  }
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  }
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  }
187  public function delete($oRow){
188  $this->getSgbd()->delete($this->sTable,$oRow->getWhere());
189  /*LOG*/_root::getLog()->info('sql delete:'.$this->getSgbd()->getRequete());
190  }
196  public function getRequete(){
197  return $this->getSgbd()->getRequete();
198  }
199  public function getIdTab(){
200  return $this->tId;
201  }
207  public function getListColumn(){
208  return $this->getSgbd()->getListColumn($this->sTable);
209  }
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 }
static getConfigVar($sCatAndVar, $uDefaut=null)
Definition: class_root.php:654
static getLog()
Definition: class_root.php:610