Wie kann ich eine Relation
array [
published = true
names = array [
5 => 'Eintrag 5',
8 => 'Eintrag 8',
11 => 'Eintrag 11'
]
}
am besten unter Laravel 5.x erzeugen oder einfügen, das es transaktive (ACID) gespeichert wird. Ich habe mich mal an einer Funktion probiert, allerdings stößt diese Funktion auf einen Timeout und ich weis einfach nicht warum.
Hier mein Ansatz:
array [
published = true
names = array [
5 => 'Eintrag 5',
8 => 'Eintrag 8',
11 => 'Eintrag 11'
]
}
am besten unter Laravel 5.x erzeugen oder einfügen, das es transaktive (ACID) gespeichert wird. Ich habe mich mal an einer Funktion probiert, allerdings stößt diese Funktion auf einen Timeout und ich weis einfach nicht warum.
Hier mein Ansatz:
PHP-Code:
class Region {
....
public static function updateOrCreate(array $attributes) {
// nothing to insert or update?
if ( !count($attributes) ) {
return null;
}
// update or insert
try {
DB::beginTransaction();
// insert or update the Region
$region = parent::updateOrCreate( [
'published' => (isset($attributes['published'])) ? $attributes['published'] : false
]);
// insert or update the RegionNames all region names
if ( isset($attributes['names']) )
foreach($attributes['names'] as $key => $val) {
if ( !RegionName::where('region_id', '=', $region->id)
->where('language_id', '=', $key)
->update([
'name' => $val
]) ) {
$region_name = RegionName([
'language_id' => $key,
'region_id' => $region->id,
'name' => $val
]);
$region_name->save();
}
}
// comit
DB::commit();
} catch (Exception $ex) {
// rollback
DB::rollback();
$region = null;
}
return $region;
}
}
Kommentar