From ad144e127c136118eca1b2e29a9a57d1ce200717 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Wed, 30 Dec 2015 15:58:51 -0800 Subject: [PATCH 1/3] Pertains to one of the elements in #1397 - timestamps and user_id's for Custom Fields --- .../admin/CustomFieldsController.php | 5 ++- ..._timestamp_and_userId_to_custom_fields.php | 34 +++++++++++++++++ ...mestamp_and_userId_to_custom_fieldsets.php | 38 +++++++++++++++++++ app/models/CustomField.php | 4 ++ app/models/CustomFieldset.php | 5 ++- 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 app/database/migrations/2015_12_30_233509_add_timestamp_and_userId_to_custom_fields.php create mode 100644 app/database/migrations/2015_12_30_233658_add_timestamp_and_userId_to_custom_fieldsets.php diff --git a/app/controllers/admin/CustomFieldsController.php b/app/controllers/admin/CustomFieldsController.php index de08ab802f..339b3dfc9a 100644 --- a/app/controllers/admin/CustomFieldsController.php +++ b/app/controllers/admin/CustomFieldsController.php @@ -8,6 +8,7 @@ use Validator; use Redirect; use Model; use Lang; +use Sentry; class CustomFieldsController extends \BaseController { @@ -43,7 +44,7 @@ class CustomFieldsController extends \BaseController { public function store() { // - $cfset=new CustomFieldset(["name" => Input::get("name")]); + $cfset=new CustomFieldset(["name" => Input::get("name"),"user_id" => Sentry::getUser()->id]); $validator=Validator::make(Input::all(),$cfset->rules); if($validator->passes()) { $cfset->save(); @@ -76,7 +77,7 @@ class CustomFieldsController extends \BaseController { public function storeField() { - $field=new CustomField(["name" => Input::get("name"),"element" => Input::get("element")]); + $field=new CustomField(["name" => Input::get("name"),"element" => Input::get("element"),"user_id" => Sentry::getUser()->id]); if(!in_array(Input::get('format'),["ALPHA","NUMERIC","MAC","IP"])) { $field->format=Input::get("custom_format"); } else { diff --git a/app/database/migrations/2015_12_30_233509_add_timestamp_and_userId_to_custom_fields.php b/app/database/migrations/2015_12_30_233509_add_timestamp_and_userId_to_custom_fields.php new file mode 100644 index 0000000000..9b7854615a --- /dev/null +++ b/app/database/migrations/2015_12_30_233509_add_timestamp_and_userId_to_custom_fields.php @@ -0,0 +1,34 @@ +integer("user_id")->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('custom_fields', function(Blueprint $table) + { + $table->dropColumn("user_id"); + }); + } + +} diff --git a/app/database/migrations/2015_12_30_233658_add_timestamp_and_userId_to_custom_fieldsets.php b/app/database/migrations/2015_12_30_233658_add_timestamp_and_userId_to_custom_fieldsets.php new file mode 100644 index 0000000000..1d2ba523ee --- /dev/null +++ b/app/database/migrations/2015_12_30_233658_add_timestamp_and_userId_to_custom_fieldsets.php @@ -0,0 +1,38 @@ +timestamps(); + $table->integer("user_id")->nullable(); + }); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('custom_fieldsets', function(Blueprint $table) + { + $table->dropTimestamps(); + $table->dropColumn("user_id"); + }); + } + +} diff --git a/app/models/CustomField.php b/app/models/CustomField.php index d04a246480..708fc0ae35 100644 --- a/app/models/CustomField.php +++ b/app/models/CustomField.php @@ -71,6 +71,10 @@ class CustomField extends Elegant public function fieldset() { return $this->belongsToMany('CustomFieldset'); //?!?!?!?!?!? } + + public function user() { + return $this->belongsTo('User'); + } //public function diff --git a/app/models/CustomFieldset.php b/app/models/CustomFieldset.php index 2af4ae9b9f..c6ef39e92a 100644 --- a/app/models/CustomFieldset.php +++ b/app/models/CustomFieldset.php @@ -2,7 +2,6 @@ class CustomFieldset extends Elegant { protected $guarded=["id"]; - public $timestamps=false; public $rules=[ "name" => "required|unique:custom_fieldsets" @@ -16,6 +15,10 @@ class CustomFieldset extends Elegant return $this->hasMany('Model',"fieldset_id"); } + public function user() { + return $this->belongsTo('User'); //WARNING - not all CustomFieldsets have a User!! + } + public function validation_rules() { $rules=[]; From 4cb0e40abb755f6194c7c3b8d114a9eae1121da9 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Wed, 30 Dec 2015 16:08:40 -0800 Subject: [PATCH 2/3] Eager-Load CustomFieldsets and CustomFields with relations --- app/controllers/admin/CustomFieldsController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/CustomFieldsController.php b/app/controllers/admin/CustomFieldsController.php index 339b3dfc9a..a9f97d5265 100644 --- a/app/controllers/admin/CustomFieldsController.php +++ b/app/controllers/admin/CustomFieldsController.php @@ -20,7 +20,11 @@ class CustomFieldsController extends \BaseController { public function index() { // - return View::make("backend.custom_fields.index")->with("custom_fieldsets",CustomFieldset::all())->with("custom_fields",CustomField::all()); + $fieldsets=CustomFieldset::with("fields","models")->get(); + //$fieldsets=CustomFieldset::all(); + $fields=CustomField::with("fieldset")->get(); + //$fields=CustomField::all(); + return View::make("backend.custom_fields.index")->with("custom_fieldsets",$fieldsets)->with("custom_fields",$fields); } From 4be3f2700c87b10f8514f4d5e5c4c1fe163885e7 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Wed, 30 Dec 2015 16:23:13 -0800 Subject: [PATCH 3/3] Make this migration run correctly in the past, before the schema has been changed --- .../migrations/2015_09_22_003413_migrate_mac_address.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/database/migrations/2015_09_22_003413_migrate_mac_address.php b/app/database/migrations/2015_09_22_003413_migrate_mac_address.php index f1bf411c93..e87c15a921 100644 --- a/app/database/migrations/2015_09_22_003413_migrate_mac_address.php +++ b/app/database/migrations/2015_09_22_003413_migrate_mac_address.php @@ -15,6 +15,7 @@ class MigrateMacAddress extends Migration { DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $f2=new CustomFieldset(['name' => "Asset with MAC Address"]); + $f2->timestamps=false; //when this model was first created, it had no timestamps. But later on it gets them. if(!$f2->save()) { throw new Exception("couldn't save customfieldset"); }