没有不值得去解决的问题,也没有不值得去学习的技术!

Yii2 Error When Using Relational Queries: yii\base\ErrorException: Undefined index: id in \vendor\yiisoft\yii2\db\ActiveRelationTrait.php:554

在 Yii2 中,使用关联查询时,报错:yii\base\ErrorException: Undefined index: id in \vendor\yiisoft\yii2\db\ActiveRelationTrait.php:554

1. In Yii2, when using a relational query, the following error occurs: yii\base\ErrorException: Undefined index: id in \vendor\yiisoft\yii2\db\ActiveRelationTrait.php:554. See Figure 1.

In Yii2, when using a relational query, the following error occurs: yii\base\ErrorException: Undefined index: id in \vendor\yiisoft\yii2\db\ActiveRelationTrait.php:554
Figure 1
PHP

$query = ConventionFreeRegistration::find()
	->select(ConventionFreeRegistration::tableName() . '.audit_state, COUNT(' . ConventionFreeRegistration::tableName() . '.`id`) AS `count`')
	->where([
		ConventionFreeRegistration::tableName() . '.convention_id' => $conventionId,
	])
	->groupBy(ConventionFreeRegistration::tableName() . '.audit_state')
	->indexBy(ConventionFreeRegistration::tableName() . '.audit_state');

$count = $query
	->joinWith(['distributor'])
	->andWhere([ConventionDistributor::tableName() . '.user_id' => Yii::$app->user->id])
	->asArray()
	->all();

// 在模型 ConventionFreeRegistration 定义关联
    public function getDistributor() {
        return $this->hasOne(ConventionDistributor::class, ['convention_id' => 'convention_id', 'id' => 'distributor_id'])
        ->viaTable(ConventionRegistrationDistributorMapping::tableName(), ['registration_id' => 'id', 'convention_id' => 'convention_id']);
    }

2. Confirmed that the generated SQL executes without errors. See Figure 2.

Confirmed that the generated SQL executes without errors
Figure 2

3. Adjusted the code implementation by removing ->asArray(), and the error no longer occurs. However, doing this makes it impossible to retrieve the count value. Adopted a more reliable alternative approach using leftJoin, which also resolves the error. See Figure 3.

Adjusted the code implementation by removing ->asArray(), and the error no longer occurs. However, doing this makes it impossible to retrieve the count value. Adopted a more reliable alternative approach using leftJoin, which also resolves the error
Figure 3
PHP

$query = ConventionFreeRegistration::find()
	->select('t.audit_state, COUNT(t.`id`) AS `count`')
	->from(ConventionFreeRegistration::tableName() . ' t')
	->where([
		't.convention_id' => $conventionId,
	])
	->groupBy('t.audit_state')
	->indexBy('t.audit_state');

$count = $query
	->leftJoin(
		ConventionRegistrationDistributorMapping::tableName() . ' m',
		't.id = m.registration_id AND t.convention_id = m.convention_id'
	)
	->leftJoin(
		ConventionDistributor::tableName() . ' d',
		'm.convention_id = d.convention_id AND m.distributor_id = d.id'
	)
	->andWhere(['d.user_id' => Yii::$app->user->id])
	->asArray()
	->all();

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系