70907

Get Yii model class based on type

Question:

Is there any built in support in Yii for typed models? For instance, If I have a class called Flashlight, Lock, and Folder that all extend my Product active Record model and I want to make sure it the typed class rather than the generic via Yii relations, how would I do that?

Currently I am overwriting __call and I'm not really happy about it. I feel like this is probably a common need.

Answer1:

this is called <a href="http://www.yiiframework.com/wiki/198/single-table-inheritance/" rel="nofollow">single table inheritance</a>.

Basically you overwrite the <a href="http://www.yiiframework.com/doc/api/1.1/CActiveRecord#instantiate-detail" rel="nofollow">instantiate</a> method of the model to return the class you need:

// protected/models/Product.php protected function instantiate($attributes){ switch($attributes['type']){ case 'flashlight': $class='Flashlight'; break; case 'lock': $class='Lock'; break; case 'folder': $class='Folder'; break; default: $class=get_class($this); } $model=new $class(null); return $model; }

Recommend

  • PHP: __call called instead of __callStatic
  • php - how to send multiple xml request (web-services)
  • Silverstripe DataObjects as Pages Part 2 tutorial- sidebar issue
  • How to use soap in javascript
  • How to get not only single record but all records that belong to specific search query
  • Loopback API include filters not working as expected
  • Yii: how to show a field from a related table's related table?
  • Models with two hasOne relations to same table
  • What is wrong with this emulation of CMPXCHG16B instruction?
  • Django Migrations fail during django initialization
  • Group variable in cobol
  • R h2o.glm - issue with max_active_predictors
  • Scala: Function returning an unknown type
  • LiveData is abstract android
  • Trying to string.Join an IList
  • Magento get URL before current
  • Why use database factory in asp.net mvc?
  • Button text different than value submitted in query string
  • How can I speed up CURL tasks?
  • Moving Android View and preventing onDraw to be called over and over again
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Marklogic : Query response time is very high
  • Remove final comma from string in vb.net
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • Custom Tabgroup Appcelerator
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Why querying a date BC is changed to AD in Java?
  • Record samples being played with OpenAL
  • How to do unit test for HttpContext.Current.Server.MapPath
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Knitr HTML Loop - Some HTML output, some R output
  • Where to put my custom functions in Wordpress?
  • XCode can't find symbols for a specific iOS library/framework project
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?