/** * Set a list of types for a role * * @param array $current * @return bool */ public function setTypes($current) { // Get an array of all the previous types $old = array(); $types = $this->types()->rows()->raw(); // Run through the $current array and determine if // each item is new or not $keep = array(); $add = array(); foreach ($current as $bit) { if (!isset($types[$bit])) { $add[] = intval($bit); } else { $keep[] = intval($bit); } } $remove = array_diff($old, $keep); // Remove any types in the remove list if (count($remove) > 0) { foreach ($remove as $type_id) { $row = Type::oneByRoleAndType($this->get('id'), $type_id); if (!$row->destroy()) { $this->addError($row->getError()); return false; } } } // Add any types not in the OLD list if (count($add) > 0) { foreach ($add as $type_id) { $row = Type::blank(); $row->set('role_id', $this->get('id')); $row->set('type_id', $type_id); if (!$row->save()) { $this->addError($row->getError()); return false; } } } return true; }
/** * Get a list of roles for this type * * @return object */ public function roles() { $model = new RoleType(); return $this->manyToMany('\\Components\\Resources\\Models\\Author\\Role', $model->getTableName(), 'type_id', 'role_id'); }