Example #1
0
 /**
  * Handle the model (\Dewdrop\Db\Table) objects for the supplied newly added
  * DB field.  We allow custom model names for situations where you need to
  * add fields from two different instances of the same model (e.g. you have
  * an two Addresses model instances on your fields set because you have both
  * a billing and a shipping address).
  *
  * @param DbField $field
  * @param string $modelName
  * @throws Exception
  */
 protected function handleModelsForDbField(DbField $field, $modelName)
 {
     $fieldTable = $field->getTable();
     $groupName = $field->getGroupName();
     if (null === $modelName && isset($this->modelInstances[$groupName]) && $this->modelInstances[$groupName] !== $fieldTable) {
         throw new Exception('When adding fields from two instances of the same model, you must specify ' . 'an alternate model name as the second paramter to add().');
     }
     if (null === $modelName) {
         $modelName = $groupName;
     }
     if (isset($this->modelsByName[$modelName]) && $this->modelsByName[$modelName] !== $fieldTable) {
         throw new Exception("The name '{$modelName}' has already been used with another model instance. " . 'Please make sure to use model names consistently when adding fields.');
     }
     $this->modelInstances[$groupName] = $fieldTable;
     $this->modelsByName[$modelName] = $fieldTable;
     // Update the field's control name so that generated IDs, etc., use the new name
     if ($modelName !== $groupName) {
         $field->setGroupName($modelName);
     }
 }