CakePHP没有将数据保存到数据库

时间:2014-01-20 12:58:25

标签: cakephp save

我目前正在尝试将各种记录保存到同一个模型中。

我已阅读有关数据格式的文档,我的自定义数据数组符合此要求。

这是我的功能:

$k = 0;
foreach($this->request->data['FileData'] as $data) {
    $name = substr($data['name'],0,strpos($data['name'],'_'));
    $ext = substr($data['name'],strrpos($data['name'],'.')+1);
    $display_name = str_replace('.'.$ext,'',str_replace($name . '_','',$data['name']));
    $Clinic = $this->Clinic->find('first',
        array(
            'conditions' => array(
                'Clinic.name' => strtoupper($data['owner'])
            ),
            'fields' => array('id')
        )
    );
    $Data['Upload'][$k]['clinic_id'] = $Clinic['Clinic']['id'];
    $Data['Upload'][$k]['file_name'] = $data['name'];
    $Data['Upload'][$k]['display_name'] = $display_name;
    $Data['Upload'][$k]['extension'] = $ext;
    $k++;
}
$this->Upload->create();
if ($this->Upload->saveMany($Data,array('deep'=>true))) { ** READ BELOW
    $this->Session->setFlash(__('The upload has been saved.'));
    return $this->redirect(array('action' => 'index'));
} else {
    $this->Session->setFlash(__('The upload could not be saved. Please, try again.'));
}

我没有验证错误。传递给此函数的数据如下所示:

array(
    'Upload' => array(
        (int) 0 => array(
            'clinic_id' => '72',
            'file_name' => 'MAHLUNGULU CLINIC_0_Guide_Tax_Season_2012_Insert_Afrikaans.pdf (6).pdf',
            'display_name' => '0_Guide_Tax_Season_2012_Insert_Afrikaans (6)',
            'extension' => 'pdf'
        ),
        (int) 1 => array(
            'clinic_id' => '79',
            'file_name' => 'ZAMA ZAMA CLINIC_1377256_515345465221385_1613340746_n.jpg',
            'display_name' => '1377256_515345465221385_1613340746_n',
            'extension' => 'jpg'
        ),
        (int) 2 => array(
            'clinic_id' => '72',
            'file_name' => 'MAHLUNGULU CLINIC_07reader.xlsx',
            'display_name' => '07reader',
            'extension' => 'xlsx'
        ),
        (int) 3 => array(
            'clinic_id' => '79',
            'file_name' => 'ZAMA ZAMA CLINIC_13-disturbed-i_still_havent_found_what_im_looking_for.mp3',
            'display_name' => '13-disturbed-i_still_havent_found_what_im_looking_for',
            'extension' => 'mp3'
        )
    )
)

(我知道这是奇怪的数据,但它是测试数据)

**我尝试了以下保存方法,所有这些方法都失败了:

$this->Upload->saveMany($Data,array('deep',true));
$this->Upload->saveMany($Data['Upload'],array('deep',true));
$this->Upload->save($Data);
$this->Upload->save($Data['Upload']);
$this->Upload->saveAll($Data);
$this->Upload->saveAll($Datap['Upload']);

它们都不起作用。请帮忙,我做错了什么?

2 个答案:

答案 0 :(得分:1)

您的帖子数据应采用蛋糕期望的格式,例如:data[Model][some_field] $this->modelname->save(data);

*EDIT*

看起来好一点,我认为你的数组顺序错误,应该是:

$Data[$k]['Upload']['display_name'] = $display_name;
$Data[$k]['Upload']['extension'] = $ext;  

即。数字索引中的条目是Model.key。

...稍微阅读文档,我明白了:

 For saving multiple records of single model, $data needs to be a numerically indexed      array of records like this:

 $data = array(
     array('title' => 'title 1'),
     array('title' => 'title 2'),
);


It is also acceptable to have the data in the following format:

$data = array(
     array('Article' => array('title' => 'title 1')),
     array('Article' => array('title' => 'title 2')),
); 

来自here。所以你根本不需要模型指示符,或者它必须在我给出的例子中的数字索引之后。祝好运。如果它不起作用,请在core.php中调试调试并查看生成的SQL(如果有的话)。

答案 1 :(得分:1)

$data['modelname']['dbfieldname'] $this->modelname->save(data);