Get the opposite relation.
@param array|BaseUserModel|string $user initiator
@param array|BaseUserModel|string $other recipient.
@param Connection $database
@return mixed It's type depends on {$model->class}.
506782
public function opposite($user, $other, $database = null)
    {
        $model = $this->noInitModel;
        return $this->andWhere(
            [$model->createdByAttribute => BaseUserModel::compositeGUIDs($other),
        $model->otherGuidAttribute => BaseUserModel::compositeGUIDs($user)])->one($database);
    }
484
public function getDefaultDb(): ?ConnectionInterface
    {
        // By default, the DB Facade does not return the
        // any actual database connection, but rather an
        // instance of \Illuminate\Database\DatabaseManager.
        // Therefore, we make sure only to obtain its
        // "connection", to make sure that its only the connection
        // instance that we obtain.
        $manager = DB::getFacadeRoot();
        if (isset($manager)) {
            return $manager->connection();
        }
        return $manager;
    }
975348
public function getConnection()
    {
        try {
            if ($this->instancia == null) {

                $json = new JsonReader(BASE_DIR . self::PATH_CONFIG_FILE);

                if (!empty(glob(dirname(__DIR__, 4) . self::PATH_CONFIG_FILE )[0])) {
                    $json = new JsonReader(glob(dirname(__DIR__, 4) . PATH_CONFIG_FILE)[0]);
                }

                $json= $json->read();
                try {

                    $connectionCache = $json->phiber->link->connection_cache == 1 ? true : false;

                    $this->instancia = new PDO(
                        $json->phiber->link->url,
                        $json->phiber->link->user,
                        $json->phiber->link->password,
                        array(
                            PDO::ATTR_PERSISTENT => $connectionCache
                        )
                    );
                } catch (PhiberException $e) {
                    throw new PhiberException(new Internationalization("database_connection_error"));
                }
            }

            return $this->instancia;

        } catch (PhiberException $e) {
            throw new PhiberException(new Internationalization("database_connection_error"));
        }
    }
859897
protected static function buildOppositeRelation($relation)
    {
        if (!$relation) {
            return null;
        }
        $opposite = static::buildRelation($relation->recipient, $relation->initiator);
        if ($relation->relationType == static::$relationSingle) {
            $opposite->relationType = static::$relationSingle;
        } elseif ($relation->relationType == static::$relationMutual) {
            $opposite->setMutualType($relation->getMutualType());
        }
        return $opposite;
    }
924925
public function relateToOtherTables(array $otherTables)
    {
        if ($calls = $this->getGenericCalls()) {
            $tablesByModelName = [];

            /** @var TableMigration $tableObject */
            foreach ($otherTables as $tableObject) {
                $tablesByModelName[$tableObject->getModelName()] = $tableObject;
            } // foreach

            /** @var ForeignKey $call */
            foreach ($calls as $call) {
                if ($on = $call->on) {
                    /** @var TableMigration $otherTable */
                    $otherTable = @$otherTables[$on];
                    $call->on = $otherTable->getName();

                    $otherCall = clone $call;

                    if ($this->isPivotTable()) {
                        $modelNames = array_map('ucfirst', explode('_', $tableName = $this->getName()));

                        $matchedModelTables = array_intersect_key($tablesByModelName, array_flip($modelNames));
                        unset($matchedModelTables[$otherTable->getModelName()]);

                        $otherCall->isForMany(true);
                        $otherCall->isForPivotTable(true);

                        $call->on = current($matchedModelTables)->getName();
                        $otherTable->addGenericCall($otherCall->setRelatedTable(current($matchedModelTables)));
                    } // if
                    else {
                        $call->setRelatedTable($otherTable);
                        $otherTable->addAsRelationSource($otherCall->setRelatedTable($this));
                    } // else
                } // if
            } // foreach
        } // if

        return $this;
    }
125109
public function relation($column1, $operator, $column2 = null)
    {
        if (func_num_args() < 3) {
            $column2 = $operator;

            $operator = null;
        }

        $this->addRelation('AND', $column1, $operator, $column2);

        return $this;
    }
734652
protected function getNewMySqlConnection(array $config, string $connection = null)
    {
        if ($connection && isset($config[$connection])) {
            $config = array_merge($config, $config[$connection]);
        }

        return ConnectorFactory::make(FW::version())->connect($config);
    }
1050293
public function setDatabaseUser(\ACS\ACSPanelBundle\Entity\DatabaseUser $databaseUser = null)
    {
        $this->database_user = $databaseUser;

        return $this;
    }
