【问题标题】:Relation two database and make join yii2关系两个数据库并加入yii2
【发布时间】:2019-07-29 10:55:33
【问题描述】:

你能帮帮我吗?如何关联并加入 .我得到error 没有名为“项目”的关系。 我在我的代码中使用 ActiveRecord:

$posts = MaKantor::find()
    ->leftJoin('project.track', '`track`.`id_office` = `m_kantor`.`kantor_id`')
    ->with('project.track')->where(['collecting_id' => $model->collecting_id])
    ->all();

和配置

    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=project',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],
    'db2' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=db_master',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],

【问题讨论】:

  • 在同一张表中使用joinWithleftJoin 有什么用处project.track

标签: yii yii2 yii2-advanced-app


【解决方案1】:

在查询中使用with('relationName')时,需要在MaKantor模型中定义关系函数。 例如:

public function getProject()
{
    return $this->hasOne(Project::className(), ['id' => 'project_id']);
}

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多