carrooi / addressable
Addressable module for Nette
1.1.0
2015-05-06 10:34 UTC
Requires
- kdyby/doctrine: ~2.2
- kdyby/events: ~2.3
- nette/application: ~2.2
- nette/di: ~2.2
- nette/utils: ~2.2
- tracy/tracy: ~2.3
Requires (Dev)
- mockery/mockery: ~0.9
- nette/bootstrap: ~2.2
- nette/mail: ~2.2
- nette/safe-stream: ~2.2
- nette/tester: ~1.3.0
This package is auto-updated.
Last update: 2024-12-06 03:52:40 UTC
README
Addressable module for Nette framework and Doctrine.
Installation
$ composer require carrooi/addressable
$ composer update
Usage
Imagine that you want to be able to add address to User
entity.
namespace App\Model\Entities; use Carrooi\Addressable\Model\Entities\IAddressableEntity; use Carrooi\Addressable\Model\Entities\TAddressable; use Doctrine\ORM\Mapping as ORM; use Kdyby\Doctrine\Entities\Attributes\Identifier; use Kdyby\Doctrine\Entities\BaseEntity; /** * @ORM\Entity * @author David Kudera */ class User extends BaseEntity implements IAddressableEntity { use Identifier; use TAddressable; }
Users facade:
namespace App\Model\Facades; use Carrooi\Addressable\Model\Facades\AddressesFacade; /** * @author David Kudera */ class UsersFacade { /** @var \Kdyby\Doctrine\EntityManager */ private $em; /** @var \Carrooi\Addressable\Model\Facades\AddressesFacade */ private $addressesFacade; /** * @param \Kdyby\Doctrine\EntityManager $em * @param \Carrooi\Addressable\Model\Facades\AddressesFacade $addressesFacade */ public function __construct(EntityManager $em, AddressesFacade $addressesFacade) { $this->em = $em; $this->addressesFacade = $addressesFacade; } /** * @param \App\Model\Entities\User $user * @param string $city * @param int $postalCode * @param int $houseNumber * @param array $values */ public function addAddress(User $user, $city, $postalCode, $houseNumber, arra $values = []) { $address = $this->addressesFacade->create($city, $postalCode, $houseNumber, $values); $user->setAddress($address); $this->em->persist($user)->flush(); return $this; } /** * @param \App\Model\Entities\User $user */ public function removeAddress(User $user) { $this->addressesFacade->remove($user->getAddress()); $user->setAddress(null); $this->em->persist($user)->flush(); return $this; } }
Usage:
/** @var \App\Model\Facades\UsersFacade @inject */ public $usersFacade; /** @var \Carrooi\Addressable\Model\Facades\AddressesFacade @inject */ public $addressesFacade; public function actionAdd() { $user = getUserSomehow(); $this->usersFacade->addAddress($user, 'Prague', 13000, 555, [ 'orientationNumber' => '8b', 'street' => 'Lorem ipsum', ]); } public function actionEdit() { $user = getUserSomehow(); $address = $user->getAddress(); $this->addressesFacade->update($address, [ 'city' => 'New York', 'postalCode' => 88877, 'houseNumber' => 1212, 'orientationNumber' => 45, 'street' => 'Ipsum', ]); } public function actionRemove() { $user = getUserSomehow(); $this->usersFacade->removeAddress($user); }
And that's it :)
Changelog
-
1.1.0
- House number is no more required, so whole street with all numbers can be stored in
street
column (BC break) - Improved tests
- House number is no more required, so whole street with all numbers can be stored in
-
1.0.0
- Initial version