0
votes

See Gist for more details (Controller, Model, etc): https://gist.github.com/2017219

My problem is the following:
I have a normal form using the HTML Helper. With ajax, I add new inputs (file) to upload dynamic images.

TipoImovel (or in english, PropertyType)
ImagemTipoImovel (or ImagePropertyType)

When I run all the operations necessary to upload and validate the data, I get the following array (saveAssociated its just a sample text):

<?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
                )

        )

)

After that I try to use the SaveAll, but is charged the following error:

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}

Where is the problem?

EDIT 12/03/2012 21:59h

I did some tests and found the following: beforeValidate is not returning the name of the image, ie, the first he's trying to save for later beforeValidate run, so he tries to save an array, only to then upload and return only the image name.

Now the question is: how to run something before saving, even in a hasMany relationship?

2
have you define the relationship in both model files ? - Saiyam Patel
Why [tipo_imovel_id] => is empty? - Gabriel Santos
Please, I need the code of INSERT INTO imagem_tipo_imoveis, where is the save / saveAll to verify inconsistence?. - Gabriel Santos
I believe that the upload has to be through the model ImagemTipoImovel, not TipoImovel as it is now. And set dependent attribute to true. - Paulo Rodrigues
For updated question, you can execute something in beforeSave method even in a hasMany relationship, in any model. The inserts are made ​​separately in each model. - Paulo Rodrigues

2 Answers

1
votes

try to set true in TipoImovel.php

'dependent' => true,

thanks

0
votes

Try change saveAll to saveAssociated.