/** * Attaches the given model(s) to the current one via its relationship * * This is kind of like calling save on an individual relationship, * except that we're attaching the models back to the parent entity. * This is helpful if you're going to call saveAndPropagate and want * to pass the parent object back to a view in the event of a save error. * * @param string $relationship The relationship to invoke * @param array|object $models The model or models to attach * @return $this * @since 2.0.0 **/ public function attach($relationship, $models) { // If we have an array, we'll put it into a rows object // (like we would if we were fetching the results from the db) if (is_array($models)) { $rows = new Rows(); foreach ($models as $model) { $rows->push($model); } } else { // Otherwise it's just a single model $rows = $models; } // Get our rows associated according to their relationship type // This means we add related keys, etc to the passed in rows $rows = $this->{$relationship}()->associate($rows); $this->addRelationship($relationship, $rows); return $this; }
/** * Checks to see if the current item is the last in the list * * @return bool * @since 2.1.0 **/ public function isLast() { if ($this->collection) { return $this->collection->isLast($this->getPkValue()); } return false; }
/** * Seeds the given rows with data * * @param \Hubzero\Database\Rows $rows The rows to seed on to * @param \Hubzero\Database\Rows $data The data from which to seed * @param string $name The relationship name * @return array * @since 2.0.0 **/ protected function seed($rows, $data, $name) { foreach ($rows as $row) { if ($related = $data->seek($row->{$this->localKey})) { $row->addRelationship($name, $related); } else { $related = $this->related; $row->addRelationship($name, $related::blank()); } } return $rows; }