Moving a field from an extended class to its parent


#1

V3.6.1

What is the best way to move a field from an extended class into its parent?

I have a base class that extends DataObject called Person that contains common information such as name, email etc. I then have 3 classes that extend Person, called Staff, Visitors and Contractors that contain fields specific to that type of person.

Currently, Staff and Contractors have a location field used to store their base location, which is stored in the Staff and Contractor classes.

The client now wants to add a location to the Visitor class as well and to retrieve a single filtered list of all people (Staff, Contractors and Visitors) that are at a specific location. This gives me two options, both of which are flawed:

  1. I add Location to the Visitor class - However, this is then both untidy as common information is stored in all of the extended classes, plus it doesn’t allow me to filter a single list of all Person records at a particular location.

  2. I add Location to the Person class and remove it from the Staff and Contractor classes - However, this then loses all of the existing locations stored for the Staff and Contractors.

My question is, what is the best solution and does anyone know of a way that I can either:

  1. Select all records from the parent class filtering on the location field in the Extended class?

  2. Move the location field from the extended classes to the parent class without losing existing data?

Thoughts, more than welcome!


#2

There’s probably a one liner mysql command to do this but since I’m not an SQL expert I’d do it a clunkier way:

  1. Add a field named LocationTemp to your Person class and run dev/build
  2. Loop through every Person object and copy the value of Location to LocationTemp (I’d use a Dev Task)
  3. Remove the Location field from Staff and Contractor $db specs, and change LocationTemp to Location in your Person $db spec, but don’t dev/build yet…
  4. Rename the LocationTemp field to Location directly in your database, and delete the Location column from the Staff and Contractor tables
  5. Rebuild the DB with SilverStripe

#3

Hiya, thanks for the reply.

That is pretty much how I did it in the end, I was just curious to see if there was a wizzier way of doing it without making changes at a database level.

Cheers