/** * 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 OODBBean $bean1 first bean * @param OODBBean $bean2 second bean * @param OODBBean $bean base bean (association record) * * @throws\Exception|SQL * * @return mixed */ protected function associateBeans(OODBBean $bean1, OODBBean $bean2, OODBBean $bean) { $property1 = $bean1->getMeta('type') . '_id'; $property2 = $bean2->getMeta('type') . '_id'; if ($property1 == $property2) { $property2 = $bean2->getMeta('type') . '2_id'; } //Dont mess with other tables, only add the unique constraint if: //1. the table exists (otherwise we cant inspect it) //2. the table only contains N-M fields: ID, N-ID, M-ID. $unique = array($property1, $property2); $type = $bean->getMeta('type'); $tables = $this->writer->getTables(); if (in_array($type, $tables) && !$this->oodb->isChilled($type)) { $columns = $this->writer->getColumns($type); if (count($columns) === 3 && isset($columns['id']) && isset($columns[$property1]) && isset($columns[$property2])) { $bean->setMeta('buildcommand.unique', array($unique)); } } //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 (SQL $exception) { if (!$this->writer->sqlStateIn($exception->getSQLState(), array(QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION))) { throw $exception; } } return $results; }
/** * Handles exceptions. Suppresses exceptions caused by missing structures. * * @param Exception $exception exception to handle * * @return void */ private function handleException(\Exception $exception) { if ($this->oodb->isFrozen() || !$this->writer->sqlStateIn($exception->getSQLState(), array(QueryWriter::C_SQLSTATE_NO_SUCH_TABLE, QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN))) { throw $exception; } }
/** * Nukes the entire database. * This will remove all schema structures from the database. * Only works in fluid mode. Be careful with this method. * * @warning dangerous method, will remove all tables, columns etc. * * @return void */ public static function nuke() { if (!self::$redbean->isFrozen()) { self::$writer->wipeAll(); } }