Yii2在模型中使用控制器中的方法(REST API)

时间:2016-02-15 10:53:15

标签: php api rest model yii2

我使用Yii2框架制作了一个简单的REST API。我已经制作了一个带有一些查询的控制器来从数据库中获取数据。但现在我想从控制器中分离这些查询。我认为我shuld把函数放在Model(ActiveRecord)中获取查询,但我无法从我的控制器访问该方法。

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;
    }
}

这是我控制器中的方法:

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

当我尝试打开网址时,这会给我以下错误:

  

在非对象

上调用成员函数getUniqueId()

我不知道该怎么做或在哪里放置这些查询方法。

1 个答案:

答案 0 :(得分:2)

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

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

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;
    }

请注意,我已从->all()中删除了getCar()。现在你的行动就像:

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;
}

希望这应该有用......