2017 © Pedro Peláez
 

library forms-doctrine

Helper for doctrine forms

image

webchemistry/forms-doctrine

Helper for doctrine forms

  • Monday, July 16, 2018
  • by Antik
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1,017 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 1 Forks
  • 2 Open issues
  • 8 Versions
  • 3 % Grown

The README.md

Entity to array and conversely

Build Status, (*1)

Installation

extensions:
    - WebChemistry\Forms\DoctrineExtension

Usage

Entity:, (*2)

/**
 * @ORM\Entity()
 */
class User {

    /**
     * @ORM\Id()
     * @ORM\Column(type="integer", length=11)
     * @ORM\GeneratedValue()
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Tests\Item", inversedBy="users")
     */
    private $items;

    /**
     * @ORM\ManyToOne(targetEntity="Tests\Role", inversedBy="users")
     */
    private $role;

    /**
     * @ORM\OneToOne(targetEntity="Tests\Notice", inversedBy="user")
     */
    private $notice;

    public function __construct($id) {
        $this->items = new ArrayCollection();
        $this->setId($id);
    }

    public function addItem(Item $item) {
        $this->items->add($item);
        $item->addUser($this);
    }

    public function getItems() {
        return $this->items;
    }

    /**
     * @return mixed
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param mixed $id
     * @return self
     */
    public function setId($id) {
        $this->id = $id;

        return $this;
    }

    /**
     * @return Role
     */
    public function getRole() {
        return $this->role;
    }

    public function setRole($role) {
        $this->role = $role;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getNotice() {
        return $this->notice;
    }

    /**
     * @param mixed $notice
     * @return self
     */
    public function setNotice($notice) {
        $this->notice = $notice;
        $notice->setUser($this);

        return $this;
    }

}

$values = [ 'id' => 5, 'role' => [ 'id' => 1, 'name' => 2 ], 'items' => [ ['id' => 1] // Calls addItem() for each item ['id' => 2] ] ]; /** @var Entity\User $entity */ $entity = $this->helper->toEntity('Entity\User', $values); $array = $this->helper->toArray($entity); var_dump($array == $entity); // dumps true

Export selected items

public function export() {
    $settings = new new WebChemistry\Forms\Doctrine\Settings();
    $settings->setAllowedItems([
       'name', // Select name
       'items' => ['*'], // Select all items in items
       'role' => array('id') // Select id in role
    ]);

    $this->doctrine->toArray($this->entity, $settings);
}

Export one item in sub entities

public function export() {
    $settings = new new WebChemistry\Forms\Doctrine\Settings();
    $settings->setJoinOneColumn(array(
        'role' => 'id'
    ));

    // Create array: ['role' => 5] instead of ['role' => ['id' => 5, 'name' => 'foo']]

    $this->doctrine->toArray($this->entity, $settings);
}

Custom callback

public function export() {
    $settings = new new WebChemistry\Forms\Doctrine\Settings();
    $settings->setCallbacks(array(
        'role' => function ($value, $entity) {
            return ['id' => $value->getId() * 2];
        }
    ));

    // Create array ['role' => ['id' => 10]] instead of ['role' => ['id' => 5, 'name' => 'foo']]

    $this->doctrine->toArray($this->entity, $settings);
}

Auto-find by ID

public function export() {
    $settings = new new WebChemistry\Forms\Doctrine\Settings();
    $settings->setFind([
        'role' => 10 // Uses method find from repository
    ]);

    $this->doctrine->toArray($this->entity, $settings);
}

Usage in doctrine repository

Change BaseRepository:, (*3)


class BaseRepository { use WebChemistry\Forms\Doctrine\TBaseRepository; }

and using:, (*4)


class UserRepository { /** * @return Entity\User */ public function toEntity(array $values, Entity\User $defaultEntity = NULL) { $settings = new WebChemistry\Forms\Doctrine\Settings(); // ... return $this->convertToEntity($values, $defaultEntity, $settings); } /** * @return array */ public function toArray(Entity\User $entity) { $settings = new new WebChemistry\Forms\Doctrine\Settings(); // ... return $this->convertToArray($entity, $settings); } public function save(array $values) { $this->_em->persist($this->toEntity($values)); $this->_em->flush(); } }

Usage in forms


/** @var WebChemistry\Forms\Doctrine @inject */ public $doctrine; protected function createComponentForm() { $form = new WebChemistry\Forms\Form(); // For easier usage $form->setDoctrine($this->doctrine); $form->addText('name', 'User name') ->setRequired(); $form->addText('password', 'Password') ->setRequired(); $form->addCheckbox('remember', 'Remember'); $form->addSubmit('submit', 'Sign in'); $form->setEntity($this->em->getRepository('Entity\User')->find(1)); return $form; } public function afterSign(WebChemistry\Forms\Application\Form $form) { $entity = $form->getEntity(); // Gets object from set object and fill it with new values $entity = $form->getEntity('Entity\User'); // Create new class }

The Versions

16/07 2018

dev-master

9999999-dev

Helper for doctrine forms

  Sources   Download

The Requires

 

The Development Requires

helper doctrine forms nette webchemistry

03/08 2016

1.3.3

1.3.3.0

Helper for doctrine forms

  Sources   Download

The Requires

 

The Development Requires

helper doctrine forms nette webchemistry

21/06 2016

1.3.2

1.3.2.0

Helper for doctrine forms

  Sources   Download

The Requires

 

The Development Requires

helper doctrine forms nette webchemistry

20/06 2016

1.3.1

1.3.1.0

Helper for doctrine forms

  Sources   Download

The Requires

 

The Development Requires

helper doctrine forms nette webchemistry

18/06 2016

1.3

1.3.0.0

Helper for doctrine forms

  Sources   Download

The Requires

 

helper doctrine forms nette webchemistry

25/12 2015

1.2

1.2.0.0

Helper for doctrine forms

  Sources   Download

helper doctrine forms nette webchemistry

31/07 2015

1.1

1.1.0.0

Helper for doctrine forms

  Sources   Download

helper doctrine forms nette webchemistry

27/07 2015

1.0

1.0.0.0

Helper for doctrine forms

  Sources   Download

helper doctrine forms nette webchemistry