/** * * Add instance of dependent model as constraint. * * @param Opus_Model_AbstractDb $model Instance of dependent model. * * @return Opus_DocumentFinder Fluent interface. */ public function setDependentModel($model) { if (!$model instanceof Opus_Model_AbstractDb) { throw new Opus_DocumentFinder_Exception('Expected instance of Opus_Model_AbstractDb.'); } $id = null; if ($model instanceof Opus_Model_Dependent_Link_Abstract) { $id = $model->getModel()->getId(); } else { $id = $model->getId(); } if (empty($id)) { throw new Opus_DocumentFinder_Exception('Id not set for model ' . get_class($model)); } // workaround for Opus_Collection[|Role] which are implemented differently if ($model instanceof Opus_Collection) { return $this->setCollectionId($id); } if ($model instanceof Opus_CollectionRole) { return $this->setCollectionRoleId($id); } if (!($model instanceof Opus_Model_Dependent_Abstract || $model instanceof Opus_Model_Dependent_Link_Abstract)) { $linkModelClass = $this->_getLinkModelClass($model); if (is_null($linkModelClass)) { throw new Opus_DocumentFinder_Exception('link model class unknown for model ' . get_class($model)); } $model = new $linkModelClass(); } if (!is_null($id)) { $id = $this->db->quote($id); } $idCol = $model->getParentIdColumn(); $tableGatewayClass = $model->getTableGatewayClass(); if (empty($tableGatewayClass)) { throw new Opus_DocumentFinder_Exception('No table gateway class provided for ' . get_class($model)); } $table = Opus_Db_TableGateway::getInstance($tableGatewayClass)->info('name'); if (empty($idCol) || empty($table)) { throw new Opus_DocumentFinder_Exception('Cannot create subquery from dependent model ' . get_class($model)); } $idCol = $this->db->quoteIdentifier($idCol); $table = $this->db->quoteIdentifier($table); if ($model instanceof Opus_Model_Dependent_Link_Abstract) { $linkedModelKey = $model->getModelKey(); if (empty($linkedModelKey)) { throw new Opus_DocumentFinder_Exception('Cannot create subquery from dependent model ' . get_class($model)); } $linkedModelKey = $this->db->quoteIdentifier($linkedModelKey); $subselect = "SELECT {$idCol}\n FROM {$table} AS l\n WHERE l.{$idCol} = d.id\n AND l.{$linkedModelKey} = {$id}"; } else { if ($model instanceof Opus_Model_Dependent_Abstract) { $subselect = "SELECT {$idCol}\n FROM {$table} AS l\n WHERE l.{$idCol} = d.id\n AND l.id = {$id}"; } else { throw new Opus_DocumentFinder_Exception('Cannot create constraint for Model ' . get_class($model)); } } $this->select->where("EXISTS ({$subselect})"); return $this; }