/** * Given two beans this function returns TRUE if they are associated using a * many-to-many association, FALSE otherwise. * * @throws RedBean_Exception_SQL * * @param RedBean_OODBBean $bean1 bean * @param RedBean_OODBBean $bean2 bean * * @return bool $related whether they are associated N-M */ public function areRelated(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2) { if (!$bean1->getID() || !$bean2->getID()) { return false; } $table = $this->getTable(array($bean1->getMeta("type"), $bean2->getMeta("type"))); $idfield1 = $this->writer->getIDField($bean1->getMeta("type")); $idfield2 = $this->writer->getIDField($bean2->getMeta("type")); $type = $bean1->getMeta("type"); if ($type == $bean2->getMeta("type")) { $type .= "2"; $cross = 1; } else { $cross = 0; } $property1 = $type . "_id"; $property2 = $bean2->getMeta("type") . "_id"; $value1 = (int) $bean1->{$idfield1}; $value2 = (int) $bean2->{$idfield2}; try { $rows = $this->writer->selectRecord($table, array($property1 => array($value1), $property2 => array($value2)), null); if ($cross) { $rows2 = $this->writer->selectRecord($table, array($property2 => array($value1), $property1 => array($value2)), null); $rows = array_merge($rows, $rows2); } } catch (RedBean_Exception_SQL $e) { if (!$this->writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { throw $e; } return false; } return count($rows) > 0; }
/** * Internal Query function, executes the desired query. Used by * all facade query functions. This keeps things DRY. * * @throws RedBean_Exception_SQL * * @param string $method desired query method (i.e. 'cell', 'col', 'exec' etc..) * @param string $sql the sql you want to execute * @param array $bindings array of values to be bound to query statement * * @return array */ private function query($method, $sql, $bindings) { if (!$this->redbean->isFrozen()) { try { $rs = $this->adapter->{$method}($sql, $bindings); } catch (RedBean_Exception_SQL $exception) { if ($this->writer->sqlStateIn($exception->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return $method === 'getCell' ? NULL : array(); } else { throw $exception; } } return $rs; } else { return $this->adapter->{$method}($sql, $bindings); } }
/** * Convenience function to execute Queries directly. * Executes SQL. * * @param string $sql sql * @param array $values values * * @return array $results */ public static function getCol($sql, $values = array()) { if (!self::$redbean->isFrozen()) { try { $rs = RedBean_Facade::$adapter->getCol($sql, $values); } catch (RedBean_Exception_SQL $e) { if (self::$writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return array(); } else { throw $e; } } return $rs; } else { return RedBean_Facade::$adapter->getCol($sql, $values); } }
/** * Associates a pair of beans. This method associates two beans, no matter * what types. Accepts a base bean that contains data for the linking record. * This method is used by associate. This method also accepts a base bean to be used * as the template for the link record in the database. * * @param RedBean_OODBBean $bean1 first bean * @param RedBean_OODBBean $bean2 second bean * @param RedBean_OODBBean $bean base bean (association record) * * @throws Exception|RedBean_Exception_SQL * * @return mixed */ protected function associateBeans(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, RedBean_OODBBean $bean) { $property1 = $bean1->getMeta('type') . '_id'; $property2 = $bean2->getMeta('type') . '_id'; if ($property1 == $property2) { $property2 = $bean2->getMeta('type') . '2_id'; } //add a build command for Unique Indexes $bean->setMeta('buildcommand.unique', array(array($property1, $property2))); //add a build command for Single Column Index (to improve performance in case unqiue cant be used) $indexName1 = 'index_for_' . $bean->getMeta('type') . '_' . $property1; $indexName2 = 'index_for_' . $bean->getMeta('type') . '_' . $property2; $bean->setMeta('buildcommand.indexes', array($property1 => $indexName1, $property2 => $indexName2)); $this->oodb->store($bean1); $this->oodb->store($bean2); $bean->setMeta("cast.{$property1}", "id"); $bean->setMeta("cast.{$property2}", "id"); $bean->{$property1} = $bean1->id; $bean->{$property2} = $bean2->id; $results = array(); try { $id = $this->oodb->store($bean); //On creation, add constraints.... if (!$this->oodb->isFrozen() && $bean->getMeta('buildreport.flags.created')) { $bean->setMeta('buildreport.flags.created', 0); if (!$this->oodb->isFrozen()) { $this->writer->addConstraintForTypes($bean1->getMeta('type'), $bean2->getMeta('type')); } } $results[] = $id; } catch (RedBean_Exception_SQL $exception) { if (!$this->writer->sqlStateIn($exception->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION))) { throw $exception; } } return $results; }
/** * Internal Query function, executes the desired query. Used by * all facade query functions. This keeps things DRY. * * @throws RedBean_Exception_SQL * * @param string $method desired query method (i.e. 'cell', 'col', 'exec' etc..) * @param string $sql the sql you want to execute * @param array $values array of values to be bound to query statement * * @return array $results results of query */ private static function query($method, $sql, $values) { if (!self::$redbean->isFrozen()) { try { $rs = RedBean_Facade::$adapter->{$method}($sql, $values); } catch (RedBean_Exception_SQL $e) { if (self::$writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return $method === 'getCell' ? null : array(); } else { throw $e; } } return $rs; } else { return RedBean_Facade::$adapter->{$method}($sql, $values); } }
/** * Executes SQL function but corrects for SQL states. * @param closure $func * @param mixed $default * @param string $sql * @param array $values * @return mixed $results */ private static function secureExec($func, $sql, $values, $default = NULL) { if (!self::$redbean->isFrozen()) { try { $rs = R::$adapter->{$func}($sql, $values); } catch (RedBean_Exception_SQL $e) { //die($e); if (self::$writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return $default; } else { throw $e; } } return $rs; } else { return R::$adapter->{$func}($sql, $values); } }