/** * 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; }
/** * Inspects the database schema. If you pass the type of a bean this * method will return the fields of its table in the database. * The keys of this array will be the field names and the values will be * the column types used to store their values. * If no type is passed, this method returns a list of all tables in the database. * * @param string $type Type of bean (i.e. table) you want to inspect * * @return array */ public static function inspect($type = NULL) { return $type === NULL ? self::$writer->getTables() : self::$writer->getColumns($type); }