5

基本的に次のことを行うために doSave() メソッドをオーバーライドしています: ユーザーが選択するか新しいオプションを入力できる sfWidgetFormPropelChoice フィールドがあります。ウィジェットの値を変更するにはどうすればよいですか? あるいは、私はこれに間違った方法で取り組んでいるのかもしれません。したがって、 doSave() メソッドをオーバーライドする方法は次のとおりです。

public function doSave($con = null)
{
    // Save the manufacturer as either new or existing.
    $manufacturer_obj = ManufacturerPeer::retrieveByName($this['manufacturer_id']->getValue());
    if (!empty($manufacturer_obj))
    {
        $this->getObject()->setManufacturerId($manufacturer_obj->getId()); // NEED TO CHANGE THIS TO UPDATE WIDGET'S VALUE INSTEAD?
    }
    else
    {
        $new = new Manufacturer();
        $new->setName($this['manufacturer_id']->getValue());
        $new->save();
        $this->getObject()->setManufacturerId($new->getId()); // NEED TO CHANGE THIS TO UPDATE WIDGET'S VALUE INSTEAD?
    }

    parent::doSave($con);
}
4

3 に答える 3

9

setDefault または setDefaults を使用すると、バインドされた値が自動入力されます。

(sfForm) setDefault ($name, $default)
(sfForm) setDefaults ($defaults)

利用方法

$form->setDefault('WidgetName', 'Value');
$form->setDefaults(array(
    'WidgetName' => 'Value',
));
于 2009-12-07T12:23:52.273 に答える
2

アクションでそれを行うことができます:

$this->form->getObject()->setFooId($this->foo->getId()) /*Or get the manufacturer id or name from request here */
$this->form->save();

しかし、私のビジネス ロジックは常に同じ場所にあるので、私はあなたが製造業者と直接行っているような作業をピアで行うことを好みます。

私がフォームに入れているのは、主に検証ロジックです。

Peer の save メソッドに何を入れるかの例:

public function save(PropelPDO $con= null)
{
  if ($this->isNew() && !$this->getFooId())
  {
    $foo= new Foo();
    $foo->setBar('bar');
    $this->setFoo($foo);
   } 
}
于 2009-06-23T03:24:00.180 に答える
1

ここでの 2 つの前提: a) フォームが製造元の名前を取得し、b) モデルが製造元の ID を必要としている

public function doSave($con = null)
{
    // retrieve the object from the DB or create it
    $manufacturerName = $this->values['manufacturer_id'];
    $manufacturer = ManufacturerPeer::retrieveByName($manufacturerName);
    if(!$manufacturer instanceof Manufacturer)
    {
        $manufacturer = new Manufacturer();
        $manufacturer->setName($manufacturerName);
        $manufacturer->save();
    }

    // overwrite the field value and let the form do the real work
    $this->values['manufacturer_id'] = $manufacturer->getId();

    parent::doSave($con);
}
于 2009-06-24T08:12:04.787 に答える