繁体   English   中英

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

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

目前,我正在尝试将用户选择的选项绑定到变量,然后在持久存储数据之前在函数中使用该变量。

我当前的实体代码是这样的:

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;
}

和我的形式:

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

最后是我的控制器:

 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();

还有很多代码,但是我已经删除了所有无关的东西。

所以。基本上我想做的是:

  1. 用户输入“大小和单位”(例如100克),然后单击“提交”。
  2. 新质量($ size,$ unit); 将$ size按$ unit转换为Mg
  3. 现在,数据将保存到数据库中。

如果我自己指定set $ unit变量,它可以正常工作($ unit ='g'),但是当我尝试通过$ this-getUnit传递单位时; 没有发送任何内容,而将$ unit变量保留为null。

  Unknown unit of measure ()

如果我尝试这样做但更新现有条目,则可以正常工作,但始终落后一步,例如:

因此,如果在数据库中设置了它的“ g”,而我尝试转换为“ oz”,它将转换为“ G”,然后在下一次尝试下,如果我说输入kg,它将由之前的oz转换。

我假设发生这种情况是因为表单有效,因此Symfony尝试保留该实体,但随后命中了新的Mass函数,该函数引发错误,因为尚未将其发送给$ unit变量。 我如何在持久化之前发送$ unit变量,以便将其转换为新的$ size。

现在和昨晚已经花了很多时间认真地做这件事,昨晚我醒来想了太多次,但现在我还是不能放弃,我感到如此亲近。

((我正在使用的单位转换器是https://github.com/triplepoint/php-units-of-measure/以防万一有人在想)

感谢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');      
}

在我的orm.yml中

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

现在快乐:)谢谢。

尝试在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');      
    }

或另一种方式

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