Yii Framework Forum: Problem with Single Table Inheritance and fields() method - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with Single Table Inheritance and fields() method Single Table Inheritance and REST

#1 User is offline   sonik_the_sonik 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 20-April 12
  • Location:italy

Posted 17 August 2017 - 11:23 AM

Hi guys, I'm developing my application architecture following this link https://github.com/s...inheritance.md. So I have something like:

backend
|-models
|--Car.php
|--SportCar.php
|--HeavyCar.php
api
|-modules
|--v1
|---models
|----ApiCar.php (extends SportCar)
|---controllers
|----CarController.php (with public $modelClass = 'api\modules\v1\models\ApiCar';)

I want to keep separated the api model fields form the backend model fields. Single Table Inheritance is working very well in the backend. Rest web service is up and running.

In the ApiCar class I added the fields() method as described at http://www.yiiframew...-resources.html to return a default subset of fields. Something like this:

public function fields() {
	return [
		'title' => function ($model) {
			return $model->name;
		},
		'description' => function ($model) {
			return $model->body;
		},
	];
}

The problem is that the fields() method is literally ignored (I debugged it with breakpoints in phpstorm) and every fields are returned. I tried moving the fields() method in the SportCar class and it is called correctly. Only title and description are returned.

The code in ApiCar model is very trivial

namespace api\modules\v1\models;

class ApiCar extends \backend\models\SportCar
{
    public function fields() {
		return [
			'title' => function ($model) {
				return $model->name;
			},
			'description' => function ($model) {
				return $model->body;
			},
		];
    }
}


And so is the controller

namespace api\modules\v1\controllers;

use api\controllers\ApiController;

class CarController extends ApiController
{
    public $modelClass = 'api\modules\v1\models\ApiCar';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['delete'], $actions['create']);
        
        return $actions;
    }
}



Can someone figure out what am I missing?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users