Anyone, i'm sure this has to be close, i can't move on till i sort it and i think i have been looking at it too long to sort it.
Mick
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Anyone, i'm sure this has to be close, i can't move on till i sort it and i think i have been looking at it too long to sort it.
Mick
i get "Invalid argument supplied for foreach()"
this is when the foreach isn't getting an array... are you sure that - $data['LocationFacilities'] is an array?
try this to read what exactly the $data contains
print '<pre>';print_r($data);exit();
Hi ya,
this is the output with the print statement placed directly before the foreach statement.
Array
(
[Name] => printtest
[GridRef] => abc123
[Postcode] =>
[HeightRst] =>
[Training] => 0
[Notes] =>
[LocationFacilities] => 1,2,3,4
[SecurityID] => d11fb97bf04ae37b428be58e49fa83f37b6f711f
)
that was with all four items ticked.
so $data['LocationFacilities'] is a csv string, not an array (if it were an array, you'd see something different)
try this to get your code working...
$cos = $submission ->LocationFacilities();
$cos->addMany(explode(',',$data['LocationFacilities']));
You are a god.....
Man it worked, i don't know what that code means but will look that up, all i can say is thank you.
Mick
you're welcome.
$cos = $submission ->LocationFacilities();
$cos->addMany(explode(',',$data['LocationFacilities']));
Good to see you solved it. Just for the record, I have just found out how I can make mine work as well.
(Funnily enough, it was starting to drive me crazy that it worked with my website, and I couldn't reproduce it! )
The reason it wasn't saved was that at the point of saving your Locations item does not have an ID yet! So really all you needed to do is an extra $Submission->write() before you do $form->saveInto($Submission). Then you have to have another $Submission->write() and the records in the join table will automatically get created.
On my website, at the point where I save the many_many relations, I save them into an already existing record. That's why I didn't have your problem.
Anyway, if I strip your files down to what is essential for this example, they would look like this (I used $locations instead of $Submission) :
Locations.php
<?php
class Locations extends DataObject {
static $db = array(
'Name' => 'varchar(60)'
);
static $many_many = array(
'LocationsFacilities' => 'LocationFacilities',
);
}
-----------------------------------------
LocationFacilities.php
<?php
class LocationFacilities extends DataObject {
static $db = array(
'Name' => 'VarChar(60)'
);
static $belongs_many_many = array(
'Locations' => 'Locations'
);
}
--------------------------------
And AddLocations.php
<?php
class AddLocations extends Page {
}
class AddLocations_Controller extends Page_Controller {
function locationaddForm()
{
$fac = DataObject::get("LocationFacilities");
$map = $fac->map('ID','Name');
$form = new Form($this,'locationaddForm',new Fieldset(
new TextField('Name','Name'),
new CheckboxSetField('LocationsFacilities','Location Facilities',$map)
),
new FieldSet(
new FormAction('dolocationadd', 'Submit')
));
return $form;
}
function dolocationadd($data,$form)
{
$locations = new Locations();
$locations->write();
$form->saveInto($locations);
$locations->write();
Director::redirectBack();
}
}
So you can get Silverstripe to do it for you!
Cheers,
Katja
Morning,
Thanks for that as well, yep you were correct i made that change and it also worked you are a star.
Like i said thank you to both of you, i can now finally move on to the next stage of this project.
Both from Bristol i notice do you know each other?
Mick