I'm overriding my doSave() method to basically do the following: I have a sfWidgetFormPropelChoice field that the user can either choose from, or type a new option. How can I change the widget's value? Or maybe I am approaching this the wrong way. So here is how I overrode the doSave() method:
我正在覆盖我的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);
}
3 个解决方案
#1
You should use setDefault or setDefaults and then it will autopopulate with the bound values.
您应该使用setDefault或setDefaults然后它将使用绑定值自动填充。
(sfForm) setDefault ($name, $default)
(sfForm) setDefaults ($defaults)
usage
$form->setDefault('WidgetName', 'Value');
$form->setDefaults(array(
'WidgetName' => 'Value',
));
#2
You could do it in the action :
你可以在行动中做到:
$this->form->getObject()->setFooId($this->foo->getId()) /*Or get the manufacturer id or name from request here */
$this->form->save();
But I prefer to do the kind of work you are doing with your manufacturer directly in my Peer so my business logic is always at the same place.
但我更喜欢直接在我的Peer中与制造商一起做的工作,所以我的业务逻辑总是在同一个地方。
What I put in my forms is mainly validation logic.
我在表单中添加的主要是验证逻辑。
Example of what to put in the save method of the Peer :
在Peer的save方法中放入什么的示例:
public function save(PropelPDO $con= null)
{
if ($this->isNew() && !$this->getFooId())
{
$foo= new Foo();
$foo->setBar('bar');
$this->setFoo($foo);
}
}
#3
Two assumption here: a) your form gets the name of the manufacturer and b) your model wants the ID of a manufacturer
这里有两个假设: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);
}
#1
You should use setDefault or setDefaults and then it will autopopulate with the bound values.
您应该使用setDefault或setDefaults然后它将使用绑定值自动填充。
(sfForm) setDefault ($name, $default)
(sfForm) setDefaults ($defaults)
usage
$form->setDefault('WidgetName', 'Value');
$form->setDefaults(array(
'WidgetName' => 'Value',
));
#2
You could do it in the action :
你可以在行动中做到:
$this->form->getObject()->setFooId($this->foo->getId()) /*Or get the manufacturer id or name from request here */
$this->form->save();
But I prefer to do the kind of work you are doing with your manufacturer directly in my Peer so my business logic is always at the same place.
但我更喜欢直接在我的Peer中与制造商一起做的工作,所以我的业务逻辑总是在同一个地方。
What I put in my forms is mainly validation logic.
我在表单中添加的主要是验证逻辑。
Example of what to put in the save method of the Peer :
在Peer的save方法中放入什么的示例:
public function save(PropelPDO $con= null)
{
if ($this->isNew() && !$this->getFooId())
{
$foo= new Foo();
$foo->setBar('bar');
$this->setFoo($foo);
}
}
#3
Two assumption here: a) your form gets the name of the manufacturer and b) your model wants the ID of a manufacturer
这里有两个假设: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);
}