1230479
public function getPerson(ConnectionInterface $con = null)
    {
        if ($this->aPerson === null && ($this->person_id !== null)) {
            $this->aPerson = ChildPersonQuery::create()->findPk($this->person_id, $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aPerson->addPersonTeamLinks($this);
             */
        }

        return $this->aPerson;
    }
855165
public function load(Connection $connection, array $primaryValues)
    {
        $targetAdapter = $connection->getAdapter($this->targetReflection->getAdapterName());

        if ($this->dominant) {

            $currentAdapter = $connection->getAdapter(
                $this->sourceReflection->getAdapterName()
            );
        } else{
            $currentAdapter = $targetAdapter;
        }

        $joinQuery = $currentAdapter->createSelect(
            $this->joinResource,
            [$this->joinKey, $this->referencingKey]
        );
        $joinQuery->setFilter(
            [$this->joinKey => [Filter::EQUAL => $primaryValues]]
        );

        $joinResult = $currentAdapter->execute($joinQuery);

        if (!$joinResult) {
            return [];
        }

        $joinResult = Association::groupResult(
            $joinResult,
            [$this->referencingKey, $this->joinKey]
        );

        $targetQuery = $targetAdapter->createSelect(
            $this->targetReflection->getAdapterResource()
        );
        $targetQuery->setFilter(
            [
                $this->targetReflection->getPrimaryProperty()->getUnmapped() => [
                    Entity\Filter::EQUAL => array_keys($joinResult)
                ]
            ]
        );

        $targetResult = $targetAdapter->execute($targetQuery);
        if (!$targetResult) {
            return [];
        }

        $targetResult = Association::groupResult(
            $targetResult,
            [$this->targetReflection->getPrimaryProperty()->getUnmapped()]
        );

        $result = [];
        foreach ($joinResult as $targetKey => $join) {

            foreach ($join as $originKey => $data) {
                if (!isset($targetResult[$targetKey])) {
                    throw new AssociationException(
                        "Can not merge associated result key '" . $targetKey
                        . "' not found in result from '"
                        . $this->targetReflection->getAdapterResource()
                        . "'! Maybe wrong value in join resource."
                    );
                }
                $result[$originKey][] = $targetResult[$targetKey];
            }
        }

        return $result;
    }
423598
private function getRelated($name)
    {
        $relation = &static::$relations[$name];
        $class    = $relation[0];
        $FKName   = isset($relation[1]) ? $relation[1] : static::$PKFieldName[0];

        $related = &$this->related[$name];

        return $related === null
            ? $related = new $class($this->$FKName)
            : $related;
    }
201993
protected function relation($relation, string $database = 'mysql'): array
    {
        // 处理数据形式
        if (is_array($relation)) {
            foreach ($relation as $k => $v) {
                if (is_numeric($k)) {
                    return $v;
                }
                return [$k, $v];
            }
        }

        // 字符串形式
        if (!is_string($relation)) {
            trigger_error('关联关系格式错误:' . json($relation), E_USER_ERROR);
        }

        // 分解关联键
        $matches = explode('=', $relation);

        // 如果只指明了一个字段,默认后一个字段为_id/id(表的主键)
        if (1 == count($matches)) {
            return [$relation, 'mongo' == $database ? '_id' : 'id'];
        }

        // 返回两个关联键
        return $matches;
    }
270629
public function initConnection(Connection $connection)
    {
        /** @var $pdo PDO */
        $pdo = $connection->getWrappedConnection();
        $this->fkSupported = version_compare('3.6.19', $pdo->getAttribute(PDO::ATTR_SERVER_VERSION)) < 0;
        $pdo->exec($this->buildEnableForeignKeysQuery());
    }
876841
public function leftJoin(string $table, string $first, string $operator = '=', string $two = ''): BaseQueryRelateContract
    {
        $this->query->leftjoin($table, $first, $operator, $two);

        return $this;
    }
680306
public function initConnection(Connection $connection)
    {
        /** @var $pdo PDO */
        $pdo = $connection->getWrappedConnection();
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
        $params = $connection->getParams();
        if (!isset($params['charset'])) {
            throw new RuntimeException("No connection charset specified");
        }
        $pdo->exec('SET NAMES ' . $pdo->quote($params['charset']));
    }
1168454
protected function dontSeeInDatabase($table, array $data, $connection = null)
    {
        return $this->notSeeInDatabase($table, $data, $connection);
    }
107875
public function sep($which_connection='')
    {
        $which_connection = $which_connection === '' ? $this->default_db : $which_connection;
        $adapter = $this->getAdapter($this->connectionType($which_connection));
        if ($adapter == null) {
            return '.';
        }
        return $adapter->sep();
    }