Yii Framework Forum: Dinamic model creation - Yii Framework Forum

Jump to content

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

Dinamic model creation How right? Rate Topic: -----

#1 User is offline   creocoder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 199
  • Joined: 09-March 09
  • Location:*.php

Posted 18 February 2010 - 08:17 PM

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?
No good, no bad, only consequence.
0

#2 User is offline   Daniel M 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 19-January 10
  • Location:Germany, Saarland

Posted 18 February 2010 - 08:23 PM

View Postcreocoder, on 18 February 2010 - 08:17 PM, said:

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?


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 :)
Utinam ea res tibi usui fuerit!
0

#3 User is offline   creocoder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 199
  • Joined: 09-March 09
  • Location:*.php

Posted 18 February 2010 - 08:41 PM

Quote

but abstract means that you cannot instantiate it - and you don't need to when calling a static method :)

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?
No good, no bad, only consequence.
0

#4 User is offline   Daniel M 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 19-January 10
  • Location:Germany, Saarland

Posted 18 February 2010 - 08:46 PM

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
Utinam ea res tibi usui fuerit!
0

#5 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 18 February 2010 - 09:24 PM

The Yii core uses the first method
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#6 User is offline   creocoder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 199
  • Joined: 09-March 09
  • Location:*.php

Posted 18 February 2010 - 10:34 PM

to jonah
OK, then i'll use first method in extensions too.
No good, no bad, only consequence.
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