duanjieyi6582 2013-08-28 11:31
浏览 44
已采纳

Symfony:$ request-> setParameter不接受Object

I am having trouble passing an associative array along with a redirect/forward, done in the action-file. Using Symfony 1.3.11.

Obviously there is some flaw, however this is my train of thought: Make a db query, parse the results, add them to an associative array (their ID as key) and redirect the user to the page from whence he came, along with the associative array.

$products = array();

foreach ($results as $res) {

  $product = $res->getProduct();
  $products[$product->getId()] = $product;
}

$this->getRequest()->setParameter("products", $products);
$this->forward("main", "index");

This is the returned error message:

Catchable fatal error: Object of class Product could not be converted to string in /var/www/perfecthomeweb/lib/vendor/symfony-1.3.11/lib/escaper/sfOutputEscaperObjectDecorator.class.php on line 98

Note: My initial preference was to use the

$this->redirect($request->getReferer());

in conjunction with the desired associative array, looking like this:

$this->redirect($request->getReferer() . "?persons=" . $persons);

This obviously did not work, and I am pretty sure my lack of knowledge of POST/GET is partially to blame. However, if this is doable using $request->getReferer(), that would be my clear preference, as it feels fluid and dynamic.

  • 写回答

1条回答 默认 最新

  • duanbin3021 2013-08-28 11:36
    关注

    One solution to prevent the error is to create a string in this line already:

    $products[$product->getId()] = $product;
    

    For example by provoking the error early:

    $products[$product->getId()] = (string) $product;
    

    You then can think about which string representation you want to put there. As I don't know, I can not suggest that. Probably you're looking for JSON?

    $products[$product->getId()] = json_encode($product);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