Wednesday, September 17, 2014

Array to string conversion error when writing to db

Hi,

I'm new to CakePHP, OOP and my PHP knowledge & skills could probably be considered beginner++ level, so please have patience with me.

I'm producing an event registration system for a festival website. I have created a multi-step form that uses a session to temporarily record data. It asks users to specify the ideal audience for their event, based upon age. This needs to allow multiple selections, and I want to collate and write their selections into a single mySQL database text field called 'audience'.

The code I'm using to create the multiple selections in my form view is as follows (actual form has several more options)

$options = array('5-7' => '5-7 years', '7-11' => '7-11 years', '11-14' => '7-11 years');
echo $this->Form->select('audience', $options, array('multiple' => 'checkbox'));

This generates the following HTML:

<input type="hidden" name="data[Proposal][audience]" value="" id="ProposalAudience"/>
<div class="checkbox"><input type="checkbox" name="data[Proposal][audience][]" value="5-7" id="ProposalAudience57" /><label for="ProposalAudience57">5-7 years</label></div>
<div class="checkbox"><input type="checkbox" name="data[Proposal][audience][]" value="7-11" id="ProposalAudience711" /><label for="ProposalAudience711">7-11 years</label></div>
<div class="checkbox"><input type="checkbox" name="data[Proposal][audience][]" value="11-14" id="ProposalAudience1114" /><label for="ProposalAudience1114">11-14 years</label></div>

The code I'm using in my controller file to write to the database is as follows:

$arrAudience = $this->Session->read('form.data.audience');
$strAudience = implode($arrAudience);
$this->Session->write('form.data.audience', $strAudience);
$this->Proposal->save($currentSessionData);

But I get the following errors:

Warning (2): implode() [function.implode]: Argument must be an array [APP/Controller/ProposalsController.php, line 106]
Notice (8): Array to string conversion [CORE/Cake/Model/Datasource/DboSource.php, line 1009]

The SQL query dump has the word 'Array' where I'd expect my string to be.

Where am I going wrong? Should I be using something other than the basic PHP implode function?

Thanks for any help you can give.



--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: