public function test_deleting_an_option_deletes_associated_selections() { $option = Factory::create(new Option()); $selection = Factory::create(new Selection(), ['option_id' => $option->id]); $option->delete(); $this->assertFalse(Selection::whereId($selection->id)->exists()); }
/** * Validate and attach selection models via deferred bindings * * @param array $selections * @return void * @throws \October\Rain\Exception\ValidationException */ public function bindSelections($selections) { $collection = new Collection(); if (is_array($selections)) { // Translate our selections into a collection foreach ($selections['id'] as $i => $id) { $selection = $id ? Selection::findOrNew($id) : new Selection(); $selection->sort_order = $i; $selection->name = trim($selections['name'][$i]); $selection->validate(); $collection->push($selection); } // Ensure selections are unique $names = array_map('strtolower', array_pluck($collection->toArray(), 'name')); if (count($names) > count(array_unique($names))) { $message = Lang::get('bedard.shop::lang.selection.validation_unique'); Flash::error($message); throw new ValidationException($message); } } // Require at least one selection if ($collection->count() === 0) { $message = Lang::get('bedard.shop::lang.option.selection_required'); Flash::error($message); throw new ValidationException($message); } // Bind the collection to the option $this->bindEvent('model.afterSave', function () use($collection) { $this->saveSelections($collection); }); }