Yii Framework Forum: Object instantiation - Yii Framework Forum

Jump to content

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

Object instantiation Rate Topic: -----

#1 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 17 December 2009 - 11:30 AM

Hi all,

I'll try to explain through a simple case:

I have a superclass: User
and a subclass: AdminUser

I do User::model()->findByPk(1), and expects a User object in return. But if $user->admin === true, it should return an AdminUser. How do i obtain that?

Thanks :)
0

#2 User is offline   lgoss007 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 01-October 09

Posted 18 December 2009 - 04:23 PM

Why do you need a separate class for an Admin? I would think it would be easier to have one user class with an admin property (and possibly better design).

I don't think you can get a subclass from a superclass because the superclass has no clue about it's children (since they can be anything). So if you really wanted to do something like that in a function you'd do:
if ($user->admin === true) return AdminUser::model()->findByPk(n);
else return $user;


But I'd really recommend just using User.

Lucas
0

#3 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 22 December 2009 - 05:29 PM

It was just an example, a more realistic scenario would be "categories".
These categories could then be extended into NewsCategories or ForumCategories with different logic, in order to minimize duplicate code, as all categories should have some shared functionality.
Another example, closer to the User example, could be to have a subclass called "OnlineUser" which extended User. Again duplicate code is eliminated as you don't have to check in each method if the User is online first in the UserOnline class.

In the book PHP Objects, Patterns, and Practice they recommend that practice, and the AdminUser example is also described in there.

Your suggestion isn't really good, while it would require overwriting a large amount of methods, for each "findBy*".
0

#4 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 22 December 2009 - 07:54 PM

you can see CActiveRecord::instantiate()
0

#5 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 23 December 2009 - 08:22 PM

Thanks, that was exactly what i needed.

Is it possible to do overriding when using findBy? Like:
DownloadCategories::model()->findAll();

returns only categories with the respective type?

:)
0

#6 User is offline   Alex Muir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 25-April 09

Posted 26 December 2009 - 06:32 AM

Do you mean using
CActiveRecord::model('DownloadCategoeries')->findAll();

My sites: The Big Eat and The Big Space
0

#7 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 28 December 2009 - 12:32 PM

View PostAlex Muir, on 26 December 2009 - 06:32 AM, said:

Do you mean using
CActiveRecord::model('DownloadCategoeries')->findAll();


Hmm.. I don't use that function, but it must be the same.
0

#8 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 03 January 2010 - 10:02 AM

View PostThomas Jensen, on 28 December 2009 - 12:32 PM, said:

Hmm.. I don't use that function, but it must be the same.

You'd probably want to use the AR scopes (see the manual for this).
You can then use something like:
$cat = Products::model()->catName1()->findAll(); // this will return all products with category as defined in scopes['catName1'];

$cat = Products::model()->catName2()->findAll(); // this will return all products with category as defined in scopes['catName2'];

Of course you can still add other selection criteria to the findAll function.
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