简体   繁体   English

在持久之前将表单数据绑定到变量-Symfony2 / Doctrine2

[英]Bind form data to variable before persist - Symfony2 / Doctrine2

Currently I am trying to bind a user selected option to a variable and then use that variable in a function BEFORE persisting the data. 目前,我正在尝试将用户选择的选项绑定到变量,然后在持久存储数据之前在函数中使用该变量。

My current entity code is this: 我当前的实体代码是这样的:

public function setUnit($unit)
{
    $this->unit = $unit;
    return $this;
}
public function getUnit()
{
    return $this->unit;
}
public function setSize($size)
{        
    $unit = $this->getUnit();
    $this->size = $size = new Mass($size, $unit);
    $this->size = $size->toUnit('mg');      
    return $this;
}

And my form: 和我的形式:

    $builder
        ->add('size')
        ->add('unit')
        ;

And finally my controller: 最后是我的控制器:

 public function createAction(Request $request)
 {
    $entity = new Products();

    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);


    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity->setUser($this->get('security.context')->getToken()->getUser());
        $em->persist($entity);
        $em->flush();

There is quite a bit more code but think I've removed all the non-related stuff. 还有很多代码,但是我已经删除了所有无关的东西。

SO.. Basically what I want to do it: 所以。基本上我想做的是:

  1. User enters Size and Unit (For example 100 grams) clicks submit. 用户输入“大小和单位”(例如100克),然后单击“提交”。
  2. new Mass($size, $unit); 新质量($ size,$ unit); converts $size by $unit to Mg 将$ size按$ unit转换为Mg
  3. Data is now persisted to the database. 现在,数据将保存到数据库中。

If I specify set $unit variable myself, it works fine ($unit = 'g') but when I try to pass the unit by $this-getUnit; 如果我自己指定set $ unit变量,它可以正常工作($ unit ='g'),但是当我尝试通过$ this-getUnit传递单位时; nothing is being sent leaving the $unit variable as null. 没有发送任何内容,而将$ unit变量保留为null。

  Unknown unit of measure ()

If I try doing the same but updating a pre existing entry it works fine but is always one step behind eg: 如果我尝试这样做但更新现有条目,则可以正常工作,但始终落后一步,例如:

So if its 'g' set in the database and I try to convert an 'oz' it converts the 'G' and then on the next try if say I enter kg it will then convert by the oz from before. 因此,如果在数据库中设置了它的“ g”,而我尝试转换为“ oz”,它将转换为“ G”,然后在下一次尝试下,如果我说输入kg,它将由之前的oz转换。

I'm assuming this is happening because the form is valid, so Symfony tries to persist the entity but then hits the new Mass function which throws an error because it has not been sent the $unit variable yet. 我假设发生这种情况是因为表单有效,因此Symfony尝试保留该实体,但随后命中了新的Mass函数,该函数引发错误,因为尚未将其发送给$ unit变量。 How do I send the $unit variable BEFORE persistence so as to convert it to the new $size. 我如何在持久化之前发送$ unit变量,以便将其转换为新的$ size。

Have seriously spent so many hours on this now and last night I woke up thinking about it far too many times I just can't give up now I feel so close. 现在和昨晚已经花了很多时间认真地做这件事,昨晚我醒来想了太多次,但现在我还是不能放弃,我感到如此亲近。

(( The Unit converter I am using is https://github.com/triplepoint/php-units-of-measure/ just incase anyone was wondering )) ((我正在使用的单位转换器是https://github.com/triplepoint/php-units-of-measure/以防万一有人在想)

Answer thanks to Jenechka 感谢Jenechka

public function setSize($size)
{
    $this->size = $size;       
    return $this;
}

/**
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function fixSize()
{
    $unit = $this->getUnit();
    $size = $this->getSize();

    $mass = new Mass($size, $unit);
    $this->size = $mass->toUnit('mg');      
}

and in my orm.yml 在我的orm.yml中

    lifecycleCallbacks:
    prePersist: [ setCreatedAtValue, fixSize ]
    preUpdate: [ setUpdatedAtValue, fixSize ]

Happy now :) Thank you. 现在快乐:)谢谢。

Try to "fix" size in prePersist or preUpdate events . 尝试在prePersist或preUpdate事件中 “固定”大小。

/**
 * @Entity
 * @HasLifecycleCallbacks
 */
class YourEntity {
    // ...

    public function setSize($size)
    {        
        $this->size = $size;
        return $this;
    }

    /**
     * @PrePersist
     * @PreUpdate
     */
    public function fixSize()
    {
        $unit = $this->getUnit();
        $size = $this->getSize();

        $mass = new Mass($size, $unit);
        $this->size = $mass->toUnit('mg');      
    }

Or another way 或另一种方式

public function setUnit($unit)
{
    $this->unit = $unit;
    $this->updateSize();

    return $this;
}

public function setSize($size)
{        
    $this->size = $size;
    $this->updateSize();

    return $this;
}

public function updateSize()
{
    $unit = $this->getUnit();
    $size = $this->getSize();

    if ($unit && $size) {
        $mass = new Mass($size, $unit);
        $this->size = $mass->toUnit('mg');      
    }

    return $this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM