/** * Returns all the beans associated with $bean. * This method will return an array containing all the beans that have * been associated once with the associate() function and are still * associated with the bean specified. The type parameter indicates the * type of beans you are looking for. You can also pass some extra SQL and * values for that SQL to filter your results after fetching the * related beans. * * Don't try to make use of subqueries, a subquery using IN() seems to * be slower than two queries! * * Since 3.2, you can now also pass an array of beans instead just one * bean as the first parameter. * * @param RedBean_OODBBean|array $bean the bean you have * @param string $type the type of beans you want * @param string $sql SQL snippet for extra filtering * @param array $bindings values to be inserted in SQL slots * @param boolean $glue whether the SQL should be prefixed with WHERE * * @return array */ public function relatedSimple($bean, $type, $sql = '', $bindings = array()) { $sql = $this->writer->glueSQLCondition($sql); $rows = $this->relatedRows($bean, $type, FALSE, $sql, $bindings); $links = array(); foreach ($rows as $key => $row) { if (!isset($links[$row['id']])) { $links[$row['id']] = array(); } $links[$row['id']][] = $row['linked_by']; unset($rows[$key]['linked_by']); } $beans = $this->oodb->convertToBeans($type, $rows); foreach ($beans as $bean) { $bean->setMeta('sys.belongs-to', $links[$bean->id]); } return $beans; }
/** * Breaks the association between two beans. This method unassociates two beans. If the * method succeeds the beans will no longer form an association. In the database * this means that the association record will be removed. This method uses the * OODB trash() method to remove the association links, thus giving FUSE models the * opportunity to hook-in additional business logic. If the $fast parameter is * set to boolean TRUE this method will remove the beans without their consent, * bypassing FUSE. This can be used to improve performance. * * @param RedBean_OODBBean $bean1 first bean * @param RedBean_OODBBean $bean2 second bean * @param boolean $fast If TRUE, removes the entries by query without FUSE */ public function unassociate(RedBean_OODBBean $bean1, RedBean_OODBBean $bean2, $fast = null) { $this->oodb->store($bean1); $this->oodb->store($bean2); $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, $fast); if ($cross) { $rows2 = $this->writer->selectRecord($table, array($property2 => array($value1), $property1 => array($value2)), null, $fast); if ($fast) { return; } $rows = array_merge($rows, $rows2); } if ($fast) { return; } $beans = $this->oodb->convertToBeans($table, $rows); foreach ($beans as $link) { $link->setMeta("assoc." . $bean1->getMeta("type"), $bean1); $link->setMeta("assoc." . $bean2->getMeta("type"), $bean2); $this->oodb->trash($link); } } 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; }
/** * Converts a series of rows to beans. * * @param string $type type * @param array $rows must contain an array of arrays. * * @return array $beans */ public static function convertToBeans($type, $rows) { return self::$redbean->convertToBeans($type, $rows); }
/** * Converts a series of rows to beans. * This method converts a series of rows to beans. * The type of the desired output beans can be specified in the * first parameter. The second parameter is meant for the database * result rows. * * @param string $type type of beans to produce * @param array $rows must contain an array of array * * @return array */ public function convertToBeans($type, $rows) { return $this->redbean->convertToBeans($type, $rows); }