/** * Move to trash. Validation will be performed * before trashing with `trash` (TrashInterface::ScenarioTrash) scenario. * * * @see TrashInterface::ScenarioTrash * @param boolean $runValidation whether to perform validation before saving the record. * If the validation fails, the record will not be saved to database. * * @return boolean * @Ignored */ public function trash($runValidation = true) { ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash); $validator = new Validator($this); if (!$runValidation || $validator->validate()) { $eventBefore = new TrashEvent($this); if (Event::hasHandler($this, TrashInterface::EventBeforeTrash)) { if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore)) { return false; } } $meta = ManganMeta::create($this); $trash = $eventBefore->getTrash(); if (empty($trash)) { $trash = new Trash(); } $trash->name = (string) $this; $trash->data = $this; $trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this); if (!$trash->save()) { return false; } $eventAfter = new TrashEvent($this); $eventAfter->setTrash($trash); if (!Event::valid($this, TrashInterface::EventAfterTrash, $eventAfter)) { return false; } // Use deleteOne, to avoid beforeDelete event, // which should be raised only when really removing document: // when emtying trash $em = new EntityManager($this); return $em->deleteOne(PkManager::prepareFromModel($this)); } return false; }