FOF30\Model\DataModel\Relation\BelongsToMany::__construct PHP Méthode

__construct() public méthode

Public constructor. Initialises the relation.
public __construct ( DataModel $parentModel, string $foreignModelName, string $localKey = null, string $foreignKey = null, string $pivotTable = null, string $pivotLocalKey = null, string $pivotForeignKey = null )
$parentModel FOF30\Model\DataModel The data model we are attached to
$foreignModelName string The name of the foreign key's model in the format "modelName@com_something"
$localKey string The local table key for this relation, default: parentModel's ID field name
$foreignKey string The foreign key for this relation, default: parentModel's ID field name
$pivotTable string For many-to-many relations, the pivot (glue) table
$pivotLocalKey string For many-to-many relations, the pivot table's column storing the local key
$pivotForeignKey string For many-to-many relations, the pivot table's column storing the foreign key
    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'");
            }
        }
    }