getMeta( 'changed' ) ) { list( $properties, $table ) = $bean->getPropertiesAndType(); $id = $properties['id']; unset($properties['id']); $updateValues = array(); $k1 = 'property'; $k2 = 'value'; foreach( $properties as $key => $value ) { $updateValues[] = array( $k1 => $key, $k2 => $value ); } $bean->id = $this->writer->updateRecord( $table, $updateValues, $id ); $bean->setMeta( 'changed', FALSE ); } $bean->setMeta( 'tainted', FALSE ); } /** * Part of the store() functionality. * Handles all new additions after the bean has been saved. * Stores addition bean in own-list, extracts the id and * adds a foreign key. Also adds a constraint in case the type is * in the dependent list. * * Note that this method raises a custom exception if the bean * is not an instance of OODBBean. Therefore it does not use * a type hint. This allows the user to take action in case * invalid objects are passed in the list. * * @param OODBBean $bean bean to process * @param array $ownAdditions list of addition beans in own-list * * @return void * @throws RedException */ protected function processAdditions( $bean, $ownAdditions ) { $beanType = $bean->getMeta( 'type' ); $cachedIndex = array(); foreach ( $ownAdditions as $addition ) { if ( $addition instanceof OODBBean ) { $myFieldLink = $beanType . '_id'; $alias = $bean->getMeta( 'sys.alias.' . $addition->getMeta( 'type' ) ); if ( $alias ) $myFieldLink = $alias . '_id'; $addition->$myFieldLink = $bean->id; $addition->setMeta( 'cast.' . $myFieldLink, 'id' ); $this->store( $addition ); } else { throw new RedException( 'Array may only contain OODBBeans' ); } } } /** * Dispenses a new bean (a OODBBean Bean Object) * of the specified type. Always * use this function to get an empty bean object. Never * instantiate a OODBBean yourself because it needs * to be configured before you can use it with RedBean. This * function applies the appropriate initialization / * configuration for you. * * @param string $type type of bean you want to dispense * @param int $number number of beans you would like to get * @param boolean $alwaysReturnArray if TRUE always returns the result as an array * * @return OODBBean */ public function dispense( $type, $number = 1, $alwaysReturnArray = FALSE ) { $OODBBEAN = defined( 'REDBEAN_OODBBEAN_CLASS' ) ? REDBEAN_OODBBEAN_CLASS : '\RedBeanPHP\OODBBean'; $beans = array(); for ( $i = 0; $i < $number; $i++ ) { /** @var \RedBeanPHP\OODBBean $bean */ $bean = new $OODBBEAN; $bean->initializeForDispense( $type, $this->oodb->getBeanHelper() ); $this->oodb->signal( 'dispense', $bean ); $beans[] = $bean; } return ( count( $beans ) === 1 && !$alwaysReturnArray ) ? array_pop( $beans ) : $beans; } /** * Loads a bean from the object database. * It searches for a OODBBean Bean Object in the * database. It does not matter how this bean has been stored. * RedBean uses the primary key ID $id and the string $type * to find the bean. The $type specifies what kind of bean you * are looking for; this is the same type as used with the * dispense() function. If RedBean finds the bean it will return * the OODB Bean object; if it cannot find the bean * RedBean will return a new bean of type $type and with * primary key ID 0. In the latter case it acts basically the * same as dispense(). * * Important note: * If the bean cannot be found in the database a new bean of * the specified type will be generated and returned. * * @param string $type type of bean you want to load * @param integer $id ID of the bean you want to load * * @return OODBBean * @throws SQLException */ public function load( $type, $id ) { $bean = $this->dispense( $type ); if ( isset( $this->stash[$this->nesting][$id] ) ) { $row = $this->stash[$this->nesting][$id]; } else { try { $rows = $this->writer->queryRecord( $type, array( 'id' => array( $id ) ) ); } catch ( SQLException $exception ) { if ( $this->writer->sqlStateIn( $exception->getSQLState(), array( QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, QueryWriter::C_SQLSTATE_NO_SUCH_TABLE ) ) ) { throw $exception; //only throw if frozen } } if ( empty( $rows ) ) { return $bean; } $row = array_pop( $rows ); } $bean->importRow( $row ); $this->nesting++; $this->oodb->signal( 'open', $bean ); $this->nesting--; return $bean->setMeta( 'tainted', FALSE ); } }