/** * Save fields for many-to-many relations in their pivot tables. * * @param F0FTable $table Current item table. * * @return bool True if the object can be saved successfully, false elsewhere. * @throws Exception The error message get trying to save fields into the pivot tables. */ public function onAfterStore(&$table) { // Retrieve the relations configured for this table $input = new F0FInput(); $key = $table->getConfigProviderKey() . '.relations'; $relations = $table->getConfigProvider()->get($key, array()); // Abandon the process if not a save task if (!in_array($input->getWord('task'), array('apply', 'save', 'savenew'))) { return true; } // For each relation check relative field foreach ($relations as $relation) { // Only if it is a multiple relation, sure! if ($relation['type'] == 'multiple') { // Retrive the fully qualified relation data from F0FTableRelations object $relation = array_merge(array('itemName' => $relation['itemName']), $table->getRelations()->getRelation($relation['itemName'], $relation['type'])); // Deduce the name of the field used in the form $field_name = F0FInflector::pluralize($relation['itemName']); // If field exists we catch its values! $field_values = $input->get($field_name, array(), 'array'); // If the field exists, build the correct pivot couple objects $new_couples = array(); foreach ($field_values as $value) { $new_couples[] = array($relation['ourPivotKey'] => $table->getId(), $relation['theirPivotKey'] => $value); } // Find existent relations in the pivot table $query = $table->getDbo()->getQuery(true)->select($relation['ourPivotKey'] . ', ' . $relation['theirPivotKey'])->from($relation['pivotTable'])->where($relation['ourPivotKey'] . ' = ' . $table->getId()); $existent_couples = $table->getDbo()->setQuery($query)->loadAssocList(); // Find new couples and create its foreach ($new_couples as $couple) { if (!in_array($couple, $existent_couples)) { $query = $table->getDbo()->getQuery(true)->insert($relation['pivotTable'])->columns($relation['ourPivotKey'] . ', ' . $relation['theirPivotKey'])->values($couple[$relation['ourPivotKey']] . ', ' . $couple[$relation['theirPivotKey']]); // Use database to create the new record if (!$table->getDbo()->setQuery($query)->execute()) { throw new Exception('Can\'t create the relation for the ' . $relation['pivotTable'] . ' table'); } } } // Now find the couples no more present, that will be deleted foreach ($existent_couples as $couple) { if (!in_array($couple, $new_couples)) { $query = $table->getDbo()->getQuery(true)->delete($relation['pivotTable'])->where($relation['ourPivotKey'] . ' = ' . $couple[$relation['ourPivotKey']])->where($relation['theirPivotKey'] . ' = ' . $couple[$relation['theirPivotKey']]); // Use database to create the new record if (!$table->getDbo()->setQuery($query)->execute()) { throw new Exception('Can\'t delete the relation for the ' . $relation['pivotTable'] . ' table'); } } } } } return true; }
/** * Create a relations object based on the provided F0FTable instance * * @param F0FTable $table The table instance used to initialise the relations */ public function __construct(F0FTable $table) { // Store the table $this->table = $table; // Get the table's type from its name $tableName = $table->getTableName(); $tableName = str_replace('#__', '', $tableName); $type = explode("_", $tableName); if (count($type) == 1) { $this->tableType = array_pop($type); } else { $this->componentName = array_shift($type); $this->tableType = array_pop($type); } $this->tableType = F0FInflector::singularize($this->tableType); $tableKey = $table->getKeyName(); unset($type); // Scan all table keys and look for foo_bar_id fields. These fields are used to populate parent relations. foreach ($table->getKnownFields() as $field) { // Skip the table key name if ($field == $tableKey) { continue; } if (substr($field, -3) != '_id') { continue; } $parts = explode('_', $field); // If the component type of the field is not set assume 'joomla' if (count($parts) == 2) { array_unshift($parts, 'joomla'); } // Sanity check if (count($parts) != 3) { continue; } // Make sure we skip any references back to ourselves (should be redundant, due to key field check above) if ($parts[1] == $this->tableType) { continue; } // Default item name: the name of the table, singular $itemName = F0FInflector::singularize($parts[1]); // Prefix the item name with the component name if we refer to a different component if ($parts[0] != $this->componentName) { $itemName = $parts[0] . '_' . $itemName; } // Figure out the table class $tableClass = ucfirst($parts[0]) . 'Table' . ucfirst($parts[1]); $default = empty($this->relations['parent']); $this->addParentRelation($itemName, $tableClass, $field, $field, $default); } // Get the relations from the configuration provider $key = $table->getConfigProviderKey() . '.relations'; $configRelations = $table->getConfigProvider()->get($key, array()); if (!empty($configRelations)) { foreach ($configRelations as $relation) { if (empty($relation['type'])) { continue; } if (isset($relation['pivotTable'])) { $this->addMultipleRelation($relation['itemName'], $relation['tableClass'], $relation['localKey'], $relation['ourPivotKey'], $relation['theirPivotKey'], $relation['remoteKey'], $relation['pivotTable'], $relation['default']); } else { $method = 'add' . ucfirst($relation['type']) . 'Relation'; if (!method_exists($this, $method)) { continue; } $this->{$method}($relation['itemName'], $relation['tableClass'], $relation['localKey'], $relation['remoteKey'], $relation['default']); } } } }