So, I've just shifted to hydrate approach to the forms and looks quite neat. Although, I have one slight issue. I have two entities Contact
and ContactSocialMediaAccount
so I would need Contact->getId()
to saving ContactSocialMediaAccount
entity but everytime I submit form it says Entity_ID
cannot be null. And this Entity_ID
is ContactSocialMediaAccount
table.
This is Contact table and entity
class Contact {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="ID")
*/
protected $ID;
/**
* @ORM\Column(
* name="FirstName",
* type="string",
* options={"default" : ""}
* )
*/
protected $FirstName = "";
public function getID($asString = false) {
return $asString ? "JOU-" . $this->ID : $this->ID;
}
public function setID($id) {
$this->ID = $id;
}
public function getFirstName() {
return $this->FirstName;
}
public function setFirstName($firstName) {
$this->FirstName = $firstName;
}
}
And this is ContactSocialMediaAccount
class ContactSocialMediaAccount {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="ID")
*/
/**
* @ORM\ManyToOne(targetEntity="Contact")
* @ORM\JoinColumn(name="Entity_ID", referencedColumnName="ID")
*/
private $Contact;
public function getContact() {
return $this->Contact;
}
public function setContact($contact) {
$this->Contact = $contact;
return $this;
}
}
ContactFieldSet.php
public function __construct(ObjectManager $objectManager) {
parent::__construct("Contact");
$this->setHydrator(new DoctrineHydrator($objectManager));
$this->setObject(new Contact());
//rest of the fields
$socialMediaFieldset = new SocialMediaFieldset($objectManager);
$this->add([
"type" => Element\Collection::class,
"name" => "SocialMediaAccounts",
"options" => [
"count" => 5,
"should_create_template" => false,
"allow_add" => true,
"allow_remove" => false,
"target_element" => $socialMediaFieldset
]
]);
}
And ContactSocialMediaFieldset.php
public function __construct(ObjectManager $objectManager) {
parent::__construct("socialMediaAccount");
$this->setHydrator(new DoctrineHydrator($objectManager));
$this->setObject(new ContactSocialMediaAccount());
$this->add([
"name" => "URL",
"type" => "Text",
]);
}
And my ContactForm.php
public function __construct(ObjectManager $objectManager) {
parent::__construct("contactForm");
$this->setHydrator(new DoctrineHydrator($objectManager));
$this->setInputFilter(new InputFilter());
$contactFieldset = new ContactFieldset($objectManager);
$contactFieldset->setUseAsBaseFieldset(true);
$this->add($contactFieldset);
}
How would I add populate Entity_ID
in SocialMedia fields. I have added this in contact entity
/**
* @param SocialMediaAccounts[] $socialMediaAccounts
* @return self
*/
public function setSocialMediaAccounts(array $socialMediaAccounts) {
$this->SocialMediaAccounts = $socialMediaAccounts;
return $this;
}
/**
* @return SocialMediaAccounts[]
*/
public function getSocialMediaAccounts() {
if (!$this->SocialMediaAccounts->count()) {
$accounts = [];
foreach (ContactSocialMediaAccount::getTypes() as $type => $name) {
$account = new ContactSocialMediaAccount;
$account->setSocialMediaID($type);
$this->SocialMediaAccounts->add($account);
}
}
return $this->SocialMediaAccounts;
}
/**
* @param Collection $socialMediaAccounts
*/
public function addSocialMediaAccounts(Collection $socialMediaAccounts) {
foreach ($socialMediaAccounts as $socialMediaAccount) {
$socialMediaAccount->setContact($this);
$this->SocialMediaAccounts->add($socialMediaAccount);
}
}
It doesn't seem to work out.