private function _createRelation() { $relation = new RecordRelationsRelation(); $relation->subject_id = 1; $relation->object_id = 1; $relation->property_id = 1; $relation->subject_record_type = "Item"; $relation->object_record_type = "Item"; $relation->user_id = 1; $relation->save(); $this->_relation = $relation; }
public function hookAfterSaveItem($args) { $item = $args['record']; // flag whether or not to delete all old collections for this item record $purge_collections = TRUE; // if $args['post'] is not set, this is a batch item update if (!$args['post']) { $collection_ids = array(); // if they are updating the collection ID: // 1.) Don't delete old collections // 2.) Set the $collection_ids as the collection the user selected if (!empty($item->collection_id)) { $purge_collections = FALSE; $collection_ids[] = $item->collection_id; } } else { $collection_ids = $args['post']['multicollections_collections']; } $relationTable = get_db()->getTable('RecordRelationsRelation'); $props = self::defaultParams(); $props['subject_id'] = $item->id; // delete all items old collections if the flag is set if ($purge_collections) { $currCollections = $relationTable->findBy($props); foreach ($currCollections as $collection) { $collection->delete(); } } foreach ($collection_ids as $collection_id) { $props['object_id'] = $collection_id; if ($relationTable->count($props) == 0) { $relation = new RecordRelationsRelation(); $relation->setProps($props); $relation->save(); } } }