Im having an issue with getting my doctrine entity to extract to my ZF2 form. Im fairly new to both ZF2 and Doctrine (as well as class table inheritance). I've read through practically all of the ZF2 and Doctrine documentation, but still cant seem to figure out how to get this working.
To briefly explain the system, there is a main entity called Policy. The Policy entity includes a Proposer entity and a PolicyProduct entity, as well as a couple of other columns.
I am using Class Table Inheritance in the PolicyProduct entity, as I have 3 different product types, each with different data required. The 3 product entities are TouringCaravan, StaticCaravan and Parkhome.
The Policy entity can only have 1 Proposer and 1 PolicyProduct.
All entities have getter and setter methods for each column.
The form im displaying includes fields from both the Policy entity and the PolicyProduct entity.
To display the form, i first get the Policy entity and Policy Fieldset. And then set the Policy Fieldset as the base.
$policyEntity = $entityManager->getRepository('Policy\Entity\Policy')->find($policyId);
$policyFieldset = $formElementManager->get('policy_form_policy_fieldset');
$policyFieldset->setUseAsBaseFieldset(true);
I then get the PolicyProduct specific fieldset and add it to the policyFieldset.
eg
$productFieldset = $formElementManager->get('tourers_form_touring_fieldset');
$policyFieldset->add($productFieldset);
I then add the policyFieldset to the form, bind it with the Policy entity, and return the completed form.
$productForm->add($policyFieldset);
$productForm->bind($policyEntity);
return $productForm;
To note, i am using the DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator
as my hydrator for each fieldset. I then call the following inside the init()
method of each fieldset.
// Example from PolicyFieldset
$hydrator = new DoctrineHydrator($this->objectManager,true);
$this->setHydrator($hydrator)
->setObject(new Policy());
If i complete the form and save to the database, i can see that the data is stored correctly in the tables. If i then try to hydrate the same form with the entity, none of the PolicyProduct Entity values are mapped. The values in the Policy Entity are hydrated correctly though.
If i dump out $policyEntity->getPolicyProduct()
i can see all the data, which means that the entity is correctly saving and retrieving the information, the problem appears to be when i try to extract the entity data to the form fieldset.
I hope this makes sense, but let me know if not and i'll try to clarify.
Thanks
Here are the main entities
Policy
namespace Policy\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="policy")
*/
class Policy {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Proposer\Entity\Proposer", inversedBy="policy")
* @ORM\JoinColumn(name="proposer_id", referencedColumnName="id")
*
protected $proposer;
/**
* @ORM\ManyToOne(targetEntity="Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="RESTRICT")
*/
protected $product;
/**
* @ORM\OneToOne(targetEntity="Policy\Entity\PolicyProduct")
* @ORM\JoinColumn(name="policy_product_id", referencedColumnName="id")
*/
protected $policyProduct;
/**
* @ORM\ManyToOne(targetEntity="PolicyStatus")
* @ORM\JoinColumn(name="policy_status_id", referencedColumnName="id", onDelete="RESTRICT")
*/
protected $policyStatus;
/**
* @ORM\Column(name="policyInception", type="date", nullable=true)
*/
protected $policyInception;
// Omitted getters and setters
}
PolicyProduct
namespace Policy\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="policy_product")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="productDiscriminator", type="integer")
* @ORM\DiscriminatorMap({1 = "Tourers\Entity\TouringCaravan", 2 = "Statics\Entity\StaticCaravan", 3 = "Parkhomes\Entity\Parkhome"})
*/
class PolicyProduct {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// Omitted getters and setters
}
TouringCaravan - example of a PolicyProduct Inherited Table
namespace Tourers\Entity;
use Doctrine\ORM\Mapping as ORM;
use Policy\Entity\PolicyProduct;
/**
* @ORM\Entity
* @ORM\Table(name="touring_caravan")
*/
class TouringCaravan extends PolicyProduct {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Policy\Entity\Policy")
* @ORM\JoinColumn(name="policy_id", referencedColumnName="id")
*/
protected $policy;
/**
* @ORM\ManyToOne(targetEntity="TouringCaravanMake")
* @ORM\JoinColumn(name="touring_caravan_make", referencedColumnName="id", onDelete="RESTRICT")
*/
protected $caravanMake;
/**
* @ORM\Column(name="caravanModel", type="string", length=128, unique=false, nullable=false)
*/
protected $caravanModel;
....
Lots more columns specific to the TouringCaravan Entity
// Omitted getters and setters
}