protected function getInstance()
{
//Using polymorphism to loop through the apiQueryBuilder objects
//ie) Joins, LeftJoins, etc. and call buildQueryFromParameters()
//on each of those objects
foreach ($this->apiQueryBuilders as $queryBuilder) {
$this->builder = $queryBuilder->buildQueryFromParameters($this->builder, $this->requestContainer->getInput());
}
$table = $this->requestContainer->getTable();
//must run primary key after running handleApiParams because
//any joins modifys the primaryKey to avoid ambiguity error
$primaryKey = $table . '.' . $this->requestContainer->getPrimaryKey();
$id = $this->requestContainer->getId();
if ($this->isNestedResource()) {
$instance = $this->_buildNestedResource($table, $primaryKey, $id);
} else {
$instance = $this->builder->where($primaryKey, '=', $id)->first();
}
if (!$instance) {
throw new ModelNotFoundException();
}
return $instance;
}