yii通过问题积极记录

时间:2017-01-18 10:24:53

标签: php activerecord yii yii2

请帮助;

我有模特:

class Service extends \yii\db\ActiveRecord{
    public function getCategory()
    {
        return $this->hasOne(ServiceCategory::className(), ['id' => 'service_category_id']);
    }
}

class ServiceOffer extends \yii\db\ActiveRecord
{
    public function getService()
    {
        return $this->hasOne(Service::className(), ['id' => 'service_id']);
    }

    public function getCategory()
    {
        return $this->hasOne(ServiceCategory::className(), ['id' => 'service_category_id'])->via('service');
    }

    public function getProperties()
    {
        return $this->hasMany(ServiceProperty::className(), ['service_category_id' => 'id'])
                    ->via('category');
    }
}

当我查询时:

$query = ServiceOffer::find()->with(['properties'])->all();

或:

$query = ServiceOffer::find()->joinWith(['properties'])->all();

我有错误:

Getting unknown property: app\models\ServiceOffer::service_category_id

但如果我这样做,请参阅查询sql:

$query = ServiceOffer::find()->joinWith(['properties']);
echo $query->prepare(\Yii::$app->db->queryBuilder)->createCommand()->rawSql;

SQL:

SELECT "service_offer".* FROM "service_offer" 
LEFT JOIN "service" ON "service_offer"."service_id" = "service"."id" 
LEFT JOIN "service_category" ON "service"."service_category_id" = "service_category"."id" 
LEFT JOIN "service_property" ON "service_category"."id" = "service_property"."service_category_id"

无问题地查询并执行

为什么ActiveRecord在ServiceOffer模型中找到service_category_id属性?

0 个答案:

没有答案