PHP code example of kappa / doctrine

1. Go to this page and download the library: Download kappa/doctrine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kappa / doctrine example snippets



$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

$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 = $this->crudManagerFactory->create(new User());
// or
$crudManager = $this->crudManagerFactory->create('Some\Entity\User');

$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());

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

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



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**



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;
	}