Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Assign the provided row object to the named model.  This will iterate
  * over all the fields and call setRow() on each one associated with
  * the named model so that their values can be set and retrieved for
  * editing.
  *
  * @throws \Dewdrop\Fields\Exception
  * @param string $modelName
  * @param Row $row
  * @return RowEditor
  */
 public function setRow($modelName, Row $row)
 {
     $model = $this->getModel($modelName);
     if ($model !== $row->getTable()) {
         throw new Exception('The row should be from the same table instance.');
     }
     foreach ($this->fields as $field) {
         if ($field instanceof DbField && $field->getTable() === $model) {
             $field->setRow($row);
         }
     }
     if ($this->deleteField) {
         $this->deleteField->setRow($row);
     }
     if (array_key_exists($modelName, $this->defaultsByModelName)) {
         $this->applyDefaultsToRow($row, $this->defaultsByModelName[$modelName]);
     }
     $this->rowsByName[$modelName] = $row;
     return $this;
 }