Difficult design decision which would be easy if PHP had multiple inheritance

Short version: I need a behavior be both, a CActiveRecordbehavior and a CModel.

Long version.

One of my tables has a text column containing XML data (the AssessmentQuestion.data column), and since I don't know how the XML format would evolve over time, I decided to use a behavior to handle it according to two parameters: an xml format version and a type (currently it can be only "multichoice").

So far so good. The next decision I've made was to use a generalized AssessmentQuestionbehavior which should be inherited by classes like AssessmentQuestionbehavior_multichoice_1_1 and contain much of the underlying functionality for all the questions.

Beside that, AssessmentQuestionbehavior should actually follow the broker pattern, and detach itself in AssessmentQuestionbehavior::afterFind() & co in the detriment of the right class for the given type/version of the question (actually I plan to make it degradable, if it doesn't find version 2.4, it should iterate backwards until it finds a class, but that's another story).

Now, the problem is that questions should receive parameters over $_GET/$_POST, so each behavior should be able to use validators, scenarios, and all the features a CModel has. At the beginning I thought I wouldn't need to copy that much from CModel (only the functionality around CValidator::createValidator), but after 30 minutes of copying around I ended up with almost the entire code from CModel (since validators call addError() & co.).

Since I hate code duplication, right now I think about using a dummy CModel and wrap around it inside AssessmentQuestionbehavior (I think it's called the composite pattern).

What other alternatives would I have, especially tightly integrated into Yii?

Thanks.

Maybe you can create a model instance in your behavior?

Yes, that's what I was saying:

Quote

Since I hate code duplication, right now I think about using a dummy CModel and wrap around it inside AssessmentQuestionbehavior (I think it's called the composite pattern).

though I think it's called the proxy pattern.

I was curios if there would be any other, more convenient way provided by Yii itself.

Why not make a new class based on CModel with your augmentation and attached behaviors? Or am I not understanding your issue correctly?

Of course the dummy Cmodel I was referring to over here

Quote

Since I hate code duplication, right now I think about using a dummy CModel and wrap around it inside AssessmentQuestionbehavior (I think it's called the composite pattern).
will be a subclass of CModel, since otherwise I wouldn't be able to set the model's safe attributes, rules, attribute labels, and so on.

Actually it is already, I've already implemented it by this time. I was only curious if there would be any other way, possibly better.

Anyway, here's the code

Does it look ok? I have the feeling that it stills need to be polished up.

Note: reset() is there because the instance itself is going to be reused. It's not by itself a singleton, though, but the singletoness is forced by the caller (it's a static member of the caller, which is the base behavior)

What exactly is "overriden" in this class?

Would you need to do that if you extended CActiveRecord instead of CModel?

And to your question for a better way, this is, IMHO, the proper way.