/** * Provide a row that can be linked to all the fields from the supplied * table. If the field has a value, we'll look up the row using the table's * find() method. Otherwise, we'll create a new row. Note that an exception * is throw if you attempt to use this linker with a field that doesn't * itself have a row associated with it already. Often you'll link to the * first row using a \Dewdrop\Fields\RowEditor\Link\QueryString rule and * then string Field linker on after that. * * @throws \Dewdrop\Fields\Exception * @param Table $table * @return \Dewdrop\Db\Row */ public function link(Table $table) { if (!$this->field->hasRow()) { $this->field->setRow($this->rowEditor->getRow($this->field->getGroupName())); } $value = $this->field->getValue(); if ($value) { $row = $table->find($value); } else { $row = $table->createRow(); } return $row; }
/** * 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); } }