保存嵌入式 Propel symfony 表单以实现一对一关系

发布于 2024-12-27 18:41:58 字数 1477 浏览 1 评论 0原文

我有一对具有一对一关系的表。

我有一个投诉表单,需要在其中嵌入一个人员表单,相关架构如下:

  complaint:
    id:                            ~
    created_at:                    ~
    updated_at:                    ~
    complainant_id:                { type: integer, foreignTable: person_data, foreignReference: id, onDelete: setnull }
    status:                        { type: tinyint, default: 1 }
    complaint_title:               { type: varchar(64) } 
    complaint_number:              { type: varchar(16) } 
    recipient:                     { type: varchar(128) }

  person_data:
    id:                            ~
    created_at:                    ~
    updated_at:                    ~
    company_name:                  { type: varchar(64) }
    first_name:                    { type: varchar(64) }
    last_name:                     { type: varchar(64) }
    email:                         { type: varchar(128) }

我能够成功地将两个对象保存到数据库,但主要投诉对象没有使用 person_data 行的投诉 ID 进行更新。

有谁知道为什么这不能正常工作以及如何强制它正确更新投诉对象?

我使用的是 symfony 1.4.13,Propel 1.6.3。

更新:

以下是嵌入表单的代码:

<?php
    public function configure()
    {
        $use_fields = array();

        // ...other fields added...


        $sub_form   = new PersonDataForm(array(), array());
        $this->embedForm('complainant', $sub_form);
        array_push($use_fields, 'complainant');

        $this->useFields($use_fields);
    }

I have a pair of tables that have a one-to-one relationship.

I have a complaint form that needs to embed a person form inside of that, the relevant schema is below:

  complaint:
    id:                            ~
    created_at:                    ~
    updated_at:                    ~
    complainant_id:                { type: integer, foreignTable: person_data, foreignReference: id, onDelete: setnull }
    status:                        { type: tinyint, default: 1 }
    complaint_title:               { type: varchar(64) } 
    complaint_number:              { type: varchar(16) } 
    recipient:                     { type: varchar(128) }

  person_data:
    id:                            ~
    created_at:                    ~
    updated_at:                    ~
    company_name:                  { type: varchar(64) }
    first_name:                    { type: varchar(64) }
    last_name:                     { type: varchar(64) }
    email:                         { type: varchar(128) }

I am able to successfully save both objects to the database but the main complaint object is not being updated with the complainant_id of the person_data row.

Does anyone know why this isn't working correctly and how to force it to update the complaint object correctly?

I am using symfony 1.4.13, Propel 1.6.3.

UPDATE:

Here is the code for the embedded form:

<?php
    public function configure()
    {
        $use_fields = array();

        // ...other fields added...


        $sub_form   = new PersonDataForm(array(), array());
        $this->embedForm('complainant', $sub_form);
        array_push($use_fields, 'complainant');

        $this->useFields($use_fields);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对不⑦ 2025-01-03 18:41:58

我找到了解决这个问题的办法。

重写表单类中的 saveEmbeddedForms 方法。

更新主对象发生在保存嵌入表单之后,因此 ids 可用于更新主对象。

public function saveEmbeddedForms($con = null, $forms = null) 
{

    // save the embedded forms
    parent::saveEmbeddedForms($con, $forms);

    // loop through all embedded forms and update the main object with their ids
    foreach($this->getEmbeddedForms() as $name => $embedded_form)
    {
        switch($name)
        {
            case 'recipient':
                // criteria to determine if the sub-object should be saved or not
                if($embedded_form->getObject()->getFirstName() == '' && $embedded_form->getObject()->getLastName() == '')
                {
                    $embedded_form->getObject()->delete();
                    $this->getObject()->setRecipientId(null);
                    $this->getObject()->save();
                }
                else
                    $this->getObject()->setRecipientId($embedded_form->getObject()->getId());
                break;
            case 'complainant':
                if($embedded_form->getObject()->getFirstName() == '' && $embedded_form->getObject()->getLastName() == '')
                {
                    $embedded_form->getObject()->delete();
                    $this->getObject()->setComplainantId(null);
                    $this->getObject()->save();
                }
                else
                {
                    $this->getObject()->setComplainantId($embedded_form->getObject()->getId());
                }
                break;
            default:
                break;
        }
    }

    // save the main object with the new sub-object keys set
    $this->getObject()->save();         
}

不幸的是,我在互联网上找不到任何有此解释的地方。这是为那些追随我的人准备的。

I've found a solution to this problem.

Override the saveEmbeddedForms method in the form class.

Updating the main object occurs after the saving of the embedded forms so the ids are available to update the main object.

public function saveEmbeddedForms($con = null, $forms = null) 
{

    // save the embedded forms
    parent::saveEmbeddedForms($con, $forms);

    // loop through all embedded forms and update the main object with their ids
    foreach($this->getEmbeddedForms() as $name => $embedded_form)
    {
        switch($name)
        {
            case 'recipient':
                // criteria to determine if the sub-object should be saved or not
                if($embedded_form->getObject()->getFirstName() == '' && $embedded_form->getObject()->getLastName() == '')
                {
                    $embedded_form->getObject()->delete();
                    $this->getObject()->setRecipientId(null);
                    $this->getObject()->save();
                }
                else
                    $this->getObject()->setRecipientId($embedded_form->getObject()->getId());
                break;
            case 'complainant':
                if($embedded_form->getObject()->getFirstName() == '' && $embedded_form->getObject()->getLastName() == '')
                {
                    $embedded_form->getObject()->delete();
                    $this->getObject()->setComplainantId(null);
                    $this->getObject()->save();
                }
                else
                {
                    $this->getObject()->setComplainantId($embedded_form->getObject()->getId());
                }
                break;
            default:
                break;
        }
    }

    // save the main object with the new sub-object keys set
    $this->getObject()->save();         
}

Unfortunately there is nowhere that I can find on the internet with this explanation. So here it is for those that come after me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文