function getCount() { if ($this->isFetched()) { return parent::getCount(); } $query = clone $this->getQuery(); $alias = get_class($this) . '_counter'; $query->get(Projection::count($this->getChildren()->getLogicalSchema()->getIdentifier(), $alias)); $sqlQuery = $this->makeSqlSelectQuery($query); $result = $this->getChildren()->getDao()->getRow($sqlQuery); $this->count = (int) $result[$alias]; return $this->count; }
public function testIpAddressProperty() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $city = TestCity::create()->setName('Khimki'); TestCity::dao()->add($city); $userWithIp = TestUser::create()->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setLastLogin(Timestamp::makeNow())->setRegistered(Timestamp::makeNow())->setCity($city)->setIp(IpAddress::create('127.0.0.1')); TestUser::dao()->add($userWithIp); $this->assertTrue($userWithIp->getId() >= 1); $this->assertTrue($userWithIp->getIp() instanceof IpAddress); $plainIp = DBPool::me()->getByDao(TestUser::dao())->queryColumn(OSQL::select()->get('ip')->from(TestUser::dao()->getTable())->where(Expression::eq('id', $userWithIp->getId()))); $this->assertEquals($plainIp[0], $userWithIp->getIp()->toString()); $count = Criteria::create(TestUser::dao())->add(Expression::eq('ip', IpAddress::create('127.0.0.1')))->addProjection(Projection::count('*', 'count'))->getCustom('count'); $this->assertEquals($count, 1); } }
public function testProperties() { $query = OQL::select('from TestUser'); $criteria = Criteria::create(TestUser::dao()); $this->assertCriteria($query, $criteria); $this->assertCriteria($query->addProperties(OQL::properties('id, count(id) as count')), $criteria->addProjection(Projection::property('id'))->addProjection(Projection::count('id', 'count'))); $this->assertCriteria($query->addProperties(OQL::properties('city.id')), $criteria->addProjection(Projection::property('city.id'))); $properties = OQL::properties('id'); $this->assertFalse($properties->isDistinct()); $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('id'))); $properties = OQL::properties('id, distinct name'); $this->assertTrue($properties->isDistinct()); $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('id'))->add(Projection::property('name'))); $properties = OQL::properties('$1')->bind(1, 'foo'); $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('foo'))); $properties->bind(1, 'bar'); $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('bar'))); $this->assertCriteria(OQL::select('from TestUser')->addProperties($properties->bind(1, 'foo'))->bind(1, 'bar'), Criteria::create(TestUser::dao())->addProjection(Projection::property('bar'))); $properties = OQL::properties('id, count(distinct city.id + $0), avg(some) as someAverage, ' . 'name not like "%Ы%", foo and (bar or baz), $1 / $2, ' . 'a in ($3, $0)')->bindAll(array(1, 2, 'num', 'test')); $this->assertFalse($properties->isDistinct()); $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('id'))->add(Projection::distinctCount(Expression::add('city.id', 1)))->add(Projection::avg('some', 'someAverage'))->add(Projection::property(Expression::notLike('name', '%Ы%')))->add(Projection::property(Expression::expAnd('foo', Expression::expOr('bar', 'baz'))))->add(Projection::property(Expression::div(2, 'num')))->add(Projection::property(Expression::in('a', array('test', 1))))); }
public function testQuery() { $criteria = Criteria::create(TestUser::dao())->setProjection(Projection::property('id'))->add(Expression::isTrue('id')); $this->assertCriteria('id from TestUser where id is true', $criteria)->assertCriteria('id from TestUser where id is true order by id asc', $criteria->addOrder(OrderBy::create('id')->asc()))->assertCriteria('id from TestUser where id is true order by id asc limit 10 offset 1', $criteria->setLimit(10)->setOffset(1))->assertCriteria('id from TestUser where id is true group by id order by id asc limit 10 offset 1', $criteria->setProjection(Projection::chain()->add(Projection::property('id'))->add(Projection::group('id'))))->assertCriteria('id from TestUser where id is true group by id order by id asc having id = 1 limit 10 offset 1', $criteria->setProjection(Projection::chain()->add(Projection::property('id'))->add(Projection::group('id'))->add(Projection::having(Expression::eq('id', 1)))))->assertCriteria('count(id) as count from TestUser group by id having count = 2', Criteria::create(TestUser::dao())->setProjection(Projection::chain()->add(Projection::count('id', 'count'))->add(Projection::group('id'))->add(Projection::having(Expression::eq('count', 2))))); }