0

This issue is started to do my head in! I am trying to build a basic invoice system for my site, which is using CakePHP.

However, I can save almost all of the data without any issues at all, its when I want to save two fields, 'workdes' and 'price' which are arrays, every time I try and save the data I get an error of Array to String!

Heres my code:

        $test = $this->request->data; 
        foreach($test as $test2 => $tets3) {

            $this->Invoicedata->create();

            $InvoiceGroupData = array(
              'Invoicedata' => array(
                'workdes' => $this->request->data['workdes'],
                'price' => $this->request->data['price']
              )
            );
            //debug($InvoiceGroupData); die();

            $this->Invoicedata->saveMany($InvoiceGroupData);
        }

This is what is inside $this->request->data:

'workdes' => array(
    (int) 0 => 'dfgdfg',
    (int) 1 => 'sswd'
),
'price' => array(
    (int) 0 => '500',
    (int) 1 => '100'
)

This is just test data, I can have anything from one input or five. So What I am doing wrong? I thought that the use of SaveMany was that is did save Arrays into the database.

The table / model name I am trying to save to is call Invoicedata, and the two tables within that are 'workdes' and 'price'.

Anything else I have not put into the question and is needed, please ask!

Please Help....

Many Thanks

Glenn.

UPDATE ::

These are the two sections of code I have tried, and I am still getting Array to String issue!!

CODE TRY 1:

        $WorkHolder = $this->request->data['workdes'];
        $PriceHolder = $this->request->data['price'];

        $MyData = array(
                      array('Invoicedata' => array('workdes' => $WorkHolder)),
                      array('Invoicedata' => array('price' => $PriceHolder)),
            );
        foreach($MyData as $test2 => $test3) {

            $this->Invoicedata->create();



            //debug($test3);

            $this->Invoicedata->saveMany($test3);
        } 

CODE TRY 2:

            $WorkHolder = $this->request->data['workdes'];
            $PriceHolder = $this->request->data['price'];

            $MyData = array(
                      array('Invoicedata' => array('workdes' => $WorkHolder)),
                      array('Invoicedata' => array('price' => $PriceHolder)),
            );

            //debug($MyData); die();

            $this->Invoicedata->saveMany($MyData);

One with a foreach loop and one without, Code try 2 is what I tried 1st, now my code looks like code try 1. I have changed, or at lest i think I have changed my array data to that of want CakePHP needs it to be, so what am I doing wrong?

1 Answer 1

1

Try reading the documentation.

From docs:

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

Your data should look like this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.