From 0647b00a12da158f00c0a5d614b887316648fe3e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 10 Apr 2015 16:21:01 -0500
Subject: [PATCH] oops: Fix crash installing and adding new dynamic fields

---
 include/class.dynamic_forms.php | 7 +++----
 include/class.orm.php           | 2 ++
 scp/forms.php                   | 2 +-
 scp/lists.php                   | 6 ++++--
 4 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 01f58ac1b..bcf0503dd 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -191,8 +191,7 @@ class DynamicForm extends VerySimpleModel {
             $inst->save();
             foreach ($ht['fields'] as $f) {
                 $f = DynamicFormField::create($f);
-                $f->form_id = $inst->id;
-                $f->setForm($inst);
+                $f->form = $inst;
                 $f->save();
             }
         }
@@ -391,11 +390,11 @@ Signal::connect('model.updated',
 Signal::connect('model.created',
     array('TicketForm', 'dropDynamicDataView'),
     'DynamicFormField',
-    function($o) { return $o->getForm()->get('type') == 'T'; });
+    function($o) { return $o->form->get('type') == 'T'; });
 Signal::connect('model.deleted',
     array('TicketForm', 'dropDynamicDataView'),
     'DynamicFormField',
-    function($o) { return $o->getForm()->get('type') == 'T'; });
+    function($o) { return $o->form->get('type') == 'T'; });
 // If the `name` column is in the dirty list, we would be renaming a
 // column. Delete the view instead.
 Signal::connect('model.updated',
diff --git a/include/class.orm.php b/include/class.orm.php
index 53ea93647..e7e54d95e 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -71,6 +71,8 @@ class VerySimpleModel {
     function set($field, $value) {
         // Update of foreign-key by assignment to model instance
         if (isset(static::$meta['joins'][$field])) {
+            if (!isset(static::$meta['joins'][$field]['fkey']))
+                static::_inspect();
             $j = static::$meta['joins'][$field];
             if ($j['list'] && ($value instanceof InstrumentedList)) {
                 // Magic list property
diff --git a/scp/forms.php b/scp/forms.php
index 3b7e6af04..9721c2f00 100644
--- a/scp/forms.php
+++ b/scp/forms.php
@@ -129,7 +129,7 @@ if($_POST) {
             $form->_dfields = $form->_fields = null;
             $form->save(true);
             foreach ($form_fields as $field) {
-                $field->set('form_id', $form->get('id'));
+                $field->form = $form;
                 $field->save();
             }
             // No longer adding a new form
diff --git a/scp/lists.php b/scp/lists.php
index 5247a0643..7ea71f393 100644
--- a/scp/lists.php
+++ b/scp/lists.php
@@ -117,6 +117,7 @@ if($_POST) {
             break;
         case 'add':
             if ($list=DynamicList::add($_POST, $errors)) {
+                $form = $list->getForm();
                  $msg = sprintf(__('Successfully added %s'),
                     __('this custom list'));
             } elseif ($errors) {
@@ -180,9 +181,10 @@ if($_POST) {
                 'type' => $_POST["type-new-$i"],
                 'name' => $_POST["name-new-$i"],
             ));
-            $field->setForm($form);
-            if ($field->isValid())
+            if ($field->isValid()) {
+                $field->form = $form;
                 $field->save();
+            }
             else
                 $errors["new-$i"] = $field->errors();
         }
-- 
GitLab