Redjik
Джедай-мастер
PHP:
<?php
/**
* Обертка для создания 1го запроса ко всем полям.
* Ограничение в 63 Join никто не отменял
* => максимум 63 тв поля
* @author Иван aka Redjik
*
*/
class DB_wrapper {
private $modx;
private $db;
protected static $instance;
public $criteria;
/**
* @param DocumentParser $modx
*/
private function __construct(DocumentParser $modx){
$this->modx = $modx;
$this->db = $modx->db;
}
public static function init(DocumentParser $modx){
if ( is_null(self::$instance) ) {
self::$instance = new DB_wrapper($modx);
}
self::$instance->criteria = null;
return self::$instance;
}
/**
* Returns all records accroding to $where criteria.
* @param int $template_id
* @param array $where
* @return array
* NB! excludes all inappropriate chars in TV names
*/
public function getAll($template_id){
$modx = $this->modx;
$tvs = $this->getTVsFromTemplate($template_id);
$tvSQL = $this->prepareTvsForSQL($tvs);
$whereSQL = $this->prepareCriteria();
$sql = 'SELECT main.*'.$tvSQL['selectString'].' FROM '.$modx->getFullTableName('site_content').'AS main';
$sql .= $tvSQL['joinString'];
$sql .= $whereSQL;
$result = $modx->db->query($sql);
self::$instance->criteria = null;
return $modx->db->makeArray($result);
}
/**
* Get all tvs for selected template
* @param int|string $template_id
* @return array
*/
public function getTVsFromTemplate($template_id){
$modx = $this->modx;
$sql = 'SELECT * FROM '.$modx->getFullTableName('site_tmplvars').'
LEFT JOIN '.$modx->getFullTableName('site_tmplvar_templates').'ON id = tmplvarid
WHERE templateid = '.(int)$template_id;
$result = $modx->db->query($sql);
return $modx->db->makeArray($result);
}
/**
* Prepares tvs for SQL
* @param array $tvs
* @return multiline:string
* Returns two string fields with 'selectString' and 'joinString' keys...
* Strings are passed in MySQL syntax style.
* NB! excludes all inappropriate chars in TV names
*/
private function prepareTvsForSQL(array $tvs){
$modx = $this->modx;
$selectString = '';
$joinString = '';
if (count($tvs)){
foreach ($tvs as $tv){
$tv['name'] = preg_replace('#[^A-Za-z0-9]#U', '', $tv['name']);
$selectString .= ', tv'.$tv['id'].'.value AS '.$tv['name'];
$joinString .= ' LEFT JOIN '.$modx->getFullTableName('site_tmplvar_contentvalues').' AS tv'.$tv['id'].' ON
(tv'.$tv['id'].'.tmplvarid = '.$tv['id'].' AND main.id = tv'.$tv['id'].'.contentid) ';
}
}
return array('selectString'=>$selectString, 'joinString'=>$joinString);
}
/**
* Prepares criteria for SQL
*/
private function prepareCriteria(){
$where = ' WHERE ';
$criteria = $this->criteria;
if (count($criteria)){
foreach ($criteria as $key => $groupArrays){
if ($key != 0){
$where.= '(';
}
foreach ($groupArrays as $secondKey => $groupArray){
if ($key == 0 && $secondKey == 0){
$where.= ' '.$groupArray[0];
} else {
$where.= ' '.$groupArray[1].' '.$groupArray[0];
}
}
if ($key != 0){
$where.= ')';
}
}
}else{
return $where.' 1';
}
return $where;
}
/**
* Adds where condition for SELECT
* @param string $string Condition to seacrh
* @param string $conjunction AND|OR
* @param int $group For grouping
* @return DB_wrapper
*/
public function addCondition($string,$conjunction = 'AND',$group = 0){
$this->criteria[$group][]=array($string,$conjunction);
return self::$instance;
}
}
Есть несколько вопросов...
1) Правильно ли я оформляю комментарии.
2) В статичных методах можно обращаться только к статичным свойствам?
3) Если ответ на второй вопрос да ... то вот это
PHP:
self::$instance->criteria = null;