Skip to content
This repository has been archived by the owner on Mar 2, 2021. It is now read-only.

improve each() #19

Merged
merged 3 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# A Query Builder for MySQL and Postgres - easy to use.
# A QueryBuilder and passive record ORM for Symfony2

[![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)
[![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*,
Expand All @@ -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);
}
}
```
Expand Down Expand Up @@ -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);
}
}
```
Expand Down Expand Up @@ -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',
Expand All @@ -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());
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down