/**
  * 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)
  *
  * @return mixed
  */
 protected function associateBeans(OODBBean $bean1, OODBBean $bean2, OODBBean $bean)
 {
     $type = $bean->getMeta('type');
     $property1 = $bean1->getMeta('type') . '_id';
     $property2 = $bean2->getMeta('type') . '_id';
     if ($property1 == $property2) {
         $property2 = $bean2->getMeta('type') . '2_id';
     }
     $this->oodb->store($bean1);
     $this->oodb->store($bean2);
     $bean->setMeta("cast.{$property1}", "id");
     $bean->setMeta("cast.{$property2}", "id");
     $bean->setMeta('sys.buildcommand.unique', array($property1, $property2));
     $bean->{$property1} = $bean1->id;
     $bean->{$property2} = $bean2->id;
     $results = array();
     try {
         $id = $this->oodb->store($bean);
         $results[] = $id;
     } catch (SQLException $exception) {
         if (!$this->writer->sqlStateIn($exception->getSQLState(), array(QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION))) {
             throw $exception;
         }
     }
     return $results;
 }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * Internal Query function, executes the desired query. Used by
  * all facade query functions. This keeps things DRY.
  *
  * @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 static function query($method, $sql, $bindings)
 {
     if (!self::$redbean->isFrozen()) {
         try {
             $rs = Facade::$adapter->{$method}($sql, $bindings);
         } catch (SQLException $exception) {
             if (self::$writer->sqlStateIn($exception->getSQLState(), array(QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) {
                 return $method === 'getCell' ? NULL : array();
             } else {
                 throw $exception;
             }
         }
         return $rs;
     } else {
         return Facade::$adapter->{$method}($sql, $bindings);
     }
 }