/**
  * Override insert() to check type_id (or whatever the type key is called in the table as returned by getTypeFieldName())
  * against the ca_lists list for the table (as defined by getTypeListCode())
  */
 public function insert($pa_options = null)
 {
     if (!is_a($this, "BaseRelationshipModel")) {
         global $AUTH_CURRENT_USER_ID;
         $vb_we_set_transaction = false;
         if (!$this->inTransaction()) {
             $this->setTransaction(new Transaction($this->getDb()));
             $vb_we_set_transaction = true;
         }
         $this->opo_app_plugin_manager->hookBeforeBundleInsert(array('id' => null, 'table_num' => $this->tableNum(), 'table_name' => $this->tableName(), 'instance' => $this));
         $vb_web_set_change_log_unit_id = BaseModel::setChangeLogUnitID();
         // check that type_id is valid for this table
         $t_list = new ca_lists();
         $vn_type_id = $this->get($this->getTypeFieldName());
         $va_field_info = $this->getFieldInfo($this->getTypeFieldName());
         $vb_error = false;
         if ($this->getTypeFieldName() && !(!$vn_type_id && $va_field_info['IS_NULL'])) {
             if (!($vn_ret = $t_list->itemIsEnabled($this->getTypeListCode(), $vn_type_id))) {
                 $va_type_list = $this->getTypeList(array('directChildrenOnly' => false, 'returnHierarchyLevels' => true, 'item_id' => null));
                 if (!isset($va_type_list[$vn_type_id])) {
                     $this->postError(2510, _t("Type must be specified"), "BundlableLabelableBaseModelWithAttributes->insert()", $this->tableName() . '.' . $this->getTypeFieldName());
                 } else {
                     if (is_null($vn_ret)) {
                         $this->postError(2510, _t("<em>%1</em> is invalid", $va_type_list[$vn_type_id]['name_singular']), "BundlableLabelableBaseModelWithAttributes->insert()", $this->tableName() . '.' . $this->getTypeFieldName());
                     } else {
                         $this->postError(2510, _t("<em>%1</em> is not enabled", $va_type_list[$vn_type_id]['name_singular']), "BundlableLabelableBaseModelWithAttributes->insert()", $this->tableName() . '.' . $this->getTypeFieldName());
                     }
                 }
                 $vb_error = true;
             }
             if ($this->HIERARCHY_PARENT_ID_FLD && (bool) $this->getAppConfig()->get($this->tableName() . '_enforce_strict_type_hierarchy')) {
                 // strict means if it has a parent is can only have types that are direct sub-types of the parent's type
                 // and if it is the root of the hierarchy it can only take a top-level type
                 if ($vn_parent_id = $this->get($this->HIERARCHY_PARENT_ID_FLD)) {
                     // is child
                     $t_parent = $this->_DATAMODEL->getInstanceByTableName($this->tableName());
                     if ($t_parent->load($vn_parent_id)) {
                         $vn_parent_type_id = $t_parent->getTypeID();
                         $va_type_list = $t_parent->getTypeList(array('directChildrenOnly' => $this->getAppConfig()->get($this->tableName() . '_enforce_strict_type_hierarchy') == '~' ? false : true, 'childrenOfCurrentTypeOnly' => true, 'returnHierarchyLevels' => true));
                         if (!isset($va_type_list[$this->getTypeID()])) {
                             $va_type_list = $this->getTypeList(array('directChildrenOnly' => false, 'returnHierarchyLevels' => true, 'item_id' => null));
                             $this->postError(2510, _t("<em>%1</em> is not a valid type for a child record of type <em>%2</em>", $va_type_list[$this->getTypeID()]['name_singular'], $va_type_list[$vn_parent_type_id]['name_singular']), "BundlableLabelableBaseModelWithAttributes->insert()", $this->tableName() . '.' . $this->getTypeFieldName());
                             $vb_error = true;
                         }
                     } else {
                         // error - no parent?
                         $this->postError(2510, _t("No parent was found when verifying type of new child"), "BundlableLabelableBaseModelWithAttributes->insert()", $this->tableName() . '.' . $this->getTypeFieldName());
                         $vb_error = true;
                     }
                 } else {
                     // is root
                     $va_type_list = $this->getTypeList(array('directChildrenOnly' => true, 'item_id' => null));
                     if (!isset($va_type_list[$this->getTypeID()])) {
                         $va_type_list = $this->getTypeList(array('directChildrenOnly' => false, 'returnHierarchyLevels' => true, 'item_id' => null));
                         $this->postError(2510, _t("<em>%1</em> is not a valid type for a top-level record", $va_type_list[$this->getTypeID()]['name_singular']), "BundlableLabelableBaseModelWithAttributes->insert()", $this->tableName() . '.' . $this->getTypeFieldName());
                         $vb_error = true;
                     }
                 }
             }
         }
         if (!$this->_validateIncomingAdminIDNo(true, true)) {
             $vb_error = true;
         }
         if ($vb_error) {
             // push all attributes onto errored list
             $va_inserted_attributes_that_errored = array();
             foreach ($this->opa_attributes_to_add as $va_info) {
                 $va_inserted_attributes_that_errored[$va_info['element']][] = $va_info['values'];
             }
             foreach ($va_inserted_attributes_that_errored as $vs_element => $va_list) {
                 $this->setFailedAttributeInserts($vs_element, $va_list);
             }
             if ($vb_web_set_change_log_unit_id) {
                 BaseModel::unsetChangeLogUnitID();
             }
             if ($vb_we_set_transaction) {
                 $this->removeTransaction(false);
             }
             $this->_FIELD_VALUES[$this->primaryKey()] = null;
             // clear primary key set by BaseModel::insert()
             return false;
         }
         $this->_generateSortableIdentifierValue();
         // Process "access_inherit_from_parent" flag where supported
         if ((bool) $this->getAppConfig()->get($this->tableName() . '_allow_access_inheritance') && $this->hasField('access_inherit_from_parent')) {
             // Child record with inheritance set
             if ((bool) $this->get('access_inherit_from_parent') && ($vn_parent_id = $this->get('parent_id')) > 0) {
                 $t_parent = $this->getAppDatamodel()->getInstanceByTableNum($this->tableNum(), false);
                 if ($t_parent->load($vn_parent_id)) {
                     $this->set('access', $t_parent->set('access'));
                 }
             }
         }
     }
     // stash attributes to add
     $va_attributes_added = $this->opa_attributes_to_add;
     if (!($vn_rc = parent::insert($pa_options))) {
         // push all attributes onto errored list
         $va_inserted_attributes_that_errored = array();
         foreach ($va_attributes_added as $va_info) {
             if (isset($this->opa_failed_attribute_inserts[$va_info['element']])) {
                 continue;
             }
             $va_inserted_attributes_that_errored[$va_info['element']][] = $va_info['values'];
         }
         foreach ($va_inserted_attributes_that_errored as $vs_element => $va_list) {
             $this->setFailedAttributeInserts($vs_element, $va_list);
         }
         if ($vb_web_set_change_log_unit_id) {
             BaseModel::unsetChangeLogUnitID();
         }
         if ($vb_we_set_transaction) {
             $this->removeTransaction(false);
         }
         $this->_FIELD_VALUES[$this->primaryKey()] = null;
         // clear primary key set by BaseModel::insert()
         return false;
     }
     if ($vb_web_set_change_log_unit_id) {
         BaseModel::unsetChangeLogUnitID();
     }
     $this->opo_app_plugin_manager->hookAfterBundleInsert(array('id' => $this->getPrimaryKey(), 'table_num' => $this->tableNum(), 'table_name' => $this->tableName(), 'instance' => $this));
     if ($vb_we_set_transaction) {
         $this->removeTransaction(true);
     }
     if ($vn_id = $this->getPrimaryKey()) {
         if ($this->_rowAsSearchResult = $this->makeSearchResult($this->tableName(), array($vn_id))) {
             $this->_rowAsSearchResult->nextHit();
         }
     }
     return $vn_rc;
 }