फॉर्म सबमिशन डेटा को सहेजते समय, मुझे एक नई इकाई इंस्टेंस जारी रखने में परेशानी हो रही है जहां इकाई के पास किसी अन्य इकाई के साथ एक निरर्थक सहयोग है और मैं इसे शून्य पर सेट करने का प्रयास करता हूं। , फार्म के लिए एक नई इकाई उदाहरण बनाकर प्रपत्र को प्रस्तुत अनुरोध बंधन और बने और इकाई उदाहरण निस्तब्धता, मैं कैसे संबद्ध इकाई के लिए संपत्ति को पॉप्युलेट पर निर्भर करता है के बाद, मैं या तो मिलसिम्फनी 2 रूप: मैं एक इकाई को एक निरर्थक सहयोग के साथ कैसे बना सकता हूं?
UnexpectedTypeException: Expected argument of type "object or array", "NULL" given
(यदि सेट शून्य करने के लिए), याInvalidArgumentException: A new entity was found through the relationship 'AccessLog#document' that was not configured to cascade persist operations for entity
(यदि संबंधित इकाई का एक नया, खाली उदाहरण सेट है, जिसे मैं जारी रखना नहीं चाहता)।
यदि मैं कैस्केड बना रहता हूं, तो यह संबंधित तालिका में रिकॉर्ड बनाने की कोशिश करता है (जिसे डीबी में डेटा मॉडल द्वारा अनुमति नहीं है), भले ही इसके लिए कोई डेटा न बने। यदि कैस्केड जारी रखना जारी है तो रास्ता तय करने का तरीका है, मैं इसे नया रिकॉर्ड बनाने की कोशिश करने से कैसे रोकूं? इसे संभालने का सबसे अच्छा तरीका क्या है?
नोट, व्यवहार समान है कि संघ एकजुट या द्विपक्षीय के रूप में स्थापित किया गया है या नहीं।
विवरण:
मैं किसी अन्य संस्था (संक्षिप्त) के साथ एक बहुत-से-एक संघ के साथ एक इकाई है:
/** @Entity */
class Document
{
/** @Id @Column(type="integer") */
private $document_id;
/** @Column(length=255) */
private $name;
/** @OneToMany(targetEntity="AccessLog", mappedBy="document") */
private $access_logs;
// plus other fields
// plus getters and setters for all of the above...
}
:
/** @Entity */
class AccessLog
{
/** @Id @Column(type="integer") */
private $access_log_id;
/** @Column(type="integer", nullable=true) */
private $document_id;
/**
* @ManyToOne(targetEntity="Document", inversedBy="access_logs", cascade={"persist"})
* @JoinColumn(name="document_id", referencedColumnName="document_id")
*/
private $document;
// plus other fields
// plus getters and setters for all of the above...
}
संबंधित इकाई फैंसी कुछ भी नहीं है
मेरे पास नए एक्सेसलॉग रिकॉर्ड के लिए डेटा दर्ज करने के लिए एक सिम्फनी फॉर्म है:
class AccessLogFormType extends AbstractType
{
public function getName()
{
return 'access_log_form';
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'AccessLog');
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('access_log_id', 'hidden');
$builder->add('document_id', 'hidden', array(
'required' => false
));
$builder->add('document', new DocumentType(), array(
'label' => 'Document',
'required' => false
));
//...
}
}
निम्नलिखित समर्थन प्रकार परिभाषा के साथ:
class DocumentType extends AbstractType
{
public function getName()
{
return 'document';
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Document');
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text', array(
'required' => false
));
}
}
मेरे नियंत्रक शामिल निम्नलिखित: जब कोई मौजूदा पहुँच लॉग प्रविष्टि को अद्यतन करने या एक नया बनाने
public function save_access_log_action()
{
$request = $this->get('request');
$em = $this->get('doctrine.orm')->getEntityManager();
$access_log = null;
if ($request->getMethod() === 'POST') {
$data = $request->request->get('access_log_form');
if (is_numeric($data['access_log_id'])) {
$access_log = $em->find('AccessLog', $data['access_log_id']);
} else {
$access_log = new AccessLog();
}
if (is_numeric($data['document_id'])) {
$document = $em->find('Document', $data['document_id']);
$access_log->set_document($document);
} else {
// Not calling set_document() since there shouldn't be a
// related Document entity.
}
$form = $this->get('form.factory')
->createBuilder(new AccessLogFormType(), $access_log)
->getForm();
$form->bindRequest($request);
if ($form->isValid()) {
$em->persist($access_log);
$em->flush();
}
} else {
// ... (handle get request)
}
return $this->render('access_log_form.tpl', array(
'form' => $form->createView()
));
}
उपरोक्त कोड ठीक काम करता है और एक दस्तावेज़ को फॉर्म में चुना गया है, लेकिन यदि कोई दस्तावेज़ नहीं चुना गया है।
डेटा मॉडल को मानना नहीं बदला जा सकता है, मैं एक नई दस्तावेज़ इकाई को बनाए रखने के बिना एक नई AccessLog इकाई को जारी रखने के बारे में कैसे जा सकता हूं?
आपको 'सेटडॉक्यूमेंट (शून्य) 'करने की आवश्यकता क्यों है? क्यों न केवल 'setDocument' को कॉल करें? – greg0ire