gpt4 book ai didi

php - Yii2 在 Controller 中使用模型中的方法(REST API)

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:09 24 4
gpt4 key购买 nike

我用 Yii2 框架制作了一个简单的 REST API。我制作了一个带有一些查询的 Controller 以从数据库中获取数据。但现在我想将这些查询与 Controller 分开。我认为我应该将获取查询的函数放在模型 (ActiveRecord) 中,但我无法从我的 Controller 访问该方法。

namespace app\models;

use yii\db\ActiveRecord;

class Car extends ActiveRecord {
public function __construct(){
parent::__construct();
}

public static function tableName(){
return 'auto_new';
}

public static function primaryKey(){
return ['auto_id'];
}

// Regels voor POST callback
public function rules(){
//voorbeeld
/*
return [
[['username', 'password'], 'required']
];
*/
}

public static function getCar(){
$query = (new Query())
->select([
'a.auto_id auto_id',
'm.merk merk',
'a.model model',
'a.uitvoering uitvoering',
'k.kleur kleur',
't.transmissie transmissie',
'a.kmstand kmstand',
'a.bouwjaar bouwjaar',
'a.vermogen vermogen',
'b.brandstof brandstof',
'g.garantie garantie',
'a.prijs prijs',
'kl.postcode postcode',
'kl.woonplaats woonplaats',
'GROUP_CONCAT(DISTINCT(atn.NL)) opties',
'GROUP_CONCAT(DISTINCT(f.foto_id)) fotos'
])

->from('auto_new a')

->join('INNER JOIN', 'tbl_merken m', 'a.merk = m.merk_id') //Merk
->join('INNER JOIN', 'tbl_kleur k', 'a.kleur = k.kleur_id') //Kleur
->join('INNER JOIN', 'tbl_transmissie t', 'a.transmissie = t.transmissie_id') //Transmissie
->join('INNER JOIN', 'tbl_brandstof b', 'a.brandstof = b.brandstof_id') //Brandstof
->join('INNER JOIN', 'tbl_garantie g', 'a.garantie = g.garantie_id') //Garantie
->join('INNER JOIN', 'klanten kl', 'a.ac = kl.ac') //Klantgegevens

//Alle opties van de auto
->leftJoin('auto_accessoire acc', 'a.auto_id = acc.auto_id AND a.ac = acc.ac')
->leftJoin('tbl_accessoires_trader_new atn', 'acc.code_id = atn.code_id')
//Alle foto's van de auto
->leftJoin('auto_foto f', 'a.auto_id = f.auto_id AND a.ac = f.ac')

->groupBy('a.auto_id')
->all();
return $query;
}
}

这是我 Controller 中的方法:

public function actionGetcarsbyac($ac) {
Yii::$app->response->format = Response::FORMAT_JSON;
$model = $this->modelClass;
$query = Car::getCar();
$query->where(['a.ac' => $ac]);
return $query;
}

当我尝试打开 url 时,出现以下错误:

Call to a member function getUniqueId() on a non-object

我不知道要做什么或将这些查询方法放在哪里。

最佳答案

我认为这是因为当您输入 ->all()->one() 或任何其他 query method它为您提供了无法进一步查询的填充结果对象...

因此,当您想进一步处理该查询时,您应该返回不带查询方法的查询,然后在您的 Controller 操作中完成该查询。所以通过这样做你的代码将是这样的:

public static function getCar(){
$query = new Query();
$query->select([
'a.auto_id auto_id',
'm.merk merk',
'a.model model',
'a.uitvoering uitvoering',
'k.kleur kleur',
't.transmissie transmissie',
'a.kmstand kmstand',
'a.bouwjaar bouwjaar',
'a.vermogen vermogen',
'b.brandstof brandstof',
'g.garantie garantie',
'a.prijs prijs',
'kl.postcode postcode',
'kl.woonplaats woonplaats',
'GROUP_CONCAT(DISTINCT(atn.NL)) opties',
'GROUP_CONCAT(DISTINCT(f.foto_id)) fotos'
])

->from('auto_new a')

->join('INNER JOIN', 'tbl_merken m', 'a.merk = m.merk_id') //Merk
->join('INNER JOIN', 'tbl_kleur k', 'a.kleur = k.kleur_id') //Kleur
->join('INNER JOIN', 'tbl_transmissie t', 'a.transmissie = t.transmissie_id') //Transmissie
->join('INNER JOIN', 'tbl_brandstof b', 'a.brandstof = b.brandstof_id') //Brandstof
->join('INNER JOIN', 'tbl_garantie g', 'a.garantie = g.garantie_id') //Garantie
->join('INNER JOIN', 'klanten kl', 'a.ac = kl.ac') //Klantgegevens

//Alle opties van de auto
->leftJoin('auto_accessoire acc', 'a.auto_id = acc.auto_id AND a.ac = acc.ac')
->leftJoin('tbl_accessoires_trader_new atn', 'acc.code_id = atn.code_id')
//Alle foto's van de auto
->leftJoin('auto_foto f', 'a.auto_id = f.auto_id AND a.ac = f.ac')

->groupBy('a.auto_id')
return $query;
}

请注意,我已从 getCar() 中删除了 ->all()。现在你的 Action 会是这样的:

public function actionGetcarsbyac($ac) {
Yii::$app->response->format = Response::FORMAT_JSON;
$model = $this->modelClass;
$query = Car::getCar();
$query->where(['a.ac' => $ac])
->all(); // here you are completing your query
return $query;
}

希望这能奏效...

关于php - Yii2 在 Controller 中使用模型中的方法(REST API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com