protected function _getExistingRecords($objects) { $table = new lmbTableGateway($this->relation_info['table'], $this->conn); $criteria = new lmbSQLCriteria(); $criteria->addAnd(new lmbSQLFieldCriteria($this->relation_info['field'], $this->owner->getId())); $criteria->addAnd(new lmbSQLFieldCriteria($this->relation_info['foreign_field'], null, lmbSQLFieldCriteria::IS_NOT_NULL)); $existing_records = $table->select($criteria); return lmbCollection::toFlatArray($existing_records, $this->relation_info['foreign_field']); }
protected function _getBlocks() { $blocks = lmbCollection::toFlatArray(lmbActiveRecord::find('lmbCmsTextBlock'), 'identifier'); $result = array(); foreach ($this->toolkit->getConf('text_blocks') as $identifier => $default_properties) { if (isset($blocks[$identifier])) { $item = $blocks[$identifier]; $item['exists'] = true; } else { $item = new lmbCmsTextBlock(); $item->import($default_properties); $item->setIdentifier($identifier); $item['exists'] = false; } $result[$identifier] = $item; } return $result; }
function rewind() { foreach ($this->attach_relations as $relation_name => $params) { if (!in_array($relation_name, array_keys($this->loaded_attaches))) { $relation_type = $this->base_object->getRelationType($relation_name); $relation_info = $this->base_object->getRelationInfo($relation_name); $relation_class = $relation_info['class']; $relation_object = new $relation_class(null, $this->conn); switch ($relation_type) { case lmbActiveRecord::HAS_ONE: case lmbActiveRecord::MANY_BELONGS_TO: $ids = lmbArrayHelper::getColumnValues($this->prefix . $relation_info['field'], $this->iterator); if (!count($ids)) { $this->loaded_attaches[$relation_name] = array(); } else { $attached_objects = lmbActiveRecord::findByIds($relation_class, $ids, $params, $this->conn); $this->loaded_attaches[$relation_name] = lmbCollection::toFlatArray($attached_objects, $key_field = $relation_object->getPrimaryKeyName(), $export_each = false); } break; case lmbActiveRecord::BELONGS_TO: $ids = lmbArrayHelper::getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator); if (!count($ids)) { $this->loaded_attaches[$relation_name] = array(); } else { $criteria = lmbSQLCriteria::in($relation_info['field'], $ids); $params['criteria'] = isset($params['criteria']) ? $params['criteria']->addAnd($criteria) : $criteria; $attached_objects = lmbActiveRecord::find($relation_class, $params, $this->conn); $this->loaded_attaches[$relation_name] = lmbCollection::toFlatArray($attached_objects, $key_field = $relation_info['field'], $export_each = false); } break; case lmbActiveRecord::HAS_MANY: if (!isset($params['sort'])) { $params['sort'] = $relation_object->getDefaultSortParams(); } $params['sort'] = array($relation_info['field'] => 'ASC') + $params['sort']; $query = lmbAROneToManyCollection::createFullARQueryForRelation($relation_info, $this->conn, $params); $ids = lmbArrayHelper::getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator); $this->loaded_attaches[$relation_name] = array(); if (!count($ids)) { break; } $query->addCriteria(lmbSQLCriteria::in($relation_info['field'], $ids)); $attached_objects = $query->fetch(); foreach ($attached_objects as $attached_object) { $this->loaded_attaches[$relation_name][$attached_object->get($relation_info['field'])][] = $attached_object; } break; case lmbActiveRecord::HAS_MANY_TO_MANY: if (!isset($params['sort'])) { $params['sort'] = $relation_object->getDefaultSortParams(); } $params['sort'] = array($relation_info['field'] => 'ASC') + $params['sort']; $query = lmbARManyToManyCollection::createFullARQueryForRelation($relation_info, $this->conn, $params); $query->addField($relation_info['table'] . '.' . $relation_info['field'], "link__id"); $ids = lmbArrayHelper::getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator); $this->loaded_attaches[$relation_name] = array(); if (!count($ids)) { break; } $query->addCriteria(lmbSQLCriteria::in($relation_info['field'], $ids)); $attached_objects = $query->fetch(); foreach ($attached_objects as $attached_object) { $this->loaded_attaches[$relation_name][$attached_object->get("link__id")][] = $attached_object; } break; } } } parent::rewind(); }
function testToFlatArrayWithKeyField() { $data = array(array('x' => 'C'), array('x' => 'A'), array('x' => 'B')); $iterator = new lmbCollection($data); $arr = lmbCollection::toFlatArray($iterator, 'x'); $this->assertTrue(isset($arr['A'])); $this->assertEqual($arr['A'], array('x' => 'A')); $this->assertTrue(isset($arr['B'])); $this->assertEqual($arr['B'], array('x' => 'B')); $this->assertTrue(isset($arr['C'])); $this->assertEqual($arr['C'], array('x' => 'C')); }