SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing object type?

    Hi,

    Is it possible to change an object? I mean, in a STI configuration, let's say I have a class Person and 2 subclasses Employee and Director
    Can I update ('promote') an employee to the director staff?

    When I try to update an employee, I can change every property of the class except the type

  2. #2
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe u can create a Employee.promote2Director() method to return a clone of self in Director type... return Director.new(self) ?

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I asked this exact question about PHP years ago! http://www.sitepoint.com/forums/showthread.php?t=64171

    I think the answer is still "no", you have to go with the spirit of what someone-else is suggesting and synthesize the class upgrade.

    A little way's back I wrote an example for doing basically this with single table inheritance:
    Code:
    a_person = Person.new(:name=>'Sam')
    a_person.class # this would be 'Person'
    a_person.type = 'CoolPerson'
    a_person.save
    a_person = Person.find(a_person.id)
    a_person.class # this would be 'CoolPerson'
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I understand but if it makes sense to promote an employee to a director, what about just fixing a form select error?
    I mean it's just a field in the DB (STI -> type column) so if someone is filling a form to create a new object and makes an error for let's say
    Class Vehicule
    subclasses Car and Truck, a car can't be updated to a truck in an object approach, doesn't make sense.
    Maybe I'm just focusing on stupid things but I don't understand that

  5. #5
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'be read your thread at http://www.sitepoint.com/forums/showthread.php?t=64171
    and things are clearer now. I think I've been tangled by the STI thing where everything is in one table. The key is "think 'multiple classes'"

    Thanks for the tip


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •