Thursday, September 18, 2014

Re: Array to string conversion error when writing to db

Thanks for your response Mike.

I had actually thought of using a join table, but wasn't sure how to do it in Cake. Thanks for pointing me in the right direction.  I've now set up my join table and got it all successfully working. CakePHP makes things so simple, once you've got your head around it! It's all great, apart from one thing...

I want validation to check that at least one checkbox is selected when the form is submitted, but I can't seem to get it to work.

I've tried the following code in both my Proposal model and my new Audience model.

public $validate = array('audience' => array('rule' => array('multiple', array('min' => 1))));

Here's the code for the relevant content...

// Controllers/ProposalsController.php
$this->set('audiences', $this->Proposal->Audience->find('list', array('fields'=>array('Audience.id', 'Audience.agegroup'))));

// Proposals/add.ctp
echo $this->Form->input('Audience', array('multiple' => 'checkbox'));

// HTML generated from above (edited fore brevity)
<div class="input select"><label for="AudienceAudience">Audience</label><input type="hidden" name="data[Audience][Audience]" value="" id="AudienceAudience"/>
<div class="checkbox"><input type="checkbox" name="data[Audience][Audience][]" value="1" id="AudienceAudience1" /><label for="AudienceAudience1">Suitable for all ages</label></div>
<div class="checkbox"><input type="checkbox" name="data[Audience][Audience][]" value="2" id="AudienceAudience2" /><label for="AudienceAudience2">Lower primary (5-7 years)</label></div>
<div class="checkbox"><input type="checkbox" name="data[Audience][Audience][]" value="3" id="AudienceAudience3" /><label for="AudienceAudience3">Upper primary (7-11 years)</label></div></div>

If you can help me get the validation working I'll be most grateful!

Thanks,
Mark.


On Wednesday, 17 September 2014 11:11:26 UTC+1, Mike Karthauser wrote:
Hi Mark

I'd try it a more cakey way.

Your model would have the following relationship

Registration HasAndBelongsToMany AudienceType

id, name

AudienceType being your data
array(1 => '5-7 years', 2 => '7-11 years', '3 => '7-11 years');


You'd store the relationship between the two in a join table so you'd have a table with the following fields

registration_id, audience_type_id

That would allow you to look up registrations via the audience_type_id



My feeling is this is a better way for you to link this
Read more in the book

HTH.
Mike



On 17 Sep 2014, at 11:01, MarkB <markba...@gmail.com> wrote:

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+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

....................................
Mike Karthäuser
Director, Brightstorm Ltd.

1, Brewery Court
North Street
Bristol
BS3 1JS

mi...@brightstorm.co.uk
www.brightstorm.co.uk
+44(0) 7939252144
....................................

--
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: