/** * Tests if provided user is adopting current role. * * @param \de\toxa\txf\user $user user to check for adopting current role * @return boolean true on user is adopting role * @throws \LogicException */ public function isAdoptedByUser(user $user) { if (!$this->_id) { throw new \LogicException('unprepared role instance'); } $count = $this->_source->createQuery('user_role')->addCondition('user_uuid=?', true, $user->getUUID())->addCondition('role_id=?', true, $this->_id)->execute(true)->cell(); return $count > 0; }
/** * Creates query including all models of relation properly joined according * to declared references. * * @param bool $bound true to apply * @param bool $reverse true to start describing query at end point of relation * @return query */ public function createQuery($bound = true, $reverse = false) { if (!$this->isComplete()) { throw new \RuntimeException('relation is not complete'); } if (!$this->datasource) { throw new \RuntimeException('relation is not configured to use datasource, yet'); } /* * prepare to traverse nodes of relation in requested order */ if ($reverse) { $range = array(0, 1, count($this->nodes) - 1, 1); } else { $range = array(count($this->nodes) - 1, count($this->nodes) - 2, 0, -1); } /* * start query on data set of first node */ /** @var model_relation_node $start */ $start = $this->nodes[$range[0]]; $query = $this->datasource->createQuery($start->getFullName()); /* * joins in all further nodes' data sets */ $previous = $start; for ($index = $range[1]; $index != $range[2]; $index += $range[3]) { // get next node to join /** @var model_relation_node $node */ $node = $this->nodes[$index]; // compile condition to join next data set with previously joined one $there = $previous->getSuccessorNames($this->datasource, true); $here = $node->getPredecessorNames($this->datasource, true); $names = array(); for ($index = 0; $index < count($there); $index++) { $names[] = $there[$index] . '=' . $here[$index]; } // add data set $query->addDataset($node->getFullName($this->datasource), implode(' AND ', $names)); // keep track of current node being previous one in next iteration $previous = $node; } /* * add bound nodes as filters */ if ($bound) { foreach ($this->getConditionOnBoundNodes() as $condition) { $query->addFilter($condition['filter'], true, $condition['values']); } } return $query; }
/** * Retrieves query on datasource prepared to basically operate on current * model's data set. * * @param string $alias optional alias of model's data set in query * @return query customizable query on current model */ public function query($alias = null) { $alias = trim($alias); return $this->_source->createQuery(static::set() . ($alias != '' ? " {$alias}" : '')); }