/** * Public constructor. Initialises the relation. * * @param DataModel $parentModel The data model we are attached to * @param string $foreignModelClass The class name of the foreign key's model * @param string $localKey The local table key for this relation, default: parentModel's ID field name * @param string $foreignKey The foreign key for this relation, default: parentModel's ID field name * @param string $pivotTable For many-to-many relations, the pivot (glue) table * @param string $pivotLocalKey For many-to-many relations, the pivot table's column storing the local key * @param string $pivotForeignKey For many-to-many relations, the pivot table's column storing the foreign key * * @throws DataModel\Relation\Exception\PivotTableNotFound */ public function __construct(DataModel $parentModel, $foreignModelClass, $localKey = null, $foreignKey = null, $pivotTable = null, $pivotLocalKey = null, $pivotForeignKey = null) { parent::__construct($parentModel, $foreignModelClass, $localKey, $foreignKey, $pivotTable, $pivotLocalKey, $pivotForeignKey); if (empty($localKey)) { $this->localKey = $parentModel->getIdFieldName(); } if (empty($pivotLocalKey)) { $this->pivotLocalKey = $this->localKey; } if (empty($foreignKey)) { // Get a model instance $container = Application::getInstance($this->foreignModelApp)->getContainer(); /** @var DataModel $foreignModel */ $foreignModel = DataModel::getTmpInstance($this->foreignModelApp, $this->foreignModelName, $container); $this->foreignKey = $foreignModel->getIdFieldName(); } if (empty($pivotForeignKey)) { $this->pivotForeignKey = $this->foreignKey; } if (empty($pivotTable)) { // Get the local model's name (e.g. "users") $localParts = explode('\\', $parentModel->getName()); $localName = end($localParts); $localName = strtolower($localName); // Get the foreign model's name (e.g. "groups") if (!isset($foreignModel)) { // Get a model instance $container = Application::getInstance($this->foreignModelApp)->getContainer(); /** @var DataModel $foreignModel */ $foreignModel = DataModel::getTmpInstance($this->foreignModelApp, $this->foreignModelName, $container); } $foreignParts = explode('\\', $foreignModel->getName()); $foreignName = end($foreignParts); $foreignName = strtolower($foreignName); // Get the local model's app name $class = get_class($parentModel); $localParts = explode('\\', $class); $parentModelApp = $localParts[0]; // There are two possibilities for the table name: #__app_local_foreign or #__app_foreign_local. // There are also two possibilities for an app name (local or foreign model's) $db = $parentModel->getDbo(); $prefix = $db->getPrefix(); $tableNames = array('#__' . strtolower($parentModelApp) . '_' . $localName . '_' . $foreignName, '#__' . strtolower($parentModelApp) . '_' . $foreignName . '_' . $localName, '#__' . strtolower($this->foreignModelApp) . '_' . $localName . '_' . $foreignName, '#__' . strtolower($this->foreignModelApp) . '_' . $foreignName . '_' . $localName); $allTables = $db->getTableList(); $this->pivotTable = null; foreach ($tableNames as $tableName) { $checkName = $prefix . substr($tableName, 3); if (in_array($checkName, $allTables)) { $this->pivotTable = $tableName; } } if (empty($this->pivotTable)) { throw new DataModel\Relation\Exception\PivotTableNotFound("Pivot table for many-to-many relation between '{$localName} and '{$foreignName}' not found'"); } } }
/** * Public constructor. Initialises the relation. * * @param DataModel $parentModel The data model we are attached to * @param string $foreignModelClass The class name of the foreign key's model * @param string $localKey The local table key for this relation, default: parentModel's ID field name * @param string $foreignKey The foreign key for this relation, default: parentModel's ID field name * @param string $pivotTable IGNORED * @param string $pivotLocalKey IGNORED * @param string $pivotForeignKey IGNORED */ public function __construct(DataModel $parentModel, $foreignModelClass, $localKey = null, $foreignKey = null, $pivotTable = null, $pivotLocalKey = null, $pivotForeignKey = null) { parent::__construct($parentModel, $foreignModelClass, $localKey, $foreignKey, $pivotTable, $pivotLocalKey, $pivotForeignKey); if (empty($this->localKey)) { $this->localKey = $parentModel->getIdFieldName(); } if (empty($this->foreignKey)) { $this->foreignKey = $this->localKey; } }