因此,我刚刚改用水合方法处理表格,看起来很整洁。虽然,我有一个小问题。我有两个实体Contact
和ContactSocialMediaAccount
,因此我需要Contact->getId()
保存ContactSocialMediaAccount
实体,但是每次我提交表单时都说Entity_ID
不能为空。这个Entity_ID
是ContactSocialMediaAccount
表。
这是联系人表和实体
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;
}
}
这是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
]
]);
}
和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",
]);
}
还有我的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);
}
如何在SocialMedia字段中添加填充Entity_ID
。我已将此添加到联系人实体中
/**
* @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);
}
}
似乎无法解决问题。