I use yii2 and find that it's active record is very convenient.
But sometimes I find that we always put logic functions in active record which I think should belongs to domain.
And I have looked up some books, most of them suggest using data mapper to mapping database record to domain.
Although it is a good way to split domain and data, I don't want to waste active record feature from yii2.
I think we can make a domain extend from active record so that the database operations will in domain's parent class active record, and business logic operations will in domain:
class UserModel extends ActiveRecord{
// do database operations
}
class UserDomain extends UserModel{
// do domain's logic
}
I don't know is this design great? Please tell me your suggests.
update #1
class UserDomain {
private $model;
public function __construct(UserModel $model){
$this->model=$model;
}
public function __set($name, $value){
if (isset($this->model->$name)) {
$this->model->$name=$value;
} else {
$this->$name=$value;
}
}
public function __get($name){
if (isset($this->model->$name)) {
return $this->model->$name;
} else {
return $this->$name;
}
}
}