开发者

does sfWidgetFormSelect provide a string or an int of the selected item?

开发者 https://www.devze.com 2023-01-31 13:14 出处:网络
I\'m having an annoying problem. I\'m trying to find out what fields of a form were changed, and then insert that into a table. I managed to var_dump in doUpdateObjectas shown in the following

I'm having an annoying problem. I'm trying to find out what fields of a form were changed, and then insert that into a table. I managed to var_dump in doUpdateObjectas shown in the following

public function doUpdateObject($values) 
{ 
    parent::doUpdateObject($values);
    var_dump($this->getObject()->getModified(false));
    var_dump($this->getObject()->getModified(true));
 } 

And it seems like $this->getObject()->getModified seems to work in giving me both before and after values by setting it to either true or false.

The problem that I'm facing right now is that, some how, sfWidgetFormSelect seems to be saving one of my fields as a string. before saving, that exact same field was an int. (I got this idea by var_dump both before an开发者_如何转开发d after).

Here is what the results on both var dumps showed:

array(1) {["annoying_field"]=> int(3)} array(1) {["annoying_field"]=>string(1)"3"}

This seems to cause doctrine to think that this is a modification and thus gives a false positive.

In my base form, I have

under $this->getWidgets()
'annoying_field'    => new sfWidgetFormInputText(),

under $this->setValidators
'annoying_field'    => new sfValidatorInteger(array('required' => false)),

and lastly in my configured Form.class.php I have reconfigured the file as such:

$this->widgetSchema['annoying_field'] = new sfWidgetFormSelect(array('choices' => $statuses));

statuses is an array containing values like {""a", "b", "c", "d"}

and I just want the index of the status to be stored in the database.

And also how can I insert the changes into another database table? let's say my Log table?

Any ideas and advice as to why this is happen is appreciated, I've been trying to figure it out and browsing google for various keywords with no avail.

Thanks!

Edit:

ok so I created another field, integer in my schema just for testing. I created an entry, saved it, and edited it.

this time the same thing happened!


first if you what the status_id to be saved in the database, you should define your status array like this:

 {1 => "a", 2 => "b", 3 => "c", 4 => "d"}

So that way he know that 1 should be rendered like "a" and so on. Then, when saving, only the index should be saved.

About saving in another database, my advise is to modify the doSave method defined by the Form class yo match your needs. I only know how Propel deals with it, maybe this could help:

the doSave method dose something like this:

  protected function doSave($con = null)
  {
    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $old = $this->getObject()->getModifiedValues($this);//Define this
    $new_object = new Log($old);//Create a new log entry
    $new_object->save($con));//save it!

    $this->updateObject();

    $this->getObject()->save($con);

    // embedded forms
    $this->saveEmbeddedForms($con);
  }

Hope this helps!

Edit:

This is an example extracted from a model in one of my applications and its working ok:

Schema:

[...]    
funding_source_id:
  type: integer
  required: true
[...]

Form:

$this->setWidget('funding_source_id', new sfWidgetFormChoice(array(
   'choices' => array(1 => 'asdads', 2 => '123123123' , 3 => 'asd23qsdf'),
)));


$this->setValidator('funding_source_id', new sfValidatorChoice(array(
   'choices' => array(1 => 'asdads', 2 => '123123123' , 3 => 'asd23qsdf'),
  'required' => true
)));

About the log thing, that could be quite more complex, you should read the current implementation of the doSave method in the base form class, currently sfFomrObject on Symfony1.4., and when and how it delegates object dealing with modified values.


Okay, It turns out I forgot to do a custom validator to use the array key instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号