public static function getTailEvents($count) { assert('is_int($count)'); $sql = "select id\n from\n (select id\n from auditevent\n order by id desc\n limit {$count}) as temp\n order by id"; $ids = ZurmoRedBean::getCol($sql); $beans = ZurmoRedBean::batch('auditevent', $ids); return self::makeModels($beans, __CLASS__); }
public static function getTailDistinctEventsByEventName($eventName, User $user, $count) { assert('is_string($eventName)'); assert('is_int($count)'); $sql = "select id\n from ( select id, modelclassname, modelid, datetime from auditevent where _user_id = {$user->id}\n AND eventname = '{$eventName}' order by id desc ) auditevent\n group by concat(modelclassname, modelid) order by datetime desc limit {$count}"; $ids = ZurmoRedBean::getCol($sql); $beans = ZurmoRedBean::batch('auditevent', $ids); return self::makeModels($beans, __CLASS__); }
/** * @depends testSetAttributesWithPostForCustomField */ public function testUpdateValueOnCustomFieldRows() { $values = array('A', 'B', 'C'); $customFieldData = CustomFieldData::getByName('updateItems'); $customFieldData->serializedData = serialize($values); $this->assertTrue($customFieldData->save()); $id = $customFieldData->id; $customField = new CustomField(); $customField->value = 'A'; $customField->data = $customFieldData; $this->assertTrue($customField->save()); $customField = new CustomField(); $customField->value = 'B'; $customField->data = $customFieldData; $this->assertTrue($customField->save()); $customField = new CustomField(); $customField->value = 'C'; $customField->data = $customFieldData; $this->assertTrue($customField->save()); $customField = new CustomField(); $customField->value = 'C'; $customField->data = $customFieldData; $this->assertTrue($customField->save()); $quote = DatabaseCompatibilityUtil::getQuote(); $customFieldTableName = CustomField::getTableName(); $baseCustomFieldTableName = BaseCustomField::getTableName(); $valueAttributeColumnName = 'value'; $dataAttributeColumnName = RedBeanModel::getForeignKeyName('CustomField', 'data'); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id}"; $ids = ZurmoRedBean::getCol($sql); $beans = ZurmoRedBean::batch($customFieldTableName, $ids); $customFields = RedBeanModel::makeModels($beans, 'CustomField'); $this->assertEquals(4, count($customFields)); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} "; $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'B'"; $this->assertEquals(1, count(ZurmoRedBean::getCol($sql))); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} "; $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'C'"; $this->assertEquals(2, count(ZurmoRedBean::getCol($sql))); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} "; $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'E'"; $this->assertEquals(0, count(ZurmoRedBean::getCol($sql))); CustomField::updateValueByDataIdAndOldValueAndNewValue($id, 'C', 'E'); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} "; $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'B'"; $this->assertEquals(1, count(ZurmoRedBean::getCol($sql))); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} "; $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'C'"; $this->assertEquals(0, count(ZurmoRedBean::getCol($sql))); $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} "; $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on "; $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = "; $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id "; $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} "; $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'E'"; $this->assertEquals(2, count(ZurmoRedBean::getCol($sql))); }
/** * Handles constructing the relatedBeansAndModels with special attention to the case where it is PolyOneToMany * @param string $modelClassName * @param mixed $sqlOrBean */ private function constructRelatedBeansAndModels($modelClassName, $sqlOrBean = '') { assert('is_string($sqlOrBean) || $sqlOrBean instanceof RedBean_OODBBean'); $tableName = $modelClassName::getTableName(); if (is_string($sqlOrBean)) { $this->relatedBeansAndModels = array_values($beans = ZurmoRedBean::find($tableName, $sqlOrBean)); } else { assert('$sqlOrBean instanceof RedBean_OODBBean'); $this->bean = $sqlOrBean; try { if ($this->bean->id > 0) { if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC) { $value = array(); $values['id'] = $this->bean->id; $values['type'] = $this->bean->getMeta('type'); $this->relatedBeansAndModels = array_values(ZurmoRedBean::find($tableName, strtolower($this->linkName) . '_id = :id AND ' . strtolower($this->linkName) . '_type = :type', $values)); } else { $relatedIds = ZurmoRedBeanLinkManager::getKeys($this->bean, $tableName, $this->resolveLinkNameForCasing()); $this->relatedBeansAndModels = array_values(ZurmoRedBean::batch($tableName, $relatedIds)); } } else { $this->relatedBeansAndModels = array(); } } catch (RedBean_Exception_SQL $e) { // SQLSTATE[42S02]: Base table or view not found... // SQLSTATE[42S22]: Column not found... if (!in_array($e->getSQLState(), array('42S02', '42S22'))) { throw $e; } $this->relatedBeansAndModels = array(); } } }
/** * Given a table name, count, and offset get an array of beans. * @param string $tableName * @param integer $count * @param integer $offset * @return array of RedBean_OODBBean beans. */ public static function getSubset($tableName, $where = null, $count = null, $offset = null) { assert('is_string($tableName)'); assert('$offset === null || is_integer($offset) && $offset >= 0'); assert('$offset === null || is_integer($count) && $count >= 1'); $sql = 'select id from ' . $tableName; if ($where != null) { $sql .= ' where ' . $where; } if ($count !== null) { $sql .= " limit {$count}"; } if ($offset !== null) { $sql .= " offset {$offset}"; } $ids = ZurmoRedBean::getCol($sql); return ZurmoRedBean::batch($tableName, $ids); }
/** * Gets a range of models from the database of the named model type. * @param $modelClassName * @param $joinTablesAdapter null or instance of joinTablesAdapter. * @param $offset The zero based index of the first model to be returned. * @param $count The number of models to be returned. * @param $where * @param $orderBy - sql string. Example 'a desc' or 'a.b desc'. Currently only supports non-related attributes * @param $modelClassName Pass only when getting it at runtime gets the wrong name. * @return An array of models of the type of the extending model. */ public static function getSubset(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter = null, $offset = null, $count = null, $where = null, $orderBy = null, $modelClassName = null, $selectDistinct = false) { assert('$offset === null || is_integer($offset) && $offset >= 0'); assert('$count === null || is_integer($count) && $count >= 1'); assert('$where === null || is_string ($where) && $where != ""'); assert('$orderBy === null || is_string ($orderBy) && $orderBy != ""'); assert('$modelClassName === null || is_string($modelClassName) && $modelClassName != ""'); if ($modelClassName === null) { $modelClassName = get_called_class(); } $ids = self::getSubsetIds($joinTablesAdapter, $offset, $count, $where, $orderBy, $modelClassName, $selectDistinct); $tableName = $modelClassName::getTableName(); $beans = ZurmoRedBean::batch($tableName, $ids); return self::makeModels($beans, $modelClassName); }