saveAll hasMany在CakePHP 2.0.6中不起作用

时间:2012-03-12 12:28:53

标签: php cakephp cakephp-2.0

有关详细信息,请参阅Gist(控制器,模型等):https://gist.github.com/2017219

我的问题如下:
我有一个使用HTML Helper的普通表单。使用ajax,我添加新的输入(文件)来上传动态图像。

TipoImovel (或英文,PropertyType)
ImagemTipoImovel (或ImagePropertyType)

当我运行上传和验证数据所需的所有操作时,我得到以下数组( saveAssociated只是一个示例文本):

<?php 
// debug após sair do ImagemBehavior - responsável pelo upload

Array
(
    [TipoImovel] => Array
        (
            [empreendimento_id] => 2
            [tipo_id] => 1
            [chamada1] => <pre><span class="na">saveAssociated</span></pre>
            [chamada2] => <pre><span class="na">saveAssociated</span></pre>
            [chamada3] => <pre><span class="na">saveAssociated</span></pre>
            [descricao] => <pre><span class="na">saveAssociated</span></pre>
            [youtube_link] => saveAssociated
            [vimeo_embed] => saveAssociated
            [ativo] => 1
        )

    [ImagemTipoImovel] => Array
        (
            [0] => Array
                (
                    [tipo_imovel_id] => 
                    [imagem] => 65d7adc2c7ed519e489789fc3eb60e8a.jpg
                )

            [1] => Array
                (
                    [tipo_imovel_id] => 
                    [imagem] => f95bcebaecad405b2f7a50a2535d4c4b.jpg
                )

        )

)

之后我尝试使用SaveAll,但收取以下错误:

Notice (8): Array to string conversion [CORE/Cake/Model/Datasource/DboSource.php, line 964]

Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

SQL Query: INSERT INTO `imagem_tipo_imoveis` (`tipo_imovel_id`, `imagem`, `modified`, `created`) VALUES (23, Array, '2012-03-11 17:17:14', '2012-03-11 17:17:14')

Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp

Stack Trace
#0 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Datasource/DboSource.php(436): PDOStatement->execute(Array)
#1 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Datasource/DboSource.php(403): DboSource->_execute('INSERT INTO `im...', Array)
#2 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Datasource/DboSource.php(967): DboSource->execute('INSERT INTO `im...')
#3 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Model.php(1646): DboSource->create(Object(ImagemTipoImovel), Array, Array)
#4 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Model.php(1955): Model->save(Array, Array)
#5 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Model.php(2096): Model->saveMany(Array, Array)
#6 /home/patrick/workspace/.../cakephp/lib/Cake/Model/Model.php(1908): Model->saveAssociated(Array, Array)
#7 /home/patrick/workspace/.../cakephp/app/Controller/TipoImoveisController.php(44): Model->saveAll(Array)
#8 [internal function]: TipoImoveisController->admin_add()
#9 /home/patrick/workspace/.../cakephp/lib/Cake/Controller/Controller.php(473): ReflectionMethod->invokeArgs(Object(TipoImoveisController), Array)
#10 /home/patrick/workspace/.../cakephp/lib/Cake/Routing/Dispatcher.php(104): Controller->invokeAction(Object(CakeRequest))
#11 /home/patrick/workspace/.../cakephp/lib/Cake/Routing/Dispatcher.php(86): Dispatcher->_invoke(Object(TipoImoveisController), Object(CakeRequest), Object(CakeResponse))
#12 /home/patrick/workspace/.../cakephp/app/webroot/index.php(96): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#13 {main}

问题出在哪里?

编辑12/03/2012 21:59h

我做了一些测试,发现了以下内容: beforeValidate 没有返回图片的名称,即他之前尝试保存的第一个以前运行,所以他试图保存一个数组,然后才上传和只返回图像名称。

现在问题是:如何在保存之前运行某些东西,即使是在hasMany关系中?

2 个答案:

答案 0 :(得分:1)

尝试在TipoImovel.php中设置为true

'dependent' => true,

感谢

答案 1 :(得分:0)

尝试将saveAll更改为saveAssociated