Skip to content

Instantly share code, notes, and snippets.

@mrezekiel
Last active December 13, 2015 23:19
Show Gist options
  • Save mrezekiel/4991305 to your computer and use it in GitHub Desktop.
Save mrezekiel/4991305 to your computer and use it in GitHub Desktop.
Gist for DoctrineORMModule Issue #181
<?php
namespace Application\Entity;
use Application\Factory\ChangeTracker;
use Doctrine\ORM\Mapping as ORM;
/**
* Contacts
*
* @ORM\Table(name="contacts")
* @ORM\Entity
*/
class Contacts extends ChangeTracker
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="full_name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @var string $name
*
* @ORM\Column(name="home_phone", type="string", length=20, nullable=true)
*/
private $home;
/**
* @var string $name
*
* @ORM\Column(name="work_phone", type="string", length=20, nullable=true)
*/
private $work;
/**
* @var string $name
*
* @ORM\Column(name="mobile_phone", type="string", length=20, nullable=true)
*/
private $mobile;
/**
* @var EmployeeEmergencyContacts
*
* @ORM\OneToOne(targetEntity="EmergencyContacts", mappedBy="contact")
*/
private $employee;
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
// TODO: this is not a perminent solution, it however fixes
// the error logs being filled with undefined property Contacts::$contact
if (isset($this->$property))
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
// workaround untill doctrine fix empty strings converting to null
if ($value === '')
$value = null;
if ($value != $this->$property)
{
$this->_onPropertyChanged($property, $this->$property, $value);
$this->$property = $value;
}
}
}
<?php
namespace Application\Entity;
use Application\Factory\ChangeTracker;
use Doctrine\ORM\Mapping as ORM;
/**
* EmergencyContacts
*
* @ORM\Table(name="emergency_contacts")
* @ORM\Entity
*/
class EmergencyContacts extends ChangeTracker
{
/**
* @var Employees
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Employees", inversedBy="emergencyContacts")
* @ORM\JoinColumn(name="employee_id", referencedColumnName="id")
*/
private $employee;
/**
* @var Contacts
*
* @ORM\Id
* @ORM\OneToOne(targetEntity="Contacts", inversedBy="employee")
* @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
*/
private $contact;
/**
* @var integer $position
*
* @ORM\Column(name="position", type="integer", nullable=false)
*/
private $position;
/**
* @var string $relationship
*
* @ORM\Column(name="relationship", type="string", length=20, nullable=false)
*/
private $relationship;
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
if ($value != $this->$property)
{
$this->_onPropertyChanged($property, $this->$property, $value);
$this->$property = $value;
}
}
}
<?php
namespace Application\Entity;
use Application\Factory\ChangeTracker;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager;
/**
* Employees
*
* @ORM\Table(name="employees", indexes={@ORM\Index(name="payroll_number_index", columns={"payroll_number"})})
* @ORM\Entity(repositoryClass="Application\Repositories\EmployeesRepository")
*/
class Employees extends ChangeTracker
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $payrollNumber
*
* @ORM\Column(name="payroll_number", type="string", length=10, nullable=false)
*/
private $payrollNumber;
/**
* @var string $firstName
*
* @ORM\Column(name="first_name", type="string", length=255, nullable=false)
*/
private $firstName;
/**
* @var string $lastName
*
* @ORM\Column(name="last_name", type="string", length=255, nullable=false)
*/
private $lastName;
/**
* @var string $preferredName
*
* @ORM\Column(name="preferred_name", type="string", length=255, nullable=true)
*/
private $preferredName;
/**
* @var string $gender
*
* @ORM\Column(name="gender", type="string", length=1, nullable=false)
*/
private $gender;
/**
* @var date $dob
*
* @ORM\Column(name="dob", type="date", nullable=false)
*/
private $dob;
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255, nullable=true)
*/
private $email;
/**
* @var string $phone
*
* @ORM\Column(name="phone", type="string", length=20, nullable=true)
*/
private $phone;
/**
* @var string $mobile
*
* @ORM\Column(name="mobile", type="string", length=20, nullable=true)
*/
private $mobile;
/**
* @var string $windowsLogin
*
* @ORM\Column(name="windows_login", type="string", length=8, nullable=false)
*/
private $windowsLogin;
/**
* @var string $position
*
* @ORM\Column(name="position", type="string", length=255, nullable=true)
*/
private $position;
/**
* @var date $started
*
* @ORM\Column(name="started_date", type="date", nullable=false)
*/
private $started;
/**
* @var date $terminated
*
* @ORM\Column(name="terminated_date", type="date", nullable=true)
*/
private $terminated;
/**
* @var string $supervisor
*
* @ORM\Column(name="supervisor", type="string", length=255, nullable=true)
*/
private $supervisor;
/**
* @var Departments
*
* @ORM\ManyToOne(targetEntity="Departments", inversedBy="employees")
* @ORM\JoinColumn(nullable=true)
*/
private $department;
/**
* @var Locations
*
* @ORM\ManyToOne(targetEntity="Locations")
* @ORM\JoinColumn(nullable=true)
*/
private $location;
/**
* @var WorkContact
*
* @ORM\OneToOne(targetEntity="WorkContact", mappedBy="employee")
*/
private $workContact;
/**
* @var EmployeeAddresses
*
* @ORM\OneToMany(targetEntity="EmployeeAddresses", mappedBy="employee")
*/
private $addresses;
/**
* @var EmergencyContacts
*
* @ORM\OneToMany(targetEntity="EmergencyContacts", mappedBy="employee")
*/
private $emergencyContacts;
public function __construct()
{
$this->emergencyContacts = new ArrayCollection();
$this->addresses = new ArrayCollection();
}
/**
* Pass through entity manager with value for department
*
* @param EntityManager $em
* @param string $value
*/
public function setDepartment(EntityManager $em, $value)
{
$department = $em->getRepository('Application\Entity\Departments')->findOneBy(array('department' => $value));
if (!$department)
{
$department = new Departments();
$department->setDepartment($value);
$em->persist($department);
$em->flush();
}
$this->__set('department', $department);
}
/**
* Pass through entity manager with value for location
*
* @param EntityManager $em
* @param string $value
*/
public function setLocation(EntityManager $em, $value)
{
$location = $em->getRepository('Application\Entity\Locations')->findOneBy(array('location' => $value));
if (!$location)
{
$location = new Locations();
$location->setLocation($value);
$em->persist($location);
$em->flush();
}
$this->__set('location', $location);
}
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
if (!is_object($value) || $value instanceof \DateTime)
{
// workaround untill doctrine fix empty strings converting to null
if ($value === '')
$value = null;
if ($value != $this->$property)
{
$this->_onPropertyChanged($property, $this->$property, $value);
$this->$property = $value;
}
}
else
{
if ($value != $this->$property)
$this->_onPropertyChanged($property, $this->$property, $value);
$this->$property = $value;
}
}
/**
* Convert the object to array.
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
}
<?php
/**
* @return multitype:array \stdClass
*/
private function addresses()
{
$addresses = array();
foreach ($this->entity->addresses as $empAddress)
{
$address = new \stdClass;
$address->street = $empAddress->address->street;
$address->postcode = $empAddress->address->postcode;
$address->suburb = $empAddress->address->suburb;
$address->state = $empAddress->address->state;
$address->country = $empAddress->address->country;
if ($empAddress->postal)
$addresses['postal'] = $address;
else
$addresses['physical'] = $address;
}
return $addresses;
}
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP Notice: Undefined property: DoctrineORMModule\\Proxy\\__CG__\\Application\\Entity\\Contacts::$contact in /mnt/hgfs/mtsd/module/Application/src/Application/Entity/Contacts.php on line 67
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP Stack trace:
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 1. {main}() /mnt/hgfs/mtsd/public/index.php:0
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 2. Zend\\Mvc\\Application->run() /mnt/hgfs/mtsd/public/index.php:12
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 3. Zend\\EventManager\\EventManager->trigger($event = *uninitialized*, $target = *uninitialized*, $argv = *uninitialized*, $callback = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:295
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 4. Zend\\EventManager\\EventManager->triggerListeners($event = *uninitialized*, $e = *uninitialized*, $callback = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:204
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 5. call_user_func(*uninitialized*, *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:460
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 6. Zend\\Mvc\\DispatchListener->onDispatch($e = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:460
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 7. Zend\\Mvc\\Controller\\AbstractRestfulController->dispatch($request = *uninitialized*, $response = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php:114
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 8. Zend\\Mvc\\Controller\\AbstractController->dispatch($request = *uninitialized*, $response = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractRestfulController.php:212
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 9. Zend\\EventManager\\EventManager->trigger($event = *uninitialized*, $target = *uninitialized*, $argv = *uninitialized*, $callback = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:117
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 10. Zend\\EventManager\\EventManager->triggerListeners($event = *uninitialized*, $e = *uninitialized*, $callback = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:204
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 11. call_user_func(*uninitialized*, *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:460
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 12. Zend\\Mvc\\Controller\\AbstractRestfulController->onDispatch($e = *uninitialized*) /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:460
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 13. Rest\\Controller\\EmployeeController->getList() /mnt/hgfs/mtsd/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractRestfulController.php:279
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 14. Application\\Repositories\\EmployeesRepository->getFullStdObj($entity = *uninitialized*) /mnt/hgfs/mtsd/module/Rest/src/Rest/Controller/EmployeeController.php:52
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 15. Application\\Repositories\\EmployeesRepository->emergencyContacts() /mnt/hgfs/mtsd/module/Application/src/Application/Repositories/EmployeesRepository.php:115
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 16. Doctrine\\ORM\\PersistentCollection->getIterator() /mnt/hgfs/mtsd/module/Application/src/Application/Repositories/EmployeesRepository.php:74
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 17. Doctrine\\ORM\\PersistentCollection->initialize() /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php:600
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 18. Doctrine\\ORM\\UnitOfWork->loadCollection($collection = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php:236
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 19. Doctrine\\ORM\\Persisters\\BasicEntityPersister->loadOneToManyCollection($assoc = *uninitialized*, $sourceEntity = *uninitialized*, $coll = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2752
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 20. Doctrine\\ORM\\Persisters\\BasicEntityPersister->loadCollectionFromStatement($assoc = *uninitialized*, $stmt = *uninitialized*, $coll = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1700
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 21. Doctrine\\ORM\\Internal\\Hydration\\AbstractHydrator->hydrateAll($stmt = *uninitialized*, $resultSetMapping = *uninitialized*, $hints = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:974
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 22. Doctrine\\ORM\\Internal\\Hydration\\ObjectHydrator->hydrateAllData() /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:140
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 23. Doctrine\\ORM\\Internal\\Hydration\\ObjectHydrator->hydrateRowData($row = *uninitialized*, $cache = *uninitialized*, $result = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:178
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 24. Doctrine\\ORM\\Internal\\Hydration\\ObjectHydrator->getEntity($data = *uninitialized*, $dqlAlias = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:527
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 25. Doctrine\\ORM\\UnitOfWork->createEntity($className = *uninitialized*, $data = *uninitialized*, $hints = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:279
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 26. DoctrineORMModule\\Proxy\\__CG__\\Application\\Entity\\Contacts->addPropertyChangedListener($listener = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2663
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 27. Closure->__invoke(*uninitialized*, *uninitialized*, *uninitialized*) /mnt/hgfs/mtsd/data/DoctrineORMModule/Proxy/__CG__ApplicationEntityContacts.php:201
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 28. Doctrine\\ORM\\Proxy\\{closure}($proxy = *uninitialized*, *uninitialized*, *uninitialized*) /mnt/hgfs/mtsd/data/DoctrineORMModule/Proxy/__CG__ApplicationEntityContacts.php:201
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 29. Doctrine\\ORM\\Persisters\\BasicEntityPersister->load($criteria = *uninitialized*, $entity = *uninitialized*, $assoc = *uninitialized*, $hints = *uninitialized*, $lockMode = *uninitialized*, $limit = *uninitialized*, $orderBy = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php:162
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 30. Doctrine\\ORM\\Internal\\Hydration\\AbstractHydrator->hydrateAll($stmt = *uninitialized*, $resultSetMapping = *uninitialized*, $hints = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:733
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 31. Doctrine\\ORM\\Internal\\Hydration\\ObjectHydrator->hydrateAllData() /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:140
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 32. Doctrine\\ORM\\Internal\\Hydration\\ObjectHydrator->hydrateRowData($row = *uninitialized*, $cache = *uninitialized*, $result = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:178
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 33. ReflectionProperty->setValue(*uninitialized*, *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:493
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 34. DoctrineORMModule\\Proxy\\__CG__\\Application\\Entity\\Contacts->__set($name = *uninitialized*, $value = *uninitialized*) /mnt/hgfs/mtsd/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:493
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 35. Application\\Entity\\Contacts->__set($property = *uninitialized*, $value = *uninitialized*) /mnt/hgfs/mtsd/data/DoctrineORMModule/Proxy/__CG__ApplicationEntityContacts.php:74
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 36. DoctrineORMModule\\Proxy\\__CG__\\Application\\Entity\\Contacts->__get($name = *uninitialized*) /mnt/hgfs/mtsd/data/DoctrineORMModule/Proxy/__CG__ApplicationEntityContacts.php:83
[Wed Feb 20 08:29:08 2013] [error] [client 203.4.205.227] PHP 37. Application\\Entity\\Contacts->__get($property = *uninitialized*) /mnt/hgfs/mtsd/data/DoctrineORMModule/Proxy/__CG__ApplicationEntityContacts.php:62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment