예제 #1
0
 public function getRow($select)
 {
     if (!is_object($select)) {
         $select = new Kwf_Model_Select($select);
     }
     $id = null;
     if ($select->getPart(Kwf_Model_Select::WHERE_EQUALS)) {
         foreach ($select->getPart(Kwf_Model_Select::WHERE_EQUALS) as $k => $i) {
             if ($k == 'id') {
                 $id = $i;
             }
         }
     }
     $componentId = $this->_getComponentId($select);
     if ($id && !$componentId) {
         //only id passed, in detail form controller
         $c = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($id, array('ignoreVisible' => true));
         $select->whereEquals('component_id', $c->parent->chained->dbId);
         $select->whereEquals('id', $c->id);
         $componentId = $c->parent->dbId;
     } else {
         if ($componentId && $id) {
             $c = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($componentId, array('ignoreVisible' => true));
             $select->whereEquals('component_id', $c->chained->dbId);
             $select->whereEquals('id', $id);
         } else {
             throw new Kwf_Exception("invalid select");
         }
     }
     $proxyRow = $this->_proxyModel->getRow($select);
     return $this->getRowByProxiedRow($proxyRow, $componentId);
 }
예제 #2
0
 public function testSelect()
 {
     $select = new Kwf_Model_Select();
     $select->where('foo = ?', 1);
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE), array(array('foo = ?', 1, null)));
     $select->where('bar = ?', 1);
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE), array(array('foo = ?', 1, null), array('bar = ?', 1, null)));
     $select->whereEquals('foo', 1);
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE_EQUALS), array('foo' => 1));
     $select->whereEquals('foo', 'bar');
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE_EQUALS), array('foo' => 'bar'));
     $select->whereEquals('foo2', 'bar2');
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE_EQUALS), array('foo' => 'bar', 'foo2' => 'bar2'));
     $select->order('foo');
     $this->assertEquals($select->getPart(Kwf_Model_Select::ORDER), array(array('field' => 'foo', 'direction' => 'ASC')));
     $select->order('foo2', 'DESC');
     $this->assertEquals($select->getPart(Kwf_Model_Select::ORDER), array(array('field' => 'foo', 'direction' => 'ASC'), array('field' => 'foo2', 'direction' => 'DESC')));
     $select->limit(10);
     $this->assertEquals($select->getPart(Kwf_Model_Select::LIMIT_COUNT), 10);
     $this->assertEquals($select->getPart(Kwf_Model_Select::LIMIT_OFFSET), null);
     $select->limit(20);
     $this->assertEquals($select->getPart(Kwf_Model_Select::LIMIT_COUNT), 20);
     $select->limit(25, 10);
     $this->assertEquals($select->getPart(Kwf_Model_Select::LIMIT_COUNT), 25);
     $this->assertEquals($select->getPart(Kwf_Model_Select::LIMIT_OFFSET), 10);
     $this->assertEquals(count($select->getParts()), 5);
     $select = new Kwf_Model_Select(array('id' => 1));
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE_ID), 1);
     $select->whereId(10);
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE_ID), 10);
     $select->whereId(11);
     $this->assertEquals($select->getPart(Kwf_Model_Select::WHERE_ID), 11);
     $select = new Kwf_Model_Select();
     $select->order(Kwf_Model_Select::ORDER_RAND);
     $this->assertEquals($select->getPart(Kwf_Model_Select::ORDER), array(array('field' => Kwf_Model_Select::ORDER_RAND, 'direction' => 'ASC')));
 }
예제 #3
0
 private function _getQuery(Kwf_Model_Select $select)
 {
     $where = array();
     if ($equals = $select->getPart(Kwf_Model_Select::WHERE_EQUALS)) {
         foreach ($equals as $k => $v) {
             $where[$k] = $v;
         }
     }
     if ($exprs = $select->getPart(Kwf_Model_Select::WHERE_EXPRESSION)) {
         foreach ($exprs as $e) {
             if ($e instanceof Kwf_Model_Select_Expr_Equals) {
                 $where[$e->getField()] = $e->getValue();
             } else {
                 if ($e instanceof Kwf_Model_Select_Expr_NotEquals) {
                     $where[$e->getField()]['$ne'] = $e->getValue();
                 } else {
                     if ($e instanceof Kwf_Model_Select_Expr_HigherDate) {
                         $where[$e->getField()]['$gt'] = new MongoDate(strtotime($e->getValue()));
                     } else {
                         if ($e instanceof Kwf_Model_Select_Expr_SmallerDate) {
                             $where[$e->getField()]['$lt'] = new MongoDate(strtotime($e->getValue()));
                         } else {
                             if ($e instanceof Kwf_Model_Select_Expr_HigherEqualDate) {
                                 $where[$e->getField()]['$gte'] = new MongoDate(strtotime($e->getValue()));
                             } else {
                                 if ($e instanceof Kwf_Model_Select_Expr_SmallerEqualDate) {
                                     $where[$e->getField()]['$lte'] = new MongoDate(strtotime($e->getValue()));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($id = $select->getPart(Kwf_Model_Select::WHERE_ID)) {
         $where['id'] = $id;
         //TODO dynam.
     }
     return $where;
 }
예제 #4
0
 protected function _selectData(Kwf_Model_Select $select, array $row)
 {
     if ($where = $select->getPart(Kwf_Model_Select::WHERE_EQUALS)) {
         foreach ($where as $f => $v) {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $rv = $this->_rowValue($f, $row);
             if (is_null($rv)) {
                 return false;
             }
             if (!in_array($rv, $v)) {
                 return false;
             }
         }
     }
     if ($where = $select->getPart(Kwf_Model_Select::WHERE_EXPRESSION)) {
         foreach ($where as $expr) {
             if (!$this->_checkExpressions($expr, $row)) {
                 return false;
             }
         }
     }
     if ($where = $select->getPart(Kwf_Model_Select::WHERE_NOT_EQUALS)) {
         $foundOneMatching = false;
         foreach ($where as $f => $v) {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $rv = $this->_rowValue($f, $row);
             if ($rv && in_array($rv, $v)) {
                 $foundOneMatching = true;
                 break;
             }
         }
         if ($foundOneMatching) {
             return false;
         }
     }
     if ($where = $select->getPart(Kwf_Model_Select::WHERE_NULL)) {
         foreach ($where as $f) {
             $rv = $this->_rowValue($f, $row);
             if (!is_null($rv)) {
                 return false;
             }
         }
     }
     return true;
 }
예제 #5
0
 protected function _applySelect(Zend_Db_Select $dbSelect, Kwf_Model_Select $select)
 {
     if ($dbSelect instanceof Zend_Db_Table_Select) {
         $dbSelect->setIntegrityCheck(false);
     }
     if ($whereEquals = $select->getPart(Kwf_Model_Select::WHERE_EQUALS)) {
         foreach ($whereEquals as $field => $value) {
             if (is_array($value)) {
                 if ($value) {
                     foreach ($value as &$v) {
                         if (!is_int($v)) {
                             $v = $this->_fixStupidQuoteBug($v);
                             $v = $this->getAdapter()->quote($v);
                         }
                     }
                     $value = implode(', ', $value);
                     $dbSelect->where($this->_formatField($field, $dbSelect) . " IN ({$value})");
                 } else {
                     $dbSelect->where('0');
                 }
             } else {
                 $value = $this->_fixStupidQuoteBug($value);
                 $dbSelect->where($this->_formatField($field, $dbSelect) . " = ?", $value);
             }
         }
     }
     if ($whereNotEquals = $select->getPart(Kwf_Model_Select::WHERE_NOT_EQUALS)) {
         foreach ($whereNotEquals as $field => $value) {
             if (is_array($value)) {
                 foreach ($value as &$v) {
                     if (!is_int($v)) {
                         $v = $this->_fixStupidQuoteBug($v);
                         $v = $this->getAdapter()->quote($v);
                     }
                 }
                 $value = implode(', ', $value);
                 $dbSelect->where($this->_formatField($field, $dbSelect) . " NOT IN ({$value})");
             } else {
                 $value = $this->_fixStupidQuoteBug($value);
                 $dbSelect->where($this->_formatField($field, $dbSelect) . " != ?", $value);
             }
         }
     }
     if ($this->_hasDeletedFlag && !$select->getPart(Kwf_Model_Select::IGNORE_DELETED)) {
         $dbSelect->where("deleted = 0");
     }
     if ($where = $select->getPart(Kwf_Model_Select::WHERE)) {
         foreach ($where as $w) {
             $dbSelect->where($w[0], $w[1], $w[2]);
         }
     }
     if ($whereId = $select->getPart(Kwf_Model_Select::WHERE_ID)) {
         $whereId = $this->_fixStupidQuoteBug($whereId);
         $dbSelect->where($this->_formatField($this->getPrimaryKey(), $dbSelect) . " = ?", $whereId);
     }
     if ($whereNull = $select->getPart(Kwf_Model_Select::WHERE_NULL)) {
         foreach ($whereNull as $field) {
             $dbSelect->where("ISNULL(" . $this->_formatField($field, $dbSelect) . ")");
         }
     }
     if ($other = $select->getPart(Kwf_Model_Select::OTHER)) {
         foreach ($other as $i) {
             call_user_func_array(array($dbSelect, $i['method']), $i['arguments']);
         }
     }
     if ($whereExpression = $select->getPart(Kwf_Model_Select::WHERE_EXPRESSION)) {
         foreach ($whereExpression as $expr) {
             $expr->validate();
             $dbSelect->where($this->_createDbSelectExpression($expr, $dbSelect));
         }
     }
     if ($exprs = $select->getPart(Kwf_Model_Select::EXPR)) {
         foreach ($exprs as $field) {
             if (!($col = $this->_formatField($field, $dbSelect))) {
                 throw new Kwf_Exception("Expression '{$field}' not found");
             }
             $dbSelect->from(null, array($field => new Zend_Db_Expr($col)));
         }
     }
 }