public function add($item, $extraFields = null) { // Get Group.ID $itemID = null; if (is_numeric($item)) { $itemID = $item; } else { if ($item instanceof Group) { $itemID = $item->ID; } } // Check if this group is allowed to be added if ($this->canAddGroups(array($itemID))) { parent::add($item, $extraFields); } }
/** * Returns a many-to-many component, as a ManyManyList. * @param string $componentName Name of the many-many component * @return ManyManyList The set of components */ public function getManyManyComponents($componentName) { $manyManyComponent = $this->manyManyComponent($componentName); if (!$manyManyComponent) { throw new InvalidArgumentException(sprintf("DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'", $componentName, $this->class)); } list($parentClass, $componentClass, $parentField, $componentField, $table) = $manyManyComponent; // If we haven't been written yet, we can't save these relations, so use a list that handles this case if (!$this->ID) { if (!isset($this->unsavedRelations[$componentName])) { $this->unsavedRelations[$componentName] = new UnsavedRelationList($parentClass, $componentName, $componentClass); } return $this->unsavedRelations[$componentName]; } $extraFields = $this->manyManyExtraFieldsForComponent($componentName) ?: array(); /** @var ManyManyList $result */ $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $extraFields); // Store component data in query meta-data $result = $result->alterDataQuery(function ($query) use($extraFields) { $query->setQueryParam('Component.ExtraFields', $extraFields); }); if ($this->model) { $result->setDataModel($this->model); } $this->extend('updateManyManyComponents', $result); // If this is called on a singleton, then we return an 'orphaned relation' that can have the // foreignID set elsewhere. return $result->setDataQueryParam($this->getInheritableQueryParams())->forForeignID($this->ID); }
public function testAppendExtraFieldsToQuery() { $list = new ManyManyList('ManyManyListTest_ExtraFields', 'ManyManyListTest_ExtraFields_Clients', 'ManyManyListTest_ExtraFieldsID', 'ChildID', array('Worth' => 'Money', 'Reference' => 'Varchar')); // ensure that ManyManyListTest_ExtraFields_Clients.ValueCurrency is // selected. $db = DB::get_conn(); $expected = 'SELECT DISTINCT "ManyManyListTest_ExtraFields_Clients"."WorthCurrency",' . ' "ManyManyListTest_ExtraFields_Clients"."WorthAmount", "ManyManyListTest_ExtraFields_Clients"."Reference",' . ' "ManyManyListTest_ExtraFields"."ClassName", "ManyManyListTest_ExtraFields"."LastEdited",' . ' "ManyManyListTest_ExtraFields"."Created", "ManyManyListTest_ExtraFields"."ID",' . ' CASE WHEN "ManyManyListTest_ExtraFields"."ClassName" IS NOT NULL THEN' . ' "ManyManyListTest_ExtraFields"."ClassName" ELSE ' . Convert::raw2sql('ManyManyListTest_ExtraFields', true) . ' END AS "RecordClassName" FROM "ManyManyListTest_ExtraFields" INNER JOIN' . ' "ManyManyListTest_ExtraFields_Clients" ON' . ' "ManyManyListTest_ExtraFields_Clients"."ManyManyListTest_ExtraFieldsID" =' . ' "ManyManyListTest_ExtraFields"."ID"'; $this->assertSQLEquals($expected, $list->sql($parameters)); }