From 69ce6a8538b02f923933f03c12cfd9fa360b2833 Mon Sep 17 00:00:00 2001 From: Christian Klauenbosch Date: Wed, 12 Apr 2017 09:15:54 +0200 Subject: [PATCH 1/3] improve each() --- .../Services/Query/Builder.php | 25 ++++++++++-- .../Tests/Services/Query/BuilderTest.php | 40 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/ChrisAndChris/Common/RowMapperBundle/Services/Query/Builder.php b/src/ChrisAndChris/Common/RowMapperBundle/Services/Query/Builder.php index 6adf77a..092baba 100644 --- a/src/ChrisAndChris/Common/RowMapperBundle/Services/Query/Builder.php +++ b/src/ChrisAndChris/Common/RowMapperBundle/Services/Query/Builder.php @@ -825,12 +825,31 @@ public function combine($builder) return $this; } - public function each(array $items, \Closure $callable) + /** + * Iterate over $items calling $callable + * + * The callable gets either $item or [$key, $value] as param 0 + * and bool $hasMoreItems as param 1 + * + * @param $items + * @param \Closure $callable a closure to call + * @param bool $keyValue if set to true, the closure gets as 1. param [key, value] + * @return $this + */ + public function each($items, \Closure $callable, $keyValue = false) { $count = 0; - foreach ($items as $item) { + if (!is_array($items)) { + return $this; + } + + foreach ($items as $key => $value) { $count++; - $this->appendMultiple($callable($item, $count < count($items))); + if ($keyValue) { + $this->appendMultiple($callable([$key, $value], $count < count($items))); + } else { + $this->appendMultiple($callable($value, $count < count($items))); + } } return $this; diff --git a/src/ChrisAndChris/Common/RowMapperBundle/Tests/Services/Query/BuilderTest.php b/src/ChrisAndChris/Common/RowMapperBundle/Tests/Services/Query/BuilderTest.php index 570247d..4ee032e 100644 --- a/src/ChrisAndChris/Common/RowMapperBundle/Tests/Services/Query/BuilderTest.php +++ b/src/ChrisAndChris/Common/RowMapperBundle/Tests/Services/Query/BuilderTest.php @@ -488,6 +488,46 @@ function ($item, $isNotLast) { $this->assertEquals('`field` , `field` , `field`', $query->getQuery()); } + public function testEach_empty() + { + $builder = $this->getBuilder(); + + $builder->each( + null, + function ($item, $isNotLast) { + $builder = $this->getBuilder(); + $builder->field($item) + ->_if($isNotLast) + ->c() + ->_end(); + + return $builder; + } + ); + + $this->assertEquals(0, count($builder->getStatement())); + } + + public function testEach_notAnArray() + { + $builder = $this->getBuilder(); + + $builder->each( + 1, + function ($item, $isNotLast) { + $builder = $this->getBuilder(); + $builder->field($item) + ->_if($isNotLast) + ->c() + ->_end(); + + return $builder; + } + ); + + $this->assertEquals(0, count($builder->getStatement())); + } + public function testValues() { $builder = $this->getBuilder(); From 8e7b286982ba1e311481e40ff56999737fa0ad04 Mon Sep 17 00:00:00 2001 From: Christian Klauenbosch Date: Wed, 12 Apr 2017 09:23:07 +0200 Subject: [PATCH 2/3] changed package name, updated readme --- README.md | 16 ++++++++-------- composer.json | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 73be040..026933a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# A Query Builder for MySQL and Postgres - easy to use. - -[![Build Status](https://travis-ci.org/chrisandchris/symfony-rowmapper.svg?branch=master)](https://travis-ci.org/chrisandchris/symfony-rowmapper) -[![Code Climate](https://codeclimate.com/github/chrisandchris/symfony-rowmapper/badges/gpa.svg)](https://codeclimate.com/github/chrisandchris/symfony-rowmapper) -[![Test Coverage](https://codeclimate.com/github/chrisandchris/symfony-rowmapper/badges/coverage.svg)](https://codeclimate.com/github/chrisandchris/symfony-rowmapper/coverage) -[![Version](https://img.shields.io/packagist/v/chrisandchris/symfony-rowmapper.svg)](https://img.shields.io/packagist/v/chrisandchris/symfony-rowmapper.svg) -[![Downloads](https://img.shields.io/packagist/dt/chrisandchris/symfony-rowmapper.svg)](https://img.shields.io/packagist/dt/chrisandchris/symfony-rowmapper.svg) -[![Licence](https://img.shields.io/packagist/l/chrisandchris/symfony-rowmapper.svg)](https://img.shields.io/packagist/l/chrisandchirs/symfony-rowmapper.svg) +# A QueryBuilder and passive record ORM for Symfony2 + +[![Build Status](https://travis-ci.org/chrisandchris/passive-record-orm.svg?branch=master)](https://travis-ci.org/chrisandchris/passive-record-orm) +[![Code Climate](https://codeclimate.com/github/chrisandchris/passive-record-orm/badges/gpa.svg)](https://codeclimate.com/github/chrisandchris/passive-record-orm) +[![Test Coverage](https://codeclimate.com/github/chrisandchris/passive-record-orm/badges/coverage.svg)](https://codeclimate.com/github/chrisandchris/passive-record-orm/coverage) +[![Version](https://img.shields.io/packagist/v/chrisandchris/passive-record-orm.svg)](https://packagist.org/packages/chrisandchris/passive-record-orm) +[![Downloads](https://img.shields.io/packagist/dt/chrisandchris/passive-record-orm.svg)](https://packagist.org/packages/chrisandchris/passive-record-orm) +[![Licence](https://img.shields.io/packagist/l/chrisandchris/passive-record-orm.svg)](https://github.com/chrisandchris/passive-record-orm/blob/master/LICENSE) Despite it's package name, it's not simply a row mapper. And it's not simply for Symfony. This project is a *QueryBuilder* and a *Mapper for SQL Result Sets*, diff --git a/composer.json b/composer.json index caebd52..1abb8a8 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { - "name": "chrisandchris/symfony-rowmapper", + "name": "chrisandchris/passive-record-orm", "license": "GPL-2.0", "type": "project", - "description": "A RowMapper and QueryBuilder, makes querying databases easier and better than orm", + "description": "A database layer, passive record and orm abstraction layer for your php application. ", "keywords": [ "rowmapper", "database", From 4ff846e5566efb5c9fb5629c7ba46d7e7d013eaa Mon Sep 17 00:00:00 2001 From: Christian Klauenbosch Date: Wed, 12 Apr 2017 09:25:27 +0200 Subject: [PATCH 3/3] fixed issues in readme --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 026933a..7fab5a7 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,15 @@ class DemoRepo { } public function getCustomerName($customerId) { - $query = $this->model->getDependencyProvider->getBuilder->select() + $query = $this->model->getDependencyProvider()->getBuilder()->select() ->field('customer_name') ->table('customer') ->where() ->field('customer_id')->equals()->value($customerId) ->close() ->getSqlQuery(); - return $this->runWithFirstKeyFirstValue($query); + + return $this->model->runWithFirstKeyFirstValue($query); } } ``` @@ -85,14 +86,15 @@ class DemoRepo { } public function getCustomerName($customerId) { - $query = $this->model->getDependencyProvider->getBuilder->select() + $query = $this->model->getDependencyProvider()->getBuilder()->select() ->field('customer_name') ->table('customer') ->where() ->field('customer_id')->equals()->value($customerId) ->close() ->getSqlQuery(); - return $this->runWithFirstKeyFirstValue($query); + + return $this->model->runWithFirstKeyFirstValue($query); } } ``` @@ -128,7 +130,7 @@ class DemoModel { } public function getCustomer($customerId) { - $query = $this->model->getDependencyProvider()->getBuilder->select() + $query = $this->model->getDependencyProvider()->getBuilder()->select() ->fieldlist([ 'customer_id' => 'customerId', 'cus_name' => 'name', @@ -141,7 +143,8 @@ class DemoModel { ->field('customer_id')->equals()->value($customerId) ->close() ->getSqlQuery(); - return $this->run($query, new SomeEntity()); + + return $this->model->run($query, new SomeEntity()); } } ```