public function __construct(DataModel $parentModel, $foreignModelName, $localKey = null, $foreignKey = null, $pivotTable = null, $pivotLocalKey = null, $pivotForeignKey = null)
{
parent::__construct($parentModel, $foreignModelName, $localKey, $foreignKey, $pivotTable, $pivotLocalKey, $pivotForeignKey);
if (empty($localKey)) {
$this->localKey = $parentModel->getIdFieldName();
}
if (empty($pivotLocalKey)) {
$this->pivotLocalKey = $this->localKey;
}
if (empty($foreignKey)) {
/** @var DataModel $foreignModel */
$foreignModel = $this->getForeignModel();
$foreignModel->setIgnoreRequest(true);
$this->foreignKey = $foreignModel->getIdFieldName();
}
if (empty($pivotForeignKey)) {
$this->pivotForeignKey = $this->foreignKey;
}
if (empty($pivotTable)) {
// Get the local model's name (e.g. "users")
$localName = $parentModel->getName();
$localName = strtolower($localName);
// Get the foreign model's name (e.g. "groups")
if (!isset($foreignModel)) {
/** @var DataModel $foreignModel */
$foreignModel = $this->getForeignModel();
$foreignModel->setIgnoreRequest(true);
}
$foreignName = $foreignModel->getName();
$foreignName = strtolower($foreignName);
// Get the local model's app name
$parentModelBareComponent = $parentModel->getContainer()->bareComponentName;
$foreignModelBareComponent = $foreignModel->getContainer()->bareComponentName;
// There are two possibilities for the table name: #__component_local_foreign or #__component_foreign_local.
// There are also two possibilities for a component name (local or foreign model's)
$db = $parentModel->getDbo();
$prefix = $db->getPrefix();
$tableNames = array('#__' . strtolower($parentModelBareComponent) . '_' . $localName . '_' . $foreignName, '#__' . strtolower($parentModelBareComponent) . '_' . $foreignName . '_' . $localName, '#__' . strtolower($foreignModelBareComponent) . '_' . $localName . '_' . $foreignName, '#__' . strtolower($foreignModelBareComponent) . '_' . $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'");
}
}
}