public function testChildCountEager() { $s = new Kwf_Model_Select(); $s->whereId(1); $s->expr('parent_child_count'); $this->_modelChild->getRow($s)->parent_child_count; }
public function testSiblingEager() { $s = new Kwf_Model_Select(); $s->whereId(1); $s->expr('sibling_value'); $row = $this->_modelChild->getRow($s); $this->assertEquals(105, $row->sibling_value); }
public function testChildCountExpression() { $s = new Kwf_Model_Select(); $s->expr('child_count'); $s->whereId(1); $row = $this->_modelFoo->getRow($s); $this->assertEquals(2, $row->child_count); }
public function testGetRow() { $model = $this->_m; $model->getRow(1)->delete(); $this->assertEquals(null, $model->getRow(1)); $s = new Kwf_Model_Select(array('ignoreDeleted' => true)); $s->whereId(1); $this->assertEquals(1, count($model->getRow($s))); }
public function testParentExpr() { $s = new Kwf_Model_Select(); $s->whereId(1); $row = $this->_modelChild->getRow($s); $this->assertEquals($row->parent_foo, 5); $s2 = clone $s; $s2->expr('parent_foo'); $row = $this->_modelChild->getRow($s2); $this->assertEquals($row->parent_foo, 5); }
public function testParentExpr() { $modelChild = Kwf_Model_Abstract::getInstance('Kwf_Model_FnF_ParentExprDeletedFlag_ChildModel'); $s = new Kwf_Model_Select(); $s->whereId(1); $row = $modelChild->getRow($s); $this->assertEquals($row->parent_foo, 5); $s2 = clone $s; $s2->expr('parent_foo'); $row = $modelChild->getRow($s2); $this->assertEquals($row->parent_foo, 5); }
public function testCountRows() { $fnf = new Kwf_Model_FnF(); $fnf->setData(array(array('id' => 2, 'name' => 'foo'), array('id' => 18, 'name' => 'bar'), array('id' => 456, 'name' => 'bar2'))); $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf)); $this->assertEquals(3, $proxy->countRows()); $select = new Kwf_Model_Select(); $select->whereId(2); $fnf = new Kwf_Model_FnF(); $fnf->setData(array(array('id' => 2, 'name' => 'foo'), array('id' => 18, 'name' => 'bar'), array('id' => 456, 'name' => 'bar2'))); $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf)); $this->assertEquals(1, $proxy->countRows($select)); }
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'))); }
/** * Should return the specified row, componentId and id has to be defined */ public function getRow($select) { $componentId = $this->_getComponentId($select); $masterId = $this->_getId($select); if ($componentId && $masterId) { $select = new Kwf_Model_Select(); $c = Kwf_Component_Data_Root::getInstance()->getComponentById($componentId, array('ignoreVisible' => true)); $select->whereEquals('component_id', $c->chained->dbId); $select->whereEquals('id', $masterId); $proxyRow = $this->_proxyModel->getRow($select); return $this->getRowByProxiedRow($proxyRow, $componentId); } else { $trlRow = $this->_trlModel->getRow($select); $masterSelect = new Kwf_Model_Select(); $masterSelect->whereId($trlRow->master_id); $masterRow = $this->_proxyModel->getRow($masterSelect); return $this->getRowByProxiedRow($masterRow, $trlRow->component_id); } throw new Kwf_Exception_NotYetImplemented(); }
public function onRowEvent(Kwf_Events_Event_Row_Abstract $ev) { $eventCls = get_class($ev); $unionModel = $this->_getModel(); $sourceRow = $ev->row; $sourceModel = $sourceRow->getModel(); foreach ($unionModel->getUnionModels() as $modelKey => $m) { if ($m === $sourceModel) { $s = new Kwf_Model_Select(); $s->whereId($modelKey . $sourceRow->{$sourceModel->getPrimaryKey()}); $s->ignoreDeleted(); $unionRow = $unionModel->getRow($s); if (!$unionRow) { $unionRow = $unionModel->_getRowById($modelKey . $sourceRow->{$sourceModel->getPrimaryKey()}); $this->fireEvent(new Kwf_Events_Event_Row_Deleted($unionRow)); } else { $this->fireEvent(new $eventCls($unionRow)); } return; } } throw new Kwf_Exception("Didn't find sourceModel as unionModel"); }
private function _convertSelect($select, $modelKey, $m) { $s = new Kwf_Model_Select(); if ($p = $select->getPart(Kwf_Model_Select::WHERE_ID)) { if (substr($p, 0, strlen($modelKey)) == $modelKey) { $s->whereId(substr($p, strlen($modelKey))); } else { return null; } } if ($p = $select->getPart(Kwf_Model_Select::WHERE_EQUALS)) { foreach ($p as $f => $v) { if ($f == 'id') { if (is_array($v)) { $filterV = array(); foreach ($v as $i) { if (substr($i, 0, strlen($modelKey)) == $modelKey) { $filterV[] = substr($i, strlen($modelKey)); } } if (!$filterV) { return null; } $v = $filterV; } else { if (substr($v, 0, strlen($modelKey)) == $modelKey) { $v = substr($v, strlen($modelKey)); } else { return null; } } } if (in_array($f, $this->_getOwnColumns())) { $f = $this->_mapColumn($m, $f); $s->whereEquals($f, $v); } } } if ($p = $select->getPart(Kwf_Model_Select::WHERE_NOT_EQUALS)) { foreach ($p as $f => $v) { if ($f == 'id') { if (substr($v, 0, strlen($modelKey)) != $modelKey) { continue; } else { $v = substr($v, strlen($modelKey)); } } if (in_array($f, $this->_getOwnColumns())) { $f = $this->_mapColumn($m, $f); $s->whereNotEquals($f, $v); } } } if ($p = $select->getPart(Kwf_Model_Select::WHERE_NULL)) { foreach ($p as $f) { if (in_array($f, $this->_getOwnColumns())) { $f = $this->_mapColumn($m, $f); $s->whereNull($f); } } } if ($p = $select->getPart(Kwf_Model_Select::WHERE_EXPRESSION)) { foreach ($p as $expr) { $e = $this->_convertExpr($expr, $modelKey, $m); if ($e) { $s->where($e); } } } if ($p = $select->getPart(Kwf_Model_Select::IGNORE_DELETED)) { $s->ignoreDeleted($p); } if (isset($this->_mergeSelects[$modelKey])) { $s->merge($this->_mergeSelects[$modelKey]); } return $s; }
public function getExprValue($row, $name) { if ($name instanceof Kwf_Model_Select_Expr_Interface) { $expr = $name; } else { $expr = $this->_exprs[$name]; } if ($expr instanceof Kwf_Model_Select_Expr_Child_Contains) { if (!$row instanceof Kwf_Model_Row_Interface) { $row = $this->getRow($row[$this->getPrimaryKey()]); } $ret = (bool) $row->countChildRows($expr->getChild(), $expr->getSelect()); return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_Child_First) { if (!$row instanceof Kwf_Model_Row_Interface) { $row = $this->getRow($row[$this->getPrimaryKey()]); } $s = clone $expr->getSelect(); $s->limit(1); $childRows = $row->getChildRows($expr->getChild(), $s); if (count($childRows)) { return $childRows[0]->{$expr->getField()}; } else { return null; } } else { if ($expr instanceof Kwf_Model_Select_Expr_Child) { if (!$row instanceof Kwf_Model_Row_Interface) { $row = $this->getRow($row[$this->getPrimaryKey()]); } $childs = $row->getChildRows($expr->getChild(), $expr->getSelect()); return self::_evaluateExprForRowset($childs, $expr->getExpr()); } else { if ($expr instanceof Kwf_Model_Select_Expr_Parent) { if (!$row instanceof Kwf_Model_Row_Interface) { $row = $this->getRow($row[$this->getPrimaryKey()]); } $reference = $row->getModel()->getReference($expr->getParent()); $parentModel = $row->getModel()->getReferencedModel($expr->getParent()); $select = new Kwf_Model_Select(); $select->whereId($row->{$reference['column']}); $select->ignoreDeleted(true); $parent = $parentModel->getRow($select); if (!$parent) { return null; } $field = $expr->getField(); if (is_string($field)) { return $parent->{$field}; } else { return $this->getExprValue($parent, $field); } } else { if ($expr instanceof Kwf_Model_Select_Expr_Concat) { $ret = ''; foreach ($expr->getExpressions() as $e) { if ($e instanceof Kwf_Model_Select_Expr_Interface) { $ret .= $this->getExprValue($row, $e); } else { if (is_array($row)) { $ret .= $row[$e]; } else { $ret .= $row->{$e}; } } } return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_String) { return $expr->getString(); } else { if ($expr instanceof Kwf_Model_Select_Expr_Boolean) { return $expr->getValue(); } else { if ($expr instanceof Kwf_Model_Select_Expr_Integer) { return $expr->getValue(); } else { if ($expr instanceof Kwf_Model_Select_Expr_StrPad) { $f = $expr->getField(); if (is_array($row)) { $v = $row[$f]; } else { $v = $row->{$f}; } // faking mysql's implementation of LPAD / RPAD // mysql cuts always right when the string is too long, it does not // depend on the pad-type if ($expr->getPadLength() < mb_strlen($v)) { return substr($v, 0, $expr->getPadLength()); } $padType = STR_PAD_RIGHT; if ($expr->getPadType() == Kwf_Model_Select_Expr_StrPad::LEFT) { $padType = STR_PAD_LEFT; } else { if ($expr->getPadType() == Kwf_Model_Select_Expr_StrPad::RIGHT) { $padType = STR_PAD_RIGHT; } } return str_pad($v, $expr->getPadLength(), $expr->getPadStr(), $padType); } else { if ($expr instanceof Kwf_Model_Select_Expr_Date_Year) { $field = $expr->getField(); $format = $expr->getFormat(); if (is_array($row)) { $v = $row[$field]; } else { $v = $row->{$field}; } return date($format, strtotime($v)); } else { if ($expr instanceof Kwf_Model_Select_Expr_Date_Format) { $field = $expr->getField(); $format = $expr->getFormat(); if (is_array($row)) { $v = $row[$field]; } else { $v = $row->{$field}; } return date($format, strtotime($v)); } else { if ($expr instanceof Kwf_Model_Select_Expr_Field) { $f = $expr->getField(); if (is_array($row)) { return $row[$f]; } else { return $row->{$f}; } } else { if ($expr instanceof Kwf_Model_Select_Expr_PrimaryKey) { $f = $this->getPrimaryKey(); if (is_array($row)) { return $row[$f]; } else { return $row->{$f}; } } else { if ($expr instanceof Kwf_Model_Select_Expr_SumFields) { $ret = 0; foreach ($expr->getFields() as $f) { if (is_int($f)) { $ret += $f; } else { if (is_string($f)) { if (is_array($row)) { $ret += $row[$f]; } else { $ret += $row->{$f}; } } else { if ($f instanceof Kwf_Model_Select_Expr_Interface) { $ret += $this->getExprValue($row, $f); } else { throw new Kwf_Exception_NotYetImplemented(); } } } } return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_If) { if ($this->getExprValue($row, $expr->getIf())) { return $this->getExprValue($row, $expr->getThen()); } else { return $this->getExprValue($row, $expr->getElse()); } } else { if ($expr instanceof Kwf_Model_Select_Expr_IsNull) { if ($expr->getField() instanceof Kwf_Model_Select_Expr_Interface) { $rowValue = $this->getExprValue($row, $expr->getField()); } else { $rowValue = $row->{$expr->getField()}; } return is_null($rowValue); } else { if ($expr instanceof Kwf_Model_Select_Expr_Not) { return !$this->getExprValue($row, $expr->getExpression()); } else { if ($expr instanceof Kwf_Model_Select_Expr_Position) { $f = $expr->getField(); $s = $this->select(); foreach ($expr->getGroupBy() as $i) { $s->whereEquals($i, $row->{$i}); } if ($expr->getDirection() == Kwf_Model_Select_Expr_Position::DIRECTION_ASC) { $s->where(new Kwf_Model_Select_Expr_Lower($f, $row->{$f})); } else { $s->where(new Kwf_Model_Select_Expr_Higher($f, $row->{$f})); } return $this->countRows($s) + 1; } elseif ($expr instanceof Kwf_Model_Select_Expr_Date_Age) { $f = $expr->getField(); if (!$row->{$f}) { return null; } $timeFrom = strtotime($row->{$f}); $timeTo = $expr->getDate()->getTimestamp(); $ret = date('Y', $timeTo) - date('Y', $timeFrom) - intval(date('md', $timeTo) < date('md', $timeFrom)); return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_Divide) { $ret = null; foreach ($expr->getExpressions() as $e) { $value = $this->getExprValue($row, $e); if ($ret === null) { $ret = $value; } else { if ($value == 0) { //throw new Kwf_Exception('division by 0 not possible, check you expressions'); } else { $ret = $ret / $value; } } } if (!$ret) { $ret = 0; } return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_Multiply) { $ret = null; foreach ($expr->getExpressions() as $e) { $value = $this->getExprValue($row, $e); if ($ret === null) { $ret = $value; } else { $ret *= $value; } } if (!$ret) { $ret = 0; } return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_Subtract) { $ret = null; foreach ($expr->getExpressions() as $e) { $value = $this->getExprValue($row, $e); if ($ret === null) { $ret = $value; } else { $ret -= $value; } } if (!$ret) { $ret = 0; } return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_Add) { $ret = 0; foreach ($expr->getExpressions() as $e) { $ret += $this->getExprValue($row, $e); } return $ret; } else { if ($expr instanceof Kwf_Model_Select_Expr_And) { foreach ($expr->getExpressions() as $e) { if (!$this->getExprValue($row, $e)) { return false; } } return true; } else { if ($expr instanceof Kwf_Model_Select_Expr_Or) { foreach ($expr->getExpressions() as $e) { $value = $this->getExprValue($row, $e); if ($this->getExprValue($row, $e)) { return true; } } return false; } else { if ($expr instanceof Kwf_Model_Select_Expr_CompareField_Abstract) { $value = $expr->getFormattedValue(); if ($value instanceof Kwf_Model_Select_Expr_Interface) { $value = $this->getExprValue($row, $value); } if ($expr->getField() instanceof Kwf_Model_Select_Expr_Interface) { $rowValue = $this->getExprValue($row, $expr->getField()); } else { $rowValue = $row->{$expr->getField()}; } if ($expr instanceof Kwf_Model_Select_Expr_Higher) { return $rowValue > $value; } else { if ($expr instanceof Kwf_Model_Select_Expr_Lower) { return $rowValue < $value; } else { if ($expr instanceof Kwf_Model_Select_Expr_HigherEqual) { return $rowValue >= $value; } else { if ($expr instanceof Kwf_Model_Select_Expr_Equal) { return $rowValue == $value; } else { if ($expr instanceof Kwf_Model_Select_Expr_NotEquals) { return $rowValue != $value; } else { if ($expr instanceof Kwf_Model_Select_Expr_LowerEqual) { return $rowValue <= $value; } else { throw new Kwf_Exception_NotYetImplemented("CompareField-Expression '" . (is_string($expr) ? $expr : get_class($expr)) . "' is not yet implemented"); } } } } } } } else { if ($expr instanceof Kwf_Model_Select_Expr_Parent_Contains) { if (!$row instanceof Kwf_Model_Row_Interface) { $row = $this->getRow($row[$this->getPrimaryKey()]); } $refModel = $this->getReferencedModel($expr->getParent()); $ref = $this->getReference($expr->getParent()); return in_array($row->{$ref['column']}, $refModel->getIds($expr->getSelect())); } else { throw new Kwf_Exception_NotYetImplemented("Expression '" . (is_string($expr) ? $expr : get_class($expr)) . "' is not yet implemented"); } } } } } } } } } } } } } } } } } } } } } } } } } } }
public function testCountSelectWhereId() { $s = new Kwf_Model_Select(); $s->whereId('1m1'); $this->assertEquals(1, $this->_m->countRows($s)); }