kla/php-activerecord — pull request #323 by anther (2013-05-15 15:40:12+00:00)
Title: Allow single table inheritance

This patch adds single table inheritance the same way that Yii framework goes about it.
http://www.yiiframework.com/wiki/198/single-table-inheritance.

```
class Book extends ActiveRecord\Model
{
public static $table_name = 'books';

/** @return a string representing the class to use */
public static function instantiate($attributes)
{
if(isset($attributes['name']))
{
if($attributes['name'] == 'AwesomeBook')
return 'AwesomeBook';
}

return parent::instantiate($attributes);
}
}

class AwesomeBook extends InstantiateBook
{
public static $table_name = 'books';
}

$book = new Book();
$book->name = 'AwesomeBook';
$book->save();
$awesome = Book::find($book->id);
echo get_class($awesome); #AwesomeBook

$book = new Book();
$book->name = 'NormalBook';
$book->save();
$awesome = Book::find($book->id);
echo get_class($awesome); #Book
```

I've implemented it as having to return a string instead of a class instance since Model's constructor is the only way to mass assign attributes unguarded unfortunately, and I didn't want to complicate the process of extending the method. ```return new Book($data,false,true,false);```
I'd rather be able to do it as:
```
public static function instantiate($attributes)
{
return new AwesomeBook();
/** And then have Table worry about pushing the attributes into this particular instance */
}
```
as right now I think the default constructor for Model.php is way too complicated (And possibly why it has an after_construct callback) ;p.

I like this feature because currently I've had to implement this sort of inheritance in this extremely cludgey way (Be prepared to avert your eyes in disgust =P)
```
public static function get_all_fields_as_actual_class()
{
$fields = self::all(array('order'=>'order_by ASC'));
foreach($fields as &$field)
{
$field = Field_Factory::convert_to_type($field);
}
return $fields;
}
/** And another method that I had to add just in case I did not want all of the fields at once */
public function convert_to_child()
{
return Field_Factory::convert_to_type($this);
}
```

This change would allow this behavior to be the default and prevent convoluted solutions to this pretty simple inheritance issue.

Thoughts, feelings? Emotions? =P