Giter VIP home page Giter VIP logo

forms-doctrine's Introduction

Fork of WebChemistry/forms-doctrine

Build Status

Installation

extensions:
    - WebChemistry\Forms\DoctrineExtension

Usage

Entity:

/**
 * @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:

class BaseRepository {

    use WebChemistry\Forms\Doctrine\TBaseRepository;

}

and using:

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
}

forms-doctrine's People

Contributors

dejvidecz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.