E
E
EVOSandru62014-12-23 03:14:45
Yii
EVOSandru6, 2014-12-23 03:14:45

How to properly inherit models in Yii?

There are 2 tables - t_product and t_service , they have the same fields: name, price, etc. I created models and generated CRUD. I did this, moved the repetitive code to the Craft class. And I got 2 neat classes:
class Craft extends CActiveRecord{ ....

class Service extends Craft
{
    public $imagePath = 'images/service';
  public function tableName()
  {
    return 't_service';
  }
}

class Product extends Craft
{
    public $imagePath = 'images/product';
  public function tableName()
  {
    return 't_product';
  }
}

But I get this error: The table "Craft" mentioned in the active record of the class "Craft" was not found in the database. I understand that it does not exist, but do not create a broken table for the sake of inheriting identical entities. And Inherit Product from Service is also somehow wrong, tell me experts, how to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2014-12-23
@Nc_Soft

class Service extends Craft
{
  public $imagePath = 'images/service';

    public static function model($className = __CLASS__) 
    {
        return parent::model($className);
    }

  public function tableName()
  {
    return 't_service';
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question