Esempio n. 1
0
 /**
  * Returns all the nodes that have been attached to the specified
  * parent node.
  * @param RedBean_OODBBean $parent
  * @return array $childObjects
  */
 public function children(RedBean_OODBBean $parent)
 {
     $idfield = $this->writer->getIDField($parent->getMeta("type"));
     try {
         $ids = $this->writer->selectByCrit($idfield, $parent->getMeta("type"), $this->property, intval($parent->{$idfield}));
     } catch (RedBean_Exception_SQL $e) {
         return array();
     }
     return $this->oodb->batch($parent->getMeta("type"), $ids);
 }
Esempio n. 2
0
 /**
  *
  * @param RedBean_OODBBean $parent
  * @return array $childObjects
  */
 public function children(RedBean_OODBBean $parent)
 {
     $idfield = $this->writer->getIDField($parent->getMeta("type"));
     try {
         $ids = $this->adapter->getCol("SELECT `" . $idfield . "` FROM\n\t\t\t`" . $parent->getMeta("type") . "`\n\t\t\tWHERE `" . $this->property . "` = " . intval($parent->{$idfield}) . "\n\t\t");
     } catch (RedBean_Exception_SQL $e) {
         return array();
     }
     return $this->oodb->batch($parent->getMeta("type"), $ids);
 }
Esempio n. 3
0
 /**
  * The opposite of related(). Returns all the beans that are not
  * associated with the bean provided.
  *
  * @param RedBean_OODBBean $bean   bean provided
  * @param string				$type   type of bean you are searching for
  * @param string				$sql    SQL for extra filtering
  * @param array				$values values to be inserted in SQL slots
  *
  * @return array $beans beans
  */
 public static function unrelated(RedBean_OODBBean $bean, $type, $sql = null, $values = array())
 {
     $idfield = self::$writer->getIDField($type);
     $keys = self::$associationManager->related($bean, $type);
     $rows = self::$writer->selectRecord($type, array($idfield => $keys), array($sql, $values), false, true);
     return self::$redbean->convertToBeans($type, $rows);
 }
Esempio n. 4
0
 /**
  * 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;
 }
Esempio n. 5
0
 /**
  * Creates a view with name $viewID based on $refType bean type
  * and then left-joining the specified types in $types in the given
  * order.
  *
  * @param  string $viewID  desired name of the view
  * @param  string $refType first bean type to be used as base
  * @param  array  $types   array with types to be left-joined in view
  *
  * @return boolean $success whether we created a new view (false if already exists)
  */
 public function createView($viewID, $refType, $types)
 {
     if ($this->oodb->isFrozen()) {
         return false;
     }
     $history = array();
     $tables = array_flip($this->writer->getTables());
     $refTable = $refType;
     //$this->writer->safeTable($refType, true);
     $currentTable = $refTable;
     $history[$refType] = $refType;
     foreach ($types as $t) {
         if (!isset($history[$t])) {
             $history[$t] = $t;
             $connection = array($t, $currentTable);
             sort($connection);
             $connection = implode("_", $connection);
             $connectionTable = $this->writer->safeTable($connection, true);
             if (isset($tables[$connectionTable])) {
                 //this connection exists
                 $srcPoint = $this->writer->safeTable($connection) . "." . $this->writer->safeColumn($currentTable . "_id");
                 //i.e. partic_project.project_id
                 $dstPoint = $this->writer->safeTable($currentTable) . "." . $this->writer->safeColumn($this->writer->getIDField($currentTable));
                 //i.e. project.id
                 $joins[$connection] = array($srcPoint, $dstPoint);
                 //now join the type
                 $srcPoint = $this->writer->safeTable($connection) . "." . $this->writer->safeColumn($t . "_id");
                 $dstPoint = $this->writer->safeTable($t) . "." . $this->writer->safeColumn($this->writer->getIDField($t));
                 $joins[$t] = array($srcPoint, $dstPoint);
             } else {
                 //this connection does not exist
                 $srcPoint = $this->writer->safeTable($t) . "." . $this->writer->safeColumn($currentTable . "_id");
                 $dstPoint = $this->writer->safeTable($currentTable) . "." . $this->writer->safeColumn($this->writer->getIDField($currentTable));
                 $joins[$t] = array($srcPoint, $dstPoint);
             }
         }
         //now set the new refTable
         $currentTable = $t;
     }
     try {
         $rs = (bool) $this->writer->createView($refType, $joins, $viewID);
     } catch (Exception $e) {
         throw new RedBean_Exception_SQL('Could not create view, types does not seem related (yet)..');
     }
     return $rs;
 }
 /**
  *
  * @param RedBean_OODBBean $parent
  * @return array $childObjects
  */
 public function children(RedBean_OODBBean $parent)
 {
     $idfield = $this->writer->getIDField($parent->getMeta("type"));
     try {
         /*
         $ids = $this->adapter->getCol("SELECT `".$idfield."` FROM
         	`".$parent->getMeta("type")."`
         	WHERE `".$this->property."` = ".intval( $parent->$idfield )."
         ");
         */
         /*
         		$ids = $this->adapter->getCol($this->writer->buildSimpleQuery(
         			"select",array($idfield),$parent->getMeta("type"),
         			array("name"=>$this->property,
         				  "value"=>intval($parent->$idfield),
         				  "operator"=>"=","structure"=>"")
         		));
         */
         $ids = $this->writer->selectByCrit($idfield, $parent->getMeta("type"), $this->property, intval($parent->{$idfield}));
     } catch (RedBean_Exception_SQL $e) {
         return array();
     }
     return $this->oodb->batch($parent->getMeta("type"), $ids);
 }
Esempio n. 7
0
 /**
  * Loads a batch of beans all at once.
  * This function first inspects the cache; if every element in the batch
  * is available in the cache, the function will return the collected beans
  * from the cache. If one or more beans cannot be found, the function will
  * ask oodb for the beans and update the cache.	
  * @param string $type
  * @param integer $ids
  * @return array $beans 
  */
 public function batch($type, $ids)
 {
     $idfield = $this->writer->getIDField($type);
     $collect = array();
     foreach ($ids as $id) {
         $bean = $this->fetchFromCacheByTypeID($type, $id);
         if ($bean) {
             $collect[$id] = $bean;
         }
     }
     if (count($collect) == count($ids)) {
         return $collect;
     } else {
         $beans = $this->oodb->batch($type, $ids);
         foreach ($beans as $bean) {
             $this->putInCache($bean);
         }
         return $beans;
     }
 }
Esempio n. 8
0
 /**
  * Removes all relations for a bean
  * @param RedBean_OODBBean $bean
  * @param string $type
  */
 public function clearRelations(RedBean_OODBBean $bean, $type)
 {
     $this->oodb->store($bean);
     $table = $this->getTable(array($bean->getMeta("type"), $type));
     $idfield = $this->writer->getIDField($bean->getMeta("type"));
     if ($type == $bean->getMeta("type")) {
         $property2 = $type . "2_id";
         $cross = 1;
     } else {
         $cross = 0;
     }
     $property = $bean->getMeta("type") . "_id";
     try {
         $this->writer->deleteByCrit($table, array($property => $bean->{$idfield}));
         if ($cross) {
             $this->writer->deleteByCrit($table, array($property2 => $bean->{$idfield}));
         }
     } catch (RedBean_Exception_SQL $e) {
         if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") {
             throw $e;
         }
     }
 }
Esempio n. 9
0
 /**
  * Removes all relations for a bean
  * @param RedBean_OODBBean $bean
  * @param <type> $type
  */
 public function clearRelations(RedBean_OODBBean $bean, $type)
 {
     $this->oodb->store($bean);
     $table = $this->getTable(array($bean->getMeta("type"), $type));
     $idfield = $this->writer->getIDField($bean->getMeta("type"));
     if ($type == $bean->getMeta("type")) {
         $property2 = $type . "2_id";
         $cross = 1;
     } else {
         $cross = 0;
     }
     $property = $bean->getMeta("type") . "_id";
     $sql = "DELETE FROM `{$table}`\n\t\tWHERE " . $this->adapter->escape($property) . " = " . $this->adapter->escape($bean->{$idfield});
     if ($cross) {
         $sql .= " OR  " . $this->adapter->escape($property2) . " = " . $this->adapter->escape($bean->{$idfield});
     }
     try {
         $this->adapter->exec($sql);
     } catch (RedBean_Exception_SQL $e) {
         if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") {
             throw $e;
         }
     }
 }
Esempio n. 10
0
	/**
	 * The opposite of related(). Returns all the beans that are not
	 * associated with the bean provided.
	 *
	 * @param RedBean_OODBBean $bean   bean provided
	 * @param string				$type   type of bean you are searching for
	 * @param string				$sql    SQL for extra filtering
	 * @param array				$values values to be inserted in SQL slots
	 *
	 * @return array $beans beans 
	 */
	public static function unrelated(RedBean_OODBBean $bean, $type, $sql=null, $values=array()) {
		
		$keys = self::$associationManager->related( $bean, $type );
		$idfield = self::$writer->getIDField( $type );
		$sqlSnippet = self::$writer->getSQLSnippetFilter($idfield, $keys, $sql, true);
		return self::find( $type, $sqlSnippet, $values );
	}