0

我有一个包含一个大文本字段的类,该字段将包含可能是合同或商业表格的文本,到目前为止,这是我的代码:

public function saveAction()
{
    if (isset ($_POST['Save']) && $this->isPostSafe()) {
        $form = Form::getPostObject();
        $form->text = addslashes($form->text);
        var_dump($form);
        $form->save($this->_databaseConnection);
    }
    $this->redirect('/forms');
}

public function __construct()
{
    $company_info = Config::get('Company');
    foreach ($company_info as $setting => $value) {
        $this->variables[$setting] = $value;
    }
    $this->variables = serialize($this->variables);
}

public function set($name, $value)
{
    $values = unserialize($this->variables);
    $values[$name] = $value;
    $this->variables = serialize($this->variables);
}

public function render()
{
    var_dump($this->variables);
    $values = unserialize($this->variables);

    foreach ( $values as $key => $value) {
        $from = "[@$key]";
        $this->text = str_replace($from, $value, $this->text);
    }
    return $template;
}

问题是 var_dump 给了我未序列化的字符串,我尝试了 base64_encode 并没有解决问题

4

2 回答 2

1

你的设置功能是错误的

public function set($name, $value)
{
    $values = unserialize($this->variables);
    $values[$name] = $value;
    $this->variables = serialize($this->variables);
}

应该

public function set($name, $value)
{
    $values = unserialize($this->variables);
    $values[$name] = $value;
    $this->variables = serialize($values);
}
于 2013-07-20T05:48:46.087 回答
0

我的建议是重写代码......基本上你只想处理保存对象时的序列化和加载对象时的反序列化

public function __construct()
{
    $company_info = Config::get('Company');
    foreach ($company_info as $setting => $value) {
        $this->variables[$setting] = $value;
    }
}

public function set($name, $value)
{
      $this->variables[$name] = $value;
}

public function get($name){
      return isset($this->variables[$name])?$this->variables[$name]:"";
}

public function render()
{
    // for debugging
    var_dump($this->variables);

    foreach ( $this->variabes as $key => $value) {
        $from = "[@$key]";
        $this->text = str_replace($from, $value, $this->text);
    }
    return $template;
}

public function saveAction()
{
    if (isset ($_POST['Save']) && $this->isPostSafe()) {
        $form = Form::getPostObject();
        $form->text = addslashes($form->text);
        var_dump($form);
        $serialized = serialize($this->variables); // serialize before you save
        $form->save($this->_databaseConnection);
    }
    $this->redirect('/forms');
}
于 2013-07-20T05:56:28.053 回答