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 testSetDontReInsertSameRecordsIfTheyExists()
 {
     $group1 = $this->_initGroup();
     $group2 = $this->_initGroup();
     $group3 = $this->_initGroup();
     $group4 = $this->_initGroup();
     $user = $this->_createUserAndSave(array($group1, $group2, $group3));
     $table = lmbDBAL::table('user_for_test2group_for_test', $this->conn);
     $records = $table->select()->getArray();
     $this->assertEqual(count($records), 3);
     $collection = new lmbARManyToManyCollection('groups', $user);
     $collection->set(array($group1, $group2, $group3, $group4));
     $new_records = $table->select()->getArray();
     $this->assertEqual(count($new_records), 4);
     $this->assertEqual($records[0]['id'], $new_records[0]['id']);
     $this->assertEqual($records[1]['id'], $new_records[1]['id']);
     $this->assertEqual($records[2]['id'], $new_records[2]['id']);
     $this->assertEqual($new_records[3]['user_id'], $user->getId());
 }