2017 © Pedro Peláez
 

library doctrine

Collection of classes for better work with Doctrine

image

kappa/doctrine

Collection of classes for better work with Doctrine

  • Tuesday, November 24, 2015
  • by Budry
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,589 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 1 % Grown

The README.md

Build Status, (*1)

Kappa\Doctrine

Collection of classes for better work with Doctrine, (*2)

Requirements

Installation:

The best way to install Kappa\Doctrine is using Composer, (*3)

$ composer require kappa/doctrine:@dev

Usages

Converter::entityToArray()

Method entityToArray requires entity object and returns Kappa\Doctrine\Converters\EntityToArrayConverter., (*4)

  • setIgnoreList(array) - set list of items which you can ignore (ignore list and white list can be combined)
  • setWhiteList(array) - set list of items which you can transform (ignore list and white list can be combined)
  • addFieldResolver(column name, resolver) - you can set closure or concrete value for field
  • convert() - returns generated array

Example:, (*5)

<?php
$user = new User("Joe");
$user->setParent(new User("Joe senior"))
    ->setAge(50);
    ->setPrivate("private");
$array = $converter->entityToArray($user)
    ->setIgnoreList(["private"])
    ->addFieldResolver("age", 10)
    ->addFieldResolver("parent", function(User $parent) { return $parent->getName(); })
    ->convert();
echo $array['name']; // print Joe
echo $array['parent']; // print Joe senior
echo $array['age']; // print 10

Converter::arrayToEntity()

Method arrayToEntity requires two argument. First argument can be entity object or entity class name and returns Kappa\Doctrine\Converters\ArrayToEntityConverter., (*6)

  • setIgnoreList(array) - set list of items which you can ignore (ignore list and white list can be combined)
  • setWhiteList(array) - set list of items which you can transform (ignore list and white list can be combined)
  • addItemResolver(column name, resolver) - you can set closure or concrete value for item
  • convert() - returns generated array

Example:, (*7)

$data = [
    'name' => 'Joe',
    'age' => 50, 
    'parent' => 1,
    'sex' => 'male',
    'private' => 'text',
];
$entity = $converter->arrayToEntity('User', $data)
    ->setIgnoreList(['private'])
    ->setWhiteList(['age', 'name', 'private'])
    ->setItemResolver('parent', function ($parent) {
        return $this->dao->find($parent);
    })
    ->setItemResolver('sex', 'female')
    ->convert();
echo $entity->getName(); // print Joe
echo $entity->getSex(); // print female
$entity->getParent(); // returns instance of User

CrudManager

Recommended way for create instance of Kappa\Doctrine\Managers\CrudManager is use Kappa\Managers\CrudManagerFactory., (*8)

<?php
$crudManager = $this->crudManagerFactory->create(new User());
// or
$crudManager = $this->crudManagerFactory->create('Some\Entity\User');

Method create() requires only one argument which it can be instance of entity or full namespace name., (*9)

Created CrudManager contains three methods for basic works with entity., (*10)

  • create(array) - Create a new entity and fill with data
  • update(id, array) - Find entity by id and fill with data
  • delete(id) - Delete entity with id

FormItemsCreator

$form = new Form();
$form->addSelect('parent', 'Parent item: ', $this->formItemsCreator->create('\UserEntity', new GetAll());
// or
$user = new User();
$form->addSelect('parent', 'Parent item: ', $this->formItemsCreator->create($user, new GetAll());
$this->formItemsCreator->create('\UserEntity', new GetAll());

use default columns id and title and create array like this, (*11)

$array = [
    '1' => 'John'
];

You can change default columns via config, (*12)

doctrine:
    forms:
        items:
            identifierColumn: id
            valueColumn: name

or as a third and fourth argument, (*13)

$this->formItemsCreator->create('\UserEntity', new GetAll(), 'name', 'id');

Third argument is valueColumn and last argument is identifierColumn, (*14)

QueryExecutor

Time to time is needed run DQL query instead of manipulate with entity. Great way is build UPDATE (or DELETE) with QueryBuilder., (*15)

Is very useful to create a query object for such cases. In Doctrine and Kdyby\Doctrine you can create SELECT query and run with $this->repository->fetch(new QueryObject) but UPDATE or DELETE query is not supported. QueryExecutor is precisely for these situations., (*16)

Example:, (*17)

<?php

class ExecutableQuery implements Executable
{
    /**
     * @param QueryBuilder $queryBuilder
     * @return QueryBuilder
     */
    public function build(QueryBuilder $queryBuilder)
    {
        $queryBuilder->update('KappaTests\Mocks\FormItemsEntity', 'r')
            ->set('r.title', $queryBuilder->expr()->literal('UPDATED'))
            ->where('r.id = ?0')
            ->setParameters(1);

        return $queryBuilder;
    }
}

// and

$this->queryExecutor->execute(new ExecutableQuery());

## RouteParamsResolver

You can use `Kappa\Doctrine\Routes\RouteParamsResolver` for easy works with `FILTER_IN/OUT` in your routes

**Example**

```php
<?php

class Router
{
    private $paramsResolver;

    public function __construct(RouteParamsResolverFactory $factory)
    {
        $this->paramsResolver = $factory->create('App\Entities\Article');
    }

    /**
     * @return \Nette\Application\IRouter
     */
    public function createRouter()
    {
        $router = new RouteList();
        $router[] = new Route('<presenter>/<action>[/<id>', [
            'presenter' => 'Homepage',
            'action' => 'default',
            'id' => [
                Route::FILTER_IN => [$this->paramsResolver, 'filterIn'],
                Route::FILTER_IN => [$this->paramsResolver, 'filterOut']
            ]
        ]);

        return $router;
    }
}

The Versions

24/11 2015

dev-master

9999999-dev https://github.com/Kappa-org/Doctrine

Collection of classes for better work with Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

11/07 2014

v2.1.2

2.1.2.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

08/07 2014

v2.1.1

2.1.1.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

25/06 2014

v2.1.0

2.1.0.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

14/06 2014

v2.0.3

2.0.3.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

01/06 2014

v2.0.2

2.0.2.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

01/06 2014

v2.0.1

2.0.1.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

05/05 2014

v2.0.0

2.0.0.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

orm database framework doctrine nette kappa

08/03 2014

v1.0.0

1.0.0.0 https://github.com/Kappa-org/Doctrine

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

orm database framework doctrine nette kappa