1. Go to this page and download the library: Download pecinaon/doctrine-mapper 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/ */
pecinaon / doctrine-mapper example snippets
namespace App;
class ExampleDateFormat implements \DoctrineMapper\Parsers\Date\IDateFormat
{
/**
* Return date and time format
*
* @return string
*/
public function getDateTimeFormat()
{
return "H:i:s";
}
/**
* Return date format
*
* @return string
*/
public function getDateFormat()
{
return "d/m.y";
}
}
namespace App;
use DoctrineMapper\ArrayAccessEntityMapper;
use Nette\Application\UI\Form;
use Doctrine\ORM\Mapping as ORM;
use Nette\Object;
use Nette\Utils\ArrayHash;
/**
* Example Entity
*
* @ORM\Entity(repositoryClass="ExampleRepository")
* @ORM\Table(name="tp_example", indexes={
* })
* @author Pecina Ondřej <[email protected]>
*/
class ExampleEntity extends Object
{
/**
* @ORM\Column(length=128)
* @var string
*/
protected $name;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
* @var int
*/
protected $id;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return ExampleEntity
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}
class ExampleFormMapper
{
/** @var ArrayAccessEntityMapper */
private $arrayAccessEntityMapper;
/**
* ExampleFormMapper constructor.
* @param ArrayAccessEntityMapper $arrayAccessEntityMapper
*/
public function __construct(ArrayAccessEntityMapper $arrayAccessEntityMapper)
{
$this->arrayAccessEntityMapper = $arrayAccessEntityMapper;
}
public function getForm() {
$form = new Form();
$form->addText('name', 'Name')
->setRequired();
$form->addSubmit('Sub', 'Save');
$form->onSuccess[] = function(Form $form) {
$entity = new ExampleEntity();
// array with name is not reuired - if is not set, set all values with existing properties in entity
$this->arrayAccessEntityMapper->setToEntity($form->getValues(), $entity, ['name']);
// works same like line above
$this->arrayAccessEntityMapper->setToEntity($form->getValues(), $entity);
// you can map also in this way
$this->arrayAccessEntityMapper->setToEntity(ArrayHash::from([
'name' => 'Testing name'
]), $entity);
// do stuff with entity
};
return $form;
}
}
namespace App;
use DoctrineMapper\EntityFormMapper;
use Nette\Application\UI\Form;
use Doctrine\ORM\Mapping as ORM;
use Nette\Object;
/**
* Example entity
*
* @ORM\Entity(repositoryClass="ExampleRepository")
* @ORM\Table(name="tp_example", indexes={
* })
* @author Pecina Ondřej <[email protected]>
*/
class ExampleEntity extends Object
{
/**
* @ORM\Column(length=128)
* @var string
*/
protected $name;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
* @var int
*/
protected $id;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return ExampleEntity
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}
class ExampleFormMapper
{
/** @var EntityFormMapper */
private $entityFormMapper;
/**
* ExampleFormMapper constructor.
* @param EntityFormMapper $entityFormMapper
*/
public function __construct(EntityFormMapper $entityFormMapper)
{
$this->entityFormMapper = $entityFormMapper;
}
public function getForm() {
$form = new Form();
$form->addText('name', 'Name')
->setRequired();
$form->addSubmit('Sub', 'Save');
$form->onSuccess[] = function(Form $form) {
// do stuff
};
$entity = new ExampleEntity();
$entity->setName('Example name');
// this line map all values in entity to form in this case set to component with name name' value "Example name"
$this->entityFormMapper->setEntityToContainer($entity, $form);
return $form;
}
}
namespace App;
use DoctrineMapper\Builder\BuilderDefinition;
use DoctrineMapper\FormEntityBuilder;
use Nette\Application\UI\Form;
use Doctrine\ORM\Mapping as ORM;
use Nette\Object;
/**
* Test related entity
*
* @ORM\Entity(repositoryClass="ExampleRepositoryRel")
* @ORM\Table(name="tp_example_re", indexes={
* })
* @author Pecina Ondřej <[email protected]>
*/
class TestExampleEntityRel extends Object
{
/**
* @ORM\Column(length=128)
* @var string
*/
protected $name;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
* @var int
*/
protected $id;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return TestExampleEntityRel
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}
/**
* Example entity
*
* @ORM\Entity(repositoryClass="ExampleRepository")
* @ORM\Table(name="tp_example", indexes={
* })
* @author Pecina Ondřej <[email protected]>
*/
class ExampleEntity extends Object
{
/**
* @ORM\Column(length=128)
* @var string
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="App\TestExampleEntityRel")
* @ORM\JoinColumn(name="rel_id", referencedColumnName="id")
* @var OwnerEntity
*/
protected $related;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
* @var int
*/
protected $id;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return ExampleEntity
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @param TestExampleEntityRel $related
* @return ExampleEntity
*/
public function setRelated(TestExampleEntityRel $related)
{
$this->related = $related;
return $this;
}
/**
* @return TestExampleEntityRel
*/
public function getRelated()
{
return $this->related;
}
}
class ExampleFormMapper
{
/** @var FormEntityBuilder */
private $formEntityBuilder;
/**
* ExampleFormMapper constructor.
* @param FormEntityBuilder $formEntityBuilder
*/
public function __construct(FormEntityBuilder $formEntityBuilder)
{
$this->formEntityBuilder = $formEntityBuilder;
}
public function getForm() {
$entity = new ExampleEntity();
$entity->setName('Example name');
// TRUE param build form automatically
$builder = $this->formEntityBuilder->getBuilder($entity, TRUE);
$form = $builder->getForm();
$form->addSubmit('Sub', 'Save');
$form->onSuccess[] = function(Form $form) {
// do stuff
};
// FALSE param is manual render form
$builder = $this->formEntityBuilder->getBuilder($entity, FALSE);
$builder->add([
'propertyName' => 'name',
// allowed types in BuilderDefinition::COMPONENT_TYPE_*
'componentType' => BuilderDefinition::COMPONENT_TYPE_TEXT_AREA, // override component type (component type is generated automatically from entity)
'settings' => [
'label' => 'Name',
'placeholder' => '[email protected]',
'
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.