Hola amigos foreros hoy me presento con una contribución para el poderoso zend framework, esperando así su retroalimentación y convertir este código en algo más poderoso, les comento rápido esta clase te permite dejar de usar métodos get en tus models, es decir a veces llenamos nuestros models de métodos basuras y/o sobrecargas innecesarias sólo para obtener un solo campo o infinidad de join's, dejando así un código muy largo que cuando analizas muchos son métodos de tipo get, esta clase te permite crear query's apartir de un solo array. les dejo el código y un ejemplo de su uso saludos.
Por cierto una ventaja de usar en tus models es que conservas todas las propiedades de zend_db_table es decir pueden crear sus inserts,updates y de mas cosas xD
Código PHP:
<?php
class factoryDB extends Zend_Db_Table_Abstract {
protected $_name;
protected $alias;
protected $joins;
protected $wheres;
protected $columns;
protected $orders;
protected $groups;
protected $havings;
protected $limits;
protected $typeFetch;
public $typeReturn;
/**
* @param array(Alias => Table_name) | Table_name, fetchAll | fetchRow , array|object
* @return array|object
*/
public function __construct($tableName, $typeFetch = 'fetchAll', $typeReturn = 'object') {
$this->alias = $tableName;
$this->_name = (is_array($tableName)) ? array_shift($tableName) : $tableName;
$this->typeFetch = $typeFetch;
$this->typeReturn = $typeReturn;
parent::__construct();
}
public function __get($var) {
return $this->$var;
}
public function __set($name, $value) {
$this->$name = $value;
}
/**
* @param array $sql
* @param bolean True si quieres imprimir el assemble
* @return mixed
*/
public function getData($sql) {
/*
* $sql = array(
'columns' => array('tabla.campo1','tabla.campo2'),
'joins' => array(
array('tipo' => 'joinInner','nombre' => array('Alias2' => 'Table_name'),'condicion' => 'Alias1.campo = Alias2.campo'),
array('tipo' => 'joinLeft','nombre' => array('Alias2' => 'Table_name'),'condicion' => 'Alias1.campo = Alias2.campo'),
'wheres' => array(where_clause, where_clause),
'limits' => array('limit' => 10, 'start' => 0)
);
*/
if (!is_array($sql)) {
return false;
}
foreach ($sql AS $key => $value) {
if (is_array($value) && count($value) > 0) {
$this->__set($key, $value);
}
}
$query = parent::select()->setIntegrityCheck(false);
if(is_array($this->columns)){
$query->from($this->alias,$this->columns);
} else {
$query->from($this->alias);
}
//array('joins' => array('joinInner' => array('nombre' => array('a' => 'table'),'condicion' => 'llave1 = llave2'), 'joinLeft' => array(array('a' => 'table'),'llave1','llave2')));
if (is_array($this->joins)) {
foreach ($this->joins AS $tables){
$query->$tables['tipo']($tables['nombre'], $tables['condicion'],'');
}
}
//array('wheres' => array('a.id = 2'));
if(is_array($this->wheres)){
foreach($this->wheres AS $value){
$query->where($value);
}
}
//array('groups' => 'string');
//array('groups' => array());
if(!empty($this->groups)){
$query->group($this->groups);
}
//array('havings' => 'string');
if(!empty($this->havings)){
$query->having($this->havings);
}
//array('orders' => 'string');
if(!empty($this->orders)){
$query->order($this->orders);
}
//array('limits' => array('limit' => 10,'start' => 0))
if(!empty($this->limits)){
$query->limit($this->limits['limit'], (!empty($this->limits['start'])) ? $this->limits['start'] : 0 );
}
$fetch = $this->typeFetch;
$result = $this->$fetch($query);
if(is_object($result)){
$return = $this->typeReturn;
if($return == 'array'){
return $result->toArray();
}else{
return $result;
}
} else {
return false;
}
}
}
Quedando así su uso:
Código PHP:
<?php
$factory = new factoryDB(array('a' => 'tabla_a'), 'fetchAll', 'array');
$query = array(
'columns' => array('a.campo_1', 'b.campo_2', 'c.campo_3', 'd.campo_4'),
'joins' => array(
array('tipo' => 'joinInner', 'nombre' => array('b' => 'tabla_b'), 'condicion' => 'a.campo_1 = b.campo_2'),
array('tipo' => 'joinLeft', 'nombre' => array('c' => 'tabla_c'), 'condicion' => 'b.campo_2 = c.campo_3'),
array('tipo' => 'joinRight', 'nombre' => array('d' => 'tabla_d', 'condicion' => 'c.campo_3 = d.campo_4'))
),
'wheres' => array('a.campo_1 = 1', 'd.campo_4 <> "algo"'),
'groups' => 'a.campo_1'
);
$factory->getData($query);