开发者

How to serialize swfWidgetformChoice multiple options to single field in database in symfony 1.4

开发者 https://www.devze.com 2023-02-11 22:21 出处:网络
I am using symfony 1.4 in a project and i need to store multiple options in a single field . I am using sfWidgetFormChoice Set up loooks like this:

I am using symfony 1.4 in a project and i need to store multiple options in a single field .

I am using sfWidgetFormChoice Set up loooks like this:

    $status = Doctrine::getTable('Profile')->getStatuses();
    $this->widgetSchema['status'] = new sfWidgetFormChoice(array(
        'expanded'=>true,
        'choices'=>$status,
        'multiple'=>true
        ));
    $this->validatorSchema['status'] = new sfValidatorChoice( 
            array('choices'=>array_keys($status),
            'multiple'=>true, 'required'=>false
   ));
开发者_Go百科

In my model I use the following to serialise multiple options into single field.

  public function  setStatus($data) {
     $data = serialize($data);

     $this->_set('status', $data);

}

?> Which works like a charm and save data as:

a:2:{i:0;s:7:"relaxed";i:1;s:8:"Inactive";}

However I'm am having difficulty retrieving the serialised string as an array using the following in my model:

    public function  getStatus() {
     return unserialize($this->status);
    }

Am I missing something here? I get the following error:

Notice: Undefined property: Profile::$status in C:.../.././ Which doesnt make sense to me..


Doctrine has the "Array" type, which will automatically serialize/deserialize your array for you. Just specify the type of status as "Array" in your schema.yml file.


public function  getStatus() {
    return unserialize($this->_get('status'));
}

However, you can use a solution mentioned by @greg0ire

0

精彩评论

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

关注公众号