ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
AbstractEntity.php
1 <?php
31 {
35  protected $_adapter;
36 
40  protected $_generator;
41 
47  protected $_cleanSchema = array();
48 
54  abstract protected function _doReload();
55 
62  protected function _delete()
63  {
64  }
65 
72  protected function _postDelete()
73  {
74  }
75 
82  protected function _save()
83  {
84  }
85 
92  protected function _postSave()
93  {
94  }
95 
101  public function reload()
102  {
103  $this->_cleanSchema = array();
104 
105  if ($name = $this->getOriginName()) {
106  $this->_doReload();
107  }
108  return $this;
109  }
110 
117  public function refresh($param = null)
118  {
119  $this->reload();
120 
121  return parent::refresh($param);
122  }
123 
129  protected function _getCleanSchema()
130  {
131  return $this->_cleanSchema;
132  }
133 
141  public function __wakeup()
142  {
143  $this->reload();
144  }
145 
146 
153  public function exec($sql)
154  {
155  $queries = explode(';', $sql);
156  $queries = array_filter($queries);
157 
158  foreach ($queries as $query) {
159  $this->getAdapter()->exec($query);
160  }
161  return $this;
162  }
163 
169  public function save()
170  {
171  if ($this->isDirty()) {
172 
173  $this->_save();
174 
175  $this->exec($this->toSql($this->isExist()));
176 
177  $this->_postSave();
178 
179  $this->_name = $this->getName();
180 
181  $this->refresh();
182  return true;
183  }
184  return false;
185  }
186 
192  public function delete()
193  {
194  if (!$this->isExist()) {
195  throw new ZendDbSchema_Db_Schema_Exception('Cannot delete not existing schema');
196  }
197  $this->_delete();
198 
199  $this->exec($this->getDropSql());
200 
201  $this->_postDelete();
202 
203  $this->refresh();
204  return $this;
205  }
206 
212  public function getGenerator()
213  {
214  return $this->_generator;
215  }
216 
222  public function getAdapter()
223  {
224  return $this->_adapter;
225  }
226 
233  public static function getDefaultAdapter()
234  {
235  $defaultAdapter = Zend_Db_Table::getDefaultAdapter();
236  $config = $defaultAdapter->getConfig();
237  switch (true) {
238  case $defaultAdapter instanceof Zend_Db_Adapter_Mysqli:
239  $adapter = new ZendDbSchema_Db_Adapter_Mysqli($config);
240  break;
241  case $defaultAdapter instanceof Zend_Db_Adapter_Pdo_Mysql:
242  $adapter = new ZendDbSchema_Db_Adapter_Pdo_Mysql($config);
243  break;
244  default:
245  $adapterType = get_class($defaultAdapter);
247  "Adapter '$adapterType' not supported"
248  );
249  }
250  return $adapter;
251  }
252 }