/** * Given an external system id and model class name, try to find the associated model if it exists. If it is * not found, a NotFoundException will be thrown. Otherwise the model will be made and returned. * @param string $id * @param string $modelClassName * @return RedBeanModel $model * @throws NotFoundException */ public static function getModelByExternalSystemIdAndModelClassName($id, $modelClassName) { assert('$id != null && is_string($id)'); assert('is_string($modelClassName)'); $tableName = $modelClassName::getTableName(); $beans = ZurmoRedBean::find($tableName, ExternalSystemIdUtil::EXTERNAL_SYSTEM_ID_COLUMN_NAME . " = '{$id}'"); assert('count($beans) <= 1'); if (count($beans) == 0) { throw new NotFoundException(); } return RedBeanModel::makeModel(end($beans), $modelClassName); }
/** * Gets a model from the database by Id. * @param $id Integer Id. * @param $modelClassName Pass only when getting it at runtime * gets the wrong name. * @return A model of the type of the extending model. */ public static function getById($id, $modelClassName = null) { assert('is_integer($id) && $id > 0'); assert('$modelClassName === null || is_string($modelClassName) && $modelClassName != ""'); // I would have thought it was correct to user R::load() and get // a null, or error or something if the bean doesn't exist, but // it still returns a bean. So until I've investigated further // I'm using Finder. if ($modelClassName === null) { $modelClassName = get_called_class(); } $tableName = self::getTableName($modelClassName); $beans = R::find($tableName, "id = '{$id}'"); assert('count($beans) <= 1'); if (count($beans) == 0) { throw new NotFoundException(); } return RedBeanModel::makeModel(end($beans), $modelClassName); }
/** * @param string $hashIndex row identifier for ContactWebFormEntry * @return array of module class names and display labels. */ public static function getByHashIndex($hashIndex) { $modelClassName = get_called_class(); $tableName = self::getTableName($modelClassName); $columnName = self::getColumnNameByAttribute('hashIndex'); $beans = R::find($tableName, "{$columnName} = '{$hashIndex}'"); assert('count($beans) <= 1'); if (count($beans) == 0) { return null; } else { return RedBeanModel::makeModel(end($beans), $modelClassName); } }
/** * Gets a currency by code. * @param $code String Code. * @return A model of type currency */ public static function getByCode($code) { assert('is_string($code)'); $tableName = self::getTableName('Currency'); $beans = R::find($tableName, "code = '{$code}'"); assert('count($beans) <= 1'); if (count($beans) == 0) { throw new NotFoundException(); } return RedBeanModel::makeModel(end($beans), 'Currency'); }
/** * Returns a model by index. Used by Iterator. * @param $i An integer index >= 0 and < count(). */ protected function getByIndex($i) { assert('is_int($i)'); assert('$i >= 0'); assert('$i < $this->count()'); $beanOrModel = $this->relatedBeansAndModels[$i]; if ($beanOrModel instanceof RedBean_OODBBean) { $model = RedBeanModel::makeModel($beanOrModel, $this->modelClassName); $this->relatedBeansAndModels[$i] = $model; } return $this->relatedBeansAndModels[$i]; }