webchemistry / forms-doctrine
此包的最新版本(1.3.3)没有可用的许可信息。
Doctrine表单的辅助工具
1.3.3
2016-08-03 08:32 UTC
Requires
- kdyby/doctrine: >=2.3
- nette/di: ^2.3
Requires (Dev)
- codeception/codeception: ^2.2
README
安装
extensions: - WebChemistry\Forms\DoctrineExtension
用法
实体
/** * @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
导出选定的项目
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); }
在子实体中导出一个项目
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); }
自定义回调
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); }
按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); }
在Doctrine仓库中使用
更改BaseRepository
class BaseRepository { use WebChemistry\Forms\Doctrine\TBaseRepository; }
并使用
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(); } }
在表单中使用
/** @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 }