douzi2778 2013-08-07 17:36
浏览 18
已采纳

保存索引数组

I am currently trying to figure out how to save a indexed array to a field in my database. That said, I know an array can't be saved to a database, but you can serialize it or implode it and then save. Im not sure which one I should be using though. I don't want a collection of items to be saved in just one cell. I need the list of items to be saved one by one in the column. So my question is do I need to be using the serialize method, implode or something else? Here is a glimpse of my code and the array I am trying to save.

 public function findPolicyIds($coverageId = null) {
    $policyid = $this->Policy->find('all', array(
        'recursive' => -1,
        'conditions' => array('Policy.coverage_id' => $coverageId),
        'fields' => array('Policy.id')));

        foreach($policyid as $id) {
            $all[] = $id['Policy']['id'];




        }
        return $all;
}


 Array
(
[0] => 5202834f-111c-4a76-8b33-1ed8ae78509d
[1] => 5202834f-2ba8-4957-91db-1ed8ae78509d
[2] => 5202834f-356c-49a1-beeb-1ed8ae78509d
[3] => 5202834f-3b40-453f-a491-1ed8ae78509d
  • 写回答

1条回答 默认 最新

  • dpmir1988 2013-08-07 18:14
    关注

    It depends.

    This is probably the best answer to give you.

    Do whatever you like, as long as it doesn't magically corrupt the data between write and read.

    Lemme take a moment to explore a few options on your behalf.

    • var_export() - might not be the best idea, securitywise
    • serialize() - seems straightforward
    • implode() - seems straightforward
    • json_encode() - seems straightforward

    There are probably other, more obscure, options available. You could even build up complex data sets with XML, if you like.

    Alternatively, why not normalize the schema and add a new table to present that collections of array-data ? Normalization tends to save your bacon in the future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line