Skip to content

Instantly share code, notes, and snippets.

@szepczynski
Created April 27, 2017 12:01
Show Gist options
  • Save szepczynski/d3028eb9f92fd7aadd08a578c7a92ad3 to your computer and use it in GitHub Desktop.
Save szepczynski/d3028eb9f92fd7aadd08a578c7a92ad3 to your computer and use it in GitHub Desktop.
SharedKernel\Domain\Model\Gender:
type: embeddable
fields:
value: { type: string, length: 8, column: gender, nullable: true }
<?php
declare(strict_types=1);
namespace SharedKernel\Domain\Model;
use Assert\Assertion;
/**
* Class Gender
* @package SharedKernel\Domain\Model
*/
class Gender implements \Tarifhaus\Doctrine\ORM\NullableEmbeddableInterface
{
/**
* @var string
*/
private $value;
/**
* @param string $gender
*/
public function __construct(string $gender)
{
$gender = strtolower($gender);
Assertion::inArray($gender, ['male', 'female']);
$this->value = $gender;
}
/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
/**
* @return string
*/
public function __toString(): string
{
return $this->getValue();
}
/**
* @return bool
*/
public function isNull(): bool
{
return $this->value === null;
}
}
User\Domain\Model\User:
type: entity
table: users
id:
id:
type: userId
length: 36
nullable: false
generator:
strategy: NONE
embedded:
gender:
class: SharedKernel\Domain\Model\Gender
columnPrefix: false
@havvg
Copy link

havvg commented Apr 27, 2017

@szepczynski You would have to call $listener->addMapping('User\Domain\Model\User', 'gender'); and the listener has to be registered with postLoad event of the User\Domain\Model\User entity.

If you are using Symfony, the service definition would be something like:

services:
    tarifhaus.doctrine.nullable_embeddable_listener:
        public: false
        class: Tarifhaus\Doctrine\ORM\NullableEmbeddableListener
        arguments:
            - '@property_accessor'
        calls:
            - ['addMapping', ['User\Domain\Model\User', 'gender']]
        tags:
            - { name: 'doctrine.orm.entity_listener', entity: 'User\Domain\Model\User', event: 'postLoad' }

I will add a README with an example to the repository :)

@havvg
Copy link

havvg commented Apr 28, 2017

Could you proof-read the README, I just added, are there any details missing? - https://github.com/tarifhaus/doctrine-nullable-embeddable/blob/master/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment