Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp使用$fieldList保存多个文件(无表单)_Cakephp - Fatal编程技术网

cakephp使用$fieldList保存多个文件(无表单)

cakephp使用$fieldList保存多个文件(无表单),cakephp,Cakephp,我有一个$data数组:(构建在shell上,而不是表单上) (此处调试) 使用saveMany: $this->Dir->saveMany($data, array( 'validate' => 'false', 'fieldList' => array('name','dir_dataname', 'project_id'))); 保存失败,没有错误 我不确定我的$data数组的格式是否正确,(我不知道它是否应该有另一个级别),我根据sql选择等构建了它。但是它确

我有一个$data数组:(构建在shell上,而不是表单上)

(此处调试)

使用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。(我不想在我的$data中插入字段名)。
(没有要显示的sql_转储,因为从shell任务获取它非常麻烦)


我花了一晚上的时间想弄明白,你能给我指一下正确的方向吗?

你可以通过

debug($this->Dir->getDataSource()->getLog());
看起来您使用的
字段列表不正确
fieldList
是要保存到数据库的白名单字段列表,而不是您正在使用的“映射”


您需要在数组中为每个记录指定
field=>value
对,而不是数字索引。我可能是错的,但我从未见过这种情况,而且根据文档,情况并非如此。

IMHO,每个数组中的键都不是数据库表中的有效字段。它们应该表示与表字段相同的名称。 从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'
)
)


谢谢大家,;我要做的是遍历索引数组并构建关联结构;我会回来的。正如所怀疑的那样,我使用的是一个糟糕的$数据结构。Goggle演示了如何将索引数组转换为关联数组,然后只需保存:`foreach($dataIndexed as$num_linea=>$line_of_text){$data[$num_linea]['name'=$dataIndexed[$num_linea][1];$data[$num_linea]['dir_dataname']=$dataIndexed[$num u linea][0];$data[$num num num linea]['qca\u interv']=$dataIndexed[$num num u linea][2] `}谢谢你的指导!我循环了我的索引数组如下。你有什么建议来改进它吗?
foreach($dataIndexed as$num_linea=>$line_text){$data[$num_linea]['name']=$dataIndexed[$num_linea][1];$data[$num_linea][dir dataname]=$dataIndexed[$num u linea][0];$data[$num_linea]['qca_interv']=$dataIndexed[$num_linea][2];}
谢谢trigran,我通过您的解决方案获得了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'
)
)
$this->Dir->saveMany($data);