Dinamic model creation


(Creocoder) #1

Let’s $modelClass=‘Post’;

  1. $model=CActiveRecord::model($modelClass)->findByPk(1);

  2. $model=call_user_func(array($modelClass,‘model’))->findByPk(1);

CActiveRecord is abstract class. Right choose is 2? Am I right or there is some other variant?


(Info) #2

1 will work properly,

2 will also work

Was that your question or did you try to ask something else? :)

// Edit: Ah, wait, you said:

"CActiveRecord is abstract class."

That’s right, but abstract means that you cannot instantiate it - and you don’t need to when calling a static method :)


(Creocoder) #3

Yes, you’re right. And yet 1 a bit faster and it’s shorter :)

to Daniel M

And so, your own choose: 1 or 2?


(Info) #4

I would choose (1) as it intuitively seems faster than (2). But it’s too late (~3 am in Germany) to think about that ;D


(Poppitypop) #5

The Yii core uses the first method


(Creocoder) #6

to jonah

OK, then i’ll use first method in extensions too.