示例#1
0
 /**
  * Associates a pair of beans. This method associates two beans, no matter
  * what types.
  *
  * @param RedBean_OODBBean $bean1 first bean
  * @param RedBean_OODBBean $bean2 second bean
  * @param RedBean_OODBBean $bean  base bean
  *
  * @return mixed $id either the link ID or null
  */
 protected function associateBeans(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, RedBean_OODBBean $bean)
 {
     $idfield1 = $this->writer->getIDField($bean1->getMeta("type"));
     $idfield2 = $this->writer->getIDField($bean2->getMeta("type"));
     $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("assoc." . $bean1->getMeta("type"), $bean1);
     $bean->setMeta("assoc." . $bean2->getMeta("type"), $bean2);
     $bean->setMeta("cast.{$property1}", "id");
     $bean->setMeta("cast.{$property2}", "id");
     $bean->{$property1} = $bean1->{$idfield1};
     $bean->{$property2} = $bean2->{$idfield2};
     try {
         $id = $this->oodb->store($bean);
         //On creation, add constraints....
         if ($this->flagUseConstraints && !$this->oodb->isFrozen() && $bean->getMeta("buildreport.flags.created")) {
             $bean->setMeta("buildreport.flags.created", 0);
             if (!$this->oodb->isFrozen()) {
                 $this->writer->addConstraint($bean1, $bean2);
             }
         }
         return $id;
     } catch (RedBean_Exception_SQL $e) {
         if (!$this->writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION))) {
             throw $e;
         }
     }
 }
示例#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.
  *
  * @param RedBean_OODBBean $bean1 first bean
  * @param RedBean_OODBBean $bean2 second bean
  * @param RedBean_OODBBean $bean  base bean
  *
  * @return mixed $id either the link ID or null
  */
 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;
     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->addConstraint($bean1, $bean2);
             }
         }
         $results[] = $id;
     } catch (RedBean_Exception_SQL $e) {
         if (!$this->writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION))) {
             throw $e;
         }
     }
 }