Listing & form

- improved Listing in order to make it more DRY
- form builder with all field types
- added support for data array in ValidModel->set()
- updated models to comply with Listing & Form methods
This commit is contained in:
Jonathan Labreuille
2015-09-14 19:07:41 +02:00
parent e471d45827
commit 79f1896cf3
19 changed files with 451 additions and 272 deletions

View File

@ -15,20 +15,6 @@ class Segment extends Model {
));
}
public static function createOrUpdate($model) {
$exists = self::where('name', $model['name'])
->find_one();
if($exists === false) {
$new_model = self::create();
$new_model->name = $model['name'];
return $new_model->save();
}
$exists->name = $model['name_updated'];
return $exists->save();
}
public function subscribers() {
return $this->has_many_through(
__NAMESPACE__.'\Subscriber',
@ -54,4 +40,28 @@ class Segment extends Model {
static function group($orm, $group = null) {
}
public static function createOrUpdate($data = array()) {
$segment = false;
if(isset($data['id']) && (int)$data['id'] > 0) {
$segment = self::findOne((int)$data['id']);
}
if($segment === false) {
$segment = self::create();
$segment->hydrate($data);
} else {
unset($data['id']);
$segment->set($data);
}
$saved = $segment->save();
if($saved === false) {
return $segment->getValidationErrors();
} else {
return true;
}
}
}