2012-06-16 25 views
2

我有这个$ data数组:(建在一个外壳,而不是一种形式)

(这里调试)

array(
(int) 0 => array(
    (int) 0 => 's511013t', 
    (int) 1 => 'id3422', 
    (int) 2 => '1' 
), 
(int) 1 => array(
    (int) 0 => 's511013t', 
    (int) 1 => 'id3637', 
    (int) 2 => '1' 
) 
) 

而且使用saveMany:

$this->Dir->saveMany($data, array('validate' => 'false', 'fieldList' => array('name','dir_dataname', 'project_id'))); 

保存失败,没有错误。

我不知道我的$ data数组是否格式良好,(我很困惑它是否应该有另一个级别)我从sql选择等构建它。但它确实包含我需要保存的所有信息,单模型。

我从shell中运行了这一切,它的工作保存单个记录每次提供的字段名称:

// this works 
$this->Dir->save(array('name' => $data[0][0], 'project_id' => $data[0][2], 'dir_dataname' => $data[0][1])); 

已经阅读保存数据,我很想用saveMany和由于我的自定义$数据格式的fieldList。 (我不想在我的$数据中插入字段名称)。
(没有sql_dump显示,因为从shell任务中获取它非常麻烦)

我花了整整一个晚上试图弄清楚,你能指出我在正确的方向吗?

回答

2

恕我直言,每个数组中的键不是数据库表中的有效字段。它们应该代表与您的表格字段相同的名称。 当你建立从SQL数组,输出应该看起来像这些 - 关联数组:

array(
(int) 0 => array(
    (string) name => 's511013t', 
    (string) dir_dataname => 'id3422', 
    (string) project_id => '1' 
), 
(int) 1 => array(
    (string) name => 's511013t', 
    (string) dir_dataname => 'id3637', 
    (string) project_id => '1' 
) 
) 

Cake2.0 Docs

$this->Dir->saveMany($data); 
+0

谢谢你们;我要做的是迭代我的索引数组并构建关联结构;会回来。 <?PHP的 //关联数组例如 $喜爱=阵列( 'OS'=> '窗口', '论坛'=> '的phpBB', '人'=>'周杰伦 ); ?> –

+0

可疑的是,我使用了一个糟糕的$数据结构。 goggled如何将我的索引数组转换为关联,然后保存:'foreach($ dataIndexed as $ num_linea => $ line_of_text){ $ data [$ num_linea] ['name'] = $ dataIndexed [$ num_linea] [1] ; $ data [$ num_linea] ['dir_dataname'] = $ dataIndexed [$ num_linea] [0]; $ data [$ num_linea] ['qca_interv'] = $ dataIndexed [$ num_linea] [2];' } 感谢您的指导。 ! –

+0

我按照以下方式循环使用索引数组。你有什么建议可以改进它吗? 'foreach($ dataIndexed as $ num_linea => $ line_of_text){data_indexed [$ num_linea] [1]; $ data [$ num_linea] ['name'] = $ dataIndexed [$ num_linea] [1]; $ data [$ num_linea] ['dir_dataname'] = $ dataIndexed [$ num_linea] [0]; $ data [$ num_linea] ['qca_interv'] = $ dataIndexed [$ num_linea] [2]; }' –

0

您可以通过

debug($this->Dir->getDataSource()->getLog()); 

看起来好像你正在使用fieldList得到不正确的日志。 fieldList是要列入保存到数据库列入白名单的字段列表,而不是像您正在使用的“映射”。

您需要在数组中为每个记录指定field => value对,而不是数字索引。我可能是错的,但我从来没有看到过,根据文档,它不会那样。

+0

谢谢trigran,我通过你的解决方案得到了SQL日志。 –