Lets say i have 2 Entities, User and Address. Each User has to have an Address so there is an one-to-one relation between the tables.
Now i want to generate a user and it should automatically create an address row without data and just mark their relation.
Is this possible in the Entity or in the EntityRepository Class? Or is there a command that does this automatically?
Thanks
First, you don't need it. Use Null Object pattern.
Second, if you really need it:
class User {
public function __construct() {
$this->setAddress(new Address);
}
}
And set cascade persist:
* #OneToOne(targetEntity="Address", cascade={"persist"})
Write function in Repository:
class User_Repository {
public function createEmptyUserWithAddress() {
$user = new User();
$entityManager->persist($user);
$entityManager->flush();
$address = new Address();
$entityManager->persist($address);
$entityManager->flush();
$user->setAddress($address);
$address->setUser($user);
$entityManager->flush();
}
}
or extend constructor of user (or address):
class User {
public function __construct(Address $address) {
$this->setAddress($address);
}
}
class User_Repository {
public function createEmptyUserWithAddress() {
$address = new Address();
$entityManager->persist($address);
$entityManager->flush();
$user = new User($address);
$entityManager->persist($user);
$entityManager->flush();
}
}
you can use the __construct() to create a relationship
class User
{
/**
* #OneToOne(targetEntity="Address", cascade={"persist"})
*/
private $address;
public function __construct()
{
$address = new Address();
$address->setUser($this);
$this->address = $address;
}
}
Related
I have a ManyToMany relation (an offer can have many banners, and a banner can belong to many offers). I don't understand why this works:
$banner->getOffers()->removeElement($this->offer);
But this doesn't:
$this->offer->getBanners()->removeElement($banner);
These are my entities:
Class Banner
{
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Offer", inversedBy="banners")
*/
private $offers;
public function __construct()
{
$this->offers = new ArrayCollection();
}
public function getOffers(): Collection
{
return $this->offers;
}
}
--
Class Offer
{
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Banner", mappedBy="offers")
* #ORM\JoinColumn
*/
private $banners;
public function __construct()
{
$this->banners = new ArrayCollection();
}
public function getBanners(): Collection
{
return $this->banners;
}
}
the removeElement() method returns true:
I tried to persist $this->offer and even $banner, but nothing changed, the row isn't deleted from the banner_offer table.
I'm on Doctrine 2.7.1, Symfony 3.1.7
What am I doing wrong?
In my project I have several class table inheritances like this:
namespace MyProject\Model;
/**
* #Entity
* #InheritanceType("JOINED")
* #DiscriminatorColumn(name="discr", type="string")
* #DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/** #Entity */
class Employee extends Person
{
// ...
}
I have a method which converts entities to arrays based on the fields which have public getters. The problem here is that I lose the inheritance information in my array because the discriminator value isn't stored in a field.
So what I tried was the following, hoping doctrine would automatically set $disc:
class Person
{
// can I automatically populate this field with 'person' or 'employee'?
protected $discr;
public function getDiscr() { return $this->discr; }
public function setDiscr($disc) { $this->discr; }
// ...
}
Is there a way to make this work in doctrine? Or would I need to read the class metadata in my entity-to-array method?
Sadly, there is no documented way to map the discr column to an entity. That's because the discr column is really part of the database and not the entity.
However, it's quite common to just put the discr value directly in your class definition. It's not going to change and you will always get the same class for the same value anyways.
class Person
{
protected $discr = 'person';
class Employee extends Person
{
protected $discr = 'employee';
Here's a small example of what I have in one of my ZF2 projects (using Doctrine MongoDB ODM):
// an instance of your entity
$entity = ...;
/** #var \Doctrine\ODM\MongoDB\DocumentManager $documentManager */
$documentManager = $serviceManager->get('DocumentManager');
/** #var \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory $factory */
$factory = $documentManager->getMetadataFactory()
/** #var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $metadata */
$metadata = $factory->getMetadataFor(get_class($object));
if ($metadata->hasDiscriminator()) {
// assuming $data is result of the previous extraction
$data[$metadata->discriminatorField] = $metadata->discriminatorValue;
}
What I have done is I've implemented a custom interface DiscriminatorAwareInterface and I only apply the checks to classes that implement it (in your case it would be the class that all "discriminated" classes extend.
As a result I end up with code that looks like this:
// add value of the discrinimator field to entities that support it
if ($object instanceof DiscriminatorAwareInterface) {
/** #var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $metadata */
$metadata = $factory->getMetadataFor(get_class($object));
if ($metadata->hasDiscriminator()) {
$data[$metadata->discriminatorField] = $metadata->discriminatorValue;
}
}
I'm pretty sure it will be the same if you use the standard ORM, except instead of a document manager you will have entity manager.
Just got this problem and solved it without defining the discriminator as a real member:
abstract class MyEntity {
const TYPE_FOO = 'foo';
const TYPE_BAR = 'bar';
const TYPE_BUZ = 'buz';
...
/**
* #return string
*/
public function getMyDiscriminator()
{
$myDiscriminator = null;
switch (get_class($this)) {
case MyEntityFoo::class:
$myDiscriminator = self::TYPE_FOO;
break;
case MyEntityBar::class:
$myDiscriminator = self::TYPE_BAR;
break;
case MyEntityBuz::class:
$myDiscriminator = self::TYPE_BUZ;
break;
}
return $myDiscriminator;
}
...
}
class MyEntityFoo extends MyEntity {}
class MyEntityBar extends MyEntity {}
class MyEntityBuz extends MyEntity {}
You can use the following solution:
`$`$metadata = \Doctrine\ORM\Mapping\ClassMetadata((string)$entityName);
print_r($metadata->discriminatorValue);`
Being able to use Doctrine speeds up a lot of things however it feels somewhat clunky to me having to set / use the entity manager in all of my controllers. I would prefer to have all of the database logic in 1 specific module. Perhaps I'm just thinking about this the wrong way, and someone can point me in the right direction.
Currently I have my Entity which functions just fine and I can do insertions into the database fine with the following
namespace Manage\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class ViewController extends AbstractActionController {
public function somethingAction(){
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$user = new \Manage\Entity\User();
$user->setname('foo');
$user->settitle('bar');
$objectManager->persist($user);
$objectManager->flush();
}
}
However whenever I want to select something from the database I have to make sure to add
use Doctrine\ORM\EntityManager;
And then the following list of Controller functions...
/**
* #var EntityManager
*/
protected $entityManager;
/**
* Sets the EntityManager
*
* #param EntityManager $em
* #access protected
* #return PostController
*/
protected function setEntityManager(EntityManager $em) {
$this->entityManager = $em;
return $this;
}
/**
* Returns the EntityManager
*
* Fetches the EntityManager from ServiceLocator if it has not been initiated
* and then returns it
*
* #access protected
* #return EntityManager
*/
protected function getEntityManager() {
if (null === $this->entityManager) {
$this->setEntityManager($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));
}
return $this->entityManager;
}
Once I have added all of that I can now do a query in my getsomethingAction like so...
public function getsomethingAction() {
$repository = $this->getEntityManager()->getRepository('Manage\Entity\User');
$list = $repository->findAll();
var_dump($list);
return new ViewModel();
}
To me that feels very clunky... I can do an insert without needing all the extra functions but I cannot do a select? Is it possible to extend the Entity class in order to get the find / findAll etc functions that is provided by calling $repository = $this->getEntityManager()->getRepository('Manage\Entity\User'); directly inside the entity?
By that I mean I would prefer to be able to run the find directly on the entity as I would when I set the data... like below:
public function getsomethingAction(){
$list = new \Manage\Entity\User();
$l = $list->findAll();
var_dump($l);
return new ViewModel();
}
Ok so my main objective so far has been to move the complex logic out of the controllers into a re-usable model. So with this example answer I'm creating an interface where the complex logic would live however it also allows me to still use the model in a controller to get data from the database... here is the Model...
namespace Manage\Model;
use Doctrine\ORM\EntityManager;
class ApiInterface {
/**
* #var EntityManager
*/
protected $entityManager;
protected $sl;
/**
* Sets the EntityManager
*
* #param EntityManager $em
* #access protected
* #return PostController
*/
protected function setEntityManager(EntityManager $em) {
$this->entityManager = $em;
return $this;
}
/**
* Returns the EntityManager
*
* Fetches the EntityManager from ServiceLocator if it has not been initiated
* and then returns it
*
* #access protected
* #return EntityManager
*/
protected function getEntityManager() {
if (null === $this->entityManager) {
$this->setEntityManager($this->sl->get('Doctrine\ORM\EntityManager'));
}
return $this->entityManager;
}
public function __construct($ServiceLocator) {
$this->sl = $ServiceLocator;
}
public function get() {
$repository = $this->getEntityManager()->getRepository('Manage\Entity\ApiList');
return $repository;
}
public function set() {
return new \Manage\Entity\ApiList();
}
public function save($data) {
$objectManager = $this->sl->get('Doctrine\ORM\EntityManager');
$objectManager->persist($data);
$objectManager->flush();
}
public function doComplexLogic($foo,$bar){
// Can now use both set() and get() to inspect/modify/add data
}
}
So now inside my controller I can do something that gets some basic data from the table like:
public function getapiAction() {
$api = new \Manage\Model\ApiInterface($this->getServiceLocator());
var_dump($api->get()->findAll());
return new ViewModel();
}
And to quickly set data from a controller I can do:
public function setapiAction() {
$apiInterface = new \Manage\Model\ApiInterface($this->getServiceLocator());
$api= $apiInterface->set();
$user->setfoo('blah');
$user->setbar('moo');
$apiInterface->save($api);
return new ViewModel();
}
And it also allows me to run complex logic from the controller by taking the complexity out of the controller like so...
public function complexAction(){
$foo = $this->params()->fromQuery();
$bar = $this->params()->fromPost();
$apiInterface = new \Manage\Model\ApiInterface($this->getServiceLocator());
$apiInterface->doComplexLogic($foo, $bar);
}
Please let me know in comments if this answer would be the proper way to do things, I realize it's very simple and generic but I wanted to keep it that way so others can understand what / why and if this is a good approach / not etc.
First question on stackoverflow, little bit nervous.
So, here's the situation :
I'm using Doctrine 2 for a website. I installed it properly without pear, using the good libs and good paths in my application. I created my DB before starting to code, so I created mapping (xml & yml, just in case) with reverse-engineering method (doctrine exporter). Generated mappings are fine, then I wrote the entities files myself (the console didn't worked).
/** #Entity */
class Member {
/** #Id #Column(type="string", nullable=false) */
private $login;
/** #Column(type="string", nullable=false) */
private $password;
/** #Column(type="string") */
private $mail;
/** */
private $dateregister;
/** */
private $datelastconnexion;
/** #Column(type="string") */
private $description;
/** */
private $birthday;
/** #Column(type="string") */
private $website;
/** #Column(type="boolean") */
private $activated;
/** #Column(type="integer") */
private $idstatus;
/** #Column(type="integer") */
private $idcountry;
/** #Column(type="integer") */
private $idlang;
/** #Column(type="integer") */
private $idarticle;
/** #Column(type="integer") */
private $idfilters;
public function __construct($login) {
$this->login = $login;
$this->activated = false;
}
public function getLogin() {return $this->login;}
public function getPassword() {return $this->password;}
public function getMail() {return $this->mail;}
public function getDateregister() {return $this->dateregister->format('Y/m/d');}
public function getDatelastconnexion() {if ($this->datelastconnexion == null) return 'Never logged';
else return $this->datelastconnexion->format('Y/m/d H:i');}
public function getDescription() {return $this->description;}
public function getBirthday() {return $this->birthday->format('Y/m/d');}
public function getWebsite() {return $this->website;}
public function getActivated() {return $this->activated;}
public function getStatus() {return $this->idstatus;}
public function getCountry() {return $this->idcountry;}
public function getLang() {return $this->idlang;}
public function getArticles() {return $this->idarticle->toArray();}
public function getFilters() {return $this->idfilters->toArray();}
public function setLogin($newLogin) {$this->login = $newLogin;}
public function setPassword($newPassword) {$this->password = sha1($newPassword);}
public function setMail($newMail) {$this->mail = $newMail;}
public function setDateregister($newDate) {$this->dateregister = $newDate;}
public function setDatelastconnexion($newDate) {$this->datelastconnexion = $newDate;}
public function setDescription($newDescription) {$this->description = $newDescription;}
public function setBirthday($newDate) {$this->birthday = $newDate;}
public function setWebsite($newWebsite) {$this->website = $newWebsite;}
public function setActivated($activate) {$this->activated = $activate;}
public function setStatus($newStatus) {$this->idstatus = $newStatus;}
public function setCountry($newCountry) {$this->idcountry = $newCountry;}
public function setLang($newLang) {$this->idlang = $newLang;}
public function setArticles($newArticles) {$this->idarticle = $newArticles;}
public function setFilters($newFilters) {$this->idfilters = $newFilters;}
}
Now I was trying to insert something into the database, but I'm blocked with a small problem :
When a member tries to register, he fills some informations (logins, password, etc). I make every tests to see if informations are corrects and if they are, I create a new Object Member, which I fill with every given information and automatically generated one, then I call persist() on the object, then flush().
$newMember = new Member($login);
$newMember -> setPassword($passwd);
$newMember -> setMail($mail);
$newMember -> setDateregister($currentDate);
$newMember -> setDescription($description);
$newMember -> setBirthday($newBirthday);
$newMember -> setStatus($status[0]); // existing object
$newMember -> setCountry($country[0]); // existing object
$newMember -> setLang($lang[0]); // existing object
$doctrineManager->persist($newMember);
$doctrineManager->flush($newMember);
I got some surprise looking into the database, seeing the filled ID is equal to '' (empty string), and not $login.
So... that's my question : how is that possible ? Is it because I didn't fill every property of Member before flushing ? or didn't give information on relations between entities (one-to-many, many-to-many, many-to-one) ? or anything else ? I'm on this problem for one week now, I couldn't figure it out.
For information :
$status, $lang, $country are foreign keys.
$article and $filters are many-to-many relations.
Thanks for reading, and (I hope) thanks for explaining me my mistakes.
You must use flush like this:
$doctrineManager->flush();
after that you can access id with:
$newmember->getId();
The fact is $idarticles and $idfilters were ManyToMany relations, which I didn't specified. On construction of the object, it could be possible Doctrine 2 was not able to construct the object according to the mapping.
So the construct function looks like this :
public function __construct()
{
$this->idfilters = new \Doctrine\Common\Collections\ArrayCollection();
$this->idarticle = new \Doctrine\Common\Collections\ArrayCollection();
$this->activated = false;
}
And each ManyToMany have to get their ArrayCollections in order to work.
I'm learning doctrine2, and having a problem how to call constructor automatically.
For example, in my entity I have
/**
* #Entity
*/
class User{
....
public function __construct() {
exit('in');
}
}
and when I get the object this way:
$userObj = $em->find('User', 1);
I do get that object from database, but constructor is never called.
I want to put some common things in constructor, like validation rules, or even to put sample code from the doctrine documentation like
$this->comments = new ArrayCollection();
This ofcourse works when I create new object in code for creating a user like
$user = new User(); //now constructor works just fine
Now, what is the "proper" way of getting the entity? I doubt I have to call constructor manually each time I user $em->find() with $user0bj->__construct(); ? This would kinda sucks then... Or I should use something other then ->find() to get single entity properly?
I know I can user #PrePersist, and I am using it to actually do validation checks etc.
I guess that I'm probably missing something here, or I'm trying to use constructor in a poor way. Thanks for any explanations and guides!
I'm pretty certain that find or similar isn't expected to call the constructor...
You need to hook into the #PostLoad event.
Why would you want to call the constuctor of already persisted entity? When you need to validate it you should have done the validation or initializations before you have persisted it. So When you call a already persisted entity there is no point to validate it.
The right place to put validation and other initializations is the constructor method of entity.
Eg.
/**
* #Entity
*/
class User{
protected $name;
public function __construct($name) {
if (isset($name)) {
//** validate the name here */
$this->name=$name;
} else {
throw new Exception("no user name set!");
}
}
}
According to the doctrine2 documentation Doctrine2 never calls __construct() method of entities.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/architecture.html?highlight=construct
doctrine uses reflection to instantiate your object without invoking your constructor.
Since PHP 5.4 , you can use reflection to instanciate a class without
calling the constructor using
ReflectionClass::newInstanceWithoutConstructor
the instantiator of doctrine use it like :
private function buildFactory(string $className) : callable
{
$reflectionClass = $this->getReflectionClass($className);
if ($this->isInstantiableViaReflection($reflectionClass)) {
return [$reflectionClass, 'newInstanceWithoutConstructor'];
}
$serializedString = sprintf(
'%s:%d:"%s":0:{}',
is_subclass_of($className, Serializable::class) ? self::SERIALIZATION_FORMAT_USE_UNSERIALIZER : self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER,
strlen($className),
$className
);
$this->checkIfUnSerializationIsSupported($reflectionClass, $serializedString);
return static function () use ($serializedString) {
return unserialize($serializedString);
};
}
Doctrine ORM will "rewrite" your class, it generate a new class that implement \Doctrine\ORM\Proxy\Proxy
And it rewrite the construct method:
/**
* #param \Closure $initializer
* #param \Closure $cloner
*/
public function __construct($initializer = null, $cloner = null)
{
$this->__initializer__ = $initializer;
$this->__cloner__ = $cloner;
}
You can see it inside the cache folder ${CACHE}/doctrine/orm/Proxies.
You will need both #ORM\HasLifecycleCallbacks on the class + #ORM\PostLoad on a specific function of your choice.
Beware! If you put it on the constructor it will override loaded database data!
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="dossier")
* #ORM\Entity()
* #ORM\HasLifecycleCallbacks
*/
class Dossier
{
// ...
/**
* The normal constructor stays as usual
*/
public function __construct()
{
$this->takenActions = new ArrayCollection();
$this->classifications = new ArrayCollection();
$this->dossierProblems = new ArrayCollection();
$this->internalNotes = new ArrayCollection();
}
/**
* Triggers after the entity has been loaded in the EntityManager (e.g. Doctrine's ->find() etc...)
* The constructor does not get called. Some variables still need a default value
* Must be in combination with "ORM\HasLifecycleCallbacks" on the class
*
* #ORM\PostLoad
*/
public function postLoadCallback(): void
{
// Only put a default value when it has none yet
if (!$this->dossierProblems)
$this->dossierProblems = new ArrayCollection();
if (!$this->internalNotes)
$this->internalNotes = new ArrayCollection();
}
// ...
}