This question has been answered by m.engel. See the first response.
$package_path = $modx->getOption('core_path').'components/usravailability/model/';
$modx->addPackage('usravailability', $package_path);
$myRow = $modx->newObject('Usravailability');
$availability = $hook->getValue('hour');
foreach ($availability as $count => $day ) {
foreach ($day as $hour) {
$data = array(
'id' => $id_usr,
'day' => $count,
'time' => $hour
);
$myRow->fromArray($data);
if ( !$myRow->save() ) {
$output .= '<p>Could not create row</p>';
} else {
$output .= '<p>Created row successfully</p>';
}
}
}
//for monday hour[0][], for tuesday hour[1][] ... <input type="checkbox" name="hour[0][]" value="7:00"> <input type="checkbox" name="hour[0][]" value="8:00"> <input type="checkbox" name="hour[0][]" value="9:00"> ...
$package_path = $modx->getOption('core_path') . 'components/usravailability/model/';
$modx->addPackage('usravailability', $package_path);
$availability = $hook->getValue('hour');
foreach ($availability as $count => $day) {
foreach ($day as $hour) {
$myRow = $modx->newObject('Usravailability', array(
'id' => $id_usr,
'day' => $count,
'time' => $hour
));
if (!$myRow->save()) {
$output .= '<p>Could not create row</p>';
} else {
$output .= '<p>Created row successfully</p>';
}
}
}$modx->removeCollection('Usravailability', array( 'id' => $id_usr ));Your Problem is that you re-save one Object for every row, You have to create the new Objects inside the loop. Otherwise you will update that row for every value.
Also you don't need fromArray(), just pass it as 2nd parameter, saves some rows.
Also, be shure that "id" is not an uniq or primary key, if you want to have each timestamp in one row.
Make sure each row has a different key. If they're all [0], you'll only get the last row.
<?php
$output = '';
$id = $modx->user->get('id');
$ravailability = $hook->getValue('hour');
$package_path = $modx->getOption('core_path').'components/availability/model/';
$modx->addPackage('availability', $package_path);
$modx->removeCollection('UserAvailability', array( 'id_user' => $id ));
$key = array_keys($ravailability);
foreach ($key as $count => $day) {
foreach ($ravailability[$day] as $value) {
$myRow = $modx->newObject('UserAvailability', array(
'id_user' => $id,
'weekday' => $day,
'time' => $value
));
if ( !$myRow->save() ) {
$output .= '<p>Could not create row</p>';
}
else {
$output .= '<p>Created row successfully</p>';
}
}
}