private function getUsedEveCharacters($apiIds)
 {
     $chars = ECP\EveCharacterQuery::create()->filterByEveApiId($apiIds)->find();
     $charDict = array();
     foreach ($chars as $char) {
         $key = $char->getEveApiId();
         if (!array_key_exists($key, $charDict)) {
             $charDict[$key] = array($char);
         } else {
             $charDict[$key][] = $char;
         }
     }
     return $charDict;
 }
 /**
  * Returns the number of related EveCharacter objects.
  *
  * @param      Criteria $criteria
  * @param      boolean $distinct
  * @param      ConnectionInterface $con
  * @return int             Count of related EveCharacter objects.
  * @throws PropelException
  */
 public function countEveCharacters(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null)
 {
     $partial = $this->collEveCharactersPartial && !$this->isNew();
     if (null === $this->collEveCharacters || null !== $criteria || $partial) {
         if ($this->isNew() && null === $this->collEveCharacters) {
             return 0;
         }
         if ($partial && !$criteria) {
             return count($this->getEveCharacters());
         }
         $query = ChildEveCharacterQuery::create(null, $criteria);
         if ($distinct) {
             $query->distinct();
         }
         return $query->filterByEveApi($this)->count($con);
     }
     return count($this->collEveCharacters);
 }
 /**
  * Returns a new ChildEveCharacterQuery object.
  *
  * @param     string $modelAlias The alias of a model in the query
  * @param     Criteria $criteria Optional Criteria to build the query from
  *
  * @return ChildEveCharacterQuery
  */
 public static function create($modelAlias = null, Criteria $criteria = null)
 {
     if ($criteria instanceof ChildEveCharacterQuery) {
         return $criteria;
     }
     $query = new ChildEveCharacterQuery();
     if (null !== $modelAlias) {
         $query->setModelAlias($modelAlias);
     }
     if ($criteria instanceof Criteria) {
         $query->mergeWith($criteria);
     }
     return $query;
 }
 /**
  * Builds a Criteria object containing the primary key for this object.
  *
  * Unlike buildCriteria() this method includes the primary key values regardless
  * of whether or not they have been modified.
  *
  * @throws LogicException if no primary key is defined
  *
  * @return Criteria The Criteria object containing value(s) for primary key(s).
  */
 public function buildPkeyCriteria()
 {
     $criteria = ChildEveCharacterQuery::create();
     $criteria->add(EveCharacterTableMap::COL_ID, $this->id);
     return $criteria;
 }
 /**
  * Performs an INSERT on the database, given a EveCharacter or Criteria object.
  *
  * @param mixed               $criteria Criteria or EveCharacter object containing data that is used to create the INSERT statement.
  * @param ConnectionInterface $con the ConnectionInterface connection to use
  * @return mixed           The new primary key.
  * @throws PropelException Any exceptions caught during processing will be
  *                         rethrown wrapped into a PropelException.
  */
 public static function doInsert($criteria, ConnectionInterface $con = null)
 {
     if (null === $con) {
         $con = Propel::getServiceContainer()->getWriteConnection(EveCharacterTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from EveCharacter object
     }
     if ($criteria->containsKey(EveCharacterTableMap::COL_ID) && $criteria->keyContainsValue(EveCharacterTableMap::COL_ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . EveCharacterTableMap::COL_ID . ')');
     }
     // Set the correct dbName
     $query = EveCharacterQuery::create()->mergeWith($criteria);
     // use transaction because $criteria could contain info
     // for more than one table (I guess, conceivably)
     return $con->transaction(function () use($con, $query) {
         return $query->doInsert($con);
     });
 }
 public function processGroups()
 {
     while (true) {
         $groups = ECP\GroupQuery::create()->where('Group.lastComputed < ?', time() - 24 * 60 * 60)->find();
         $groups->populateRelation('GroupEvePerson');
         $groups->populateRelation('GroupPerson');
         $evePersonNames = array();
         foreach ($groups as $group) {
             foreach ($group->getGroupEvepeople() as $groupEvePerson) {
                 $evePersonNames[] = $groupEvePerson->getName();
             }
         }
         if (count($evePersonNames) != 0) {
             $eveCharacters = ECP\EveCharacterQuery::create()->filterByCharName($evePersonNames)->_or()->filterByCorpName($evePersonNames)->_or()->filterByAllyName($evePersonNames)->find();
             $eveCharacters->populateRelation('EveApi');
             $eveNameDict = array();
             foreach ($eveCharacters as $eveCharacter) {
                 $eveApi = $eveCharacter->getEveApi();
                 $userId = $eveApi->getUserId();
                 $eveNameDict[$eveCharacter->getCharName()] = array($userId);
                 if ($eveCharacter->getCorpId() != 0) {
                     $this->pushEveName($eveNameDict, $eveCharacter->getCorpName(), $userId);
                 }
                 if ($eveCharacter->getAllyId() != 0) {
                     $this->pushEveName($eveNameDict, $eveCharacter->getAllyName(), $userId);
                 }
             }
             foreach ($eveNameDict as $key => $userIds) {
                 $eveNameDict[$key] = array_unique($userIds);
             }
             foreach ($groups as $group) {
                 $personDict = array();
                 foreach ($group->getGrouppeople() as $groupPerson) {
                     $personKey = $groupPerson->getGroupPersonTypeId() . '-' . $groupPerson->getGroupEvePersonId();
                     if (!array_key_exists($personKey, $personDict)) {
                         $personDict[$personKey] = array($groupPerson);
                     } else {
                         $personDict[$personKey][] = $groupPerson;
                     }
                 }
                 $group->setLastComputed(time());
                 $connection = $this->getPropelConnection();
                 try {
                     $connection->beginTransaction();
                     foreach ($group->getGroupEvepeople() as $groupEvePerson) {
                         $name = $groupEvePerson->getName();
                         $userIds = array_key_exists($name, $eveNameDict) ? $eveNameDict[$name] : array();
                         $personKey = $groupEvePerson->getGroupPersonTypeId() . '-' . $groupEvePerson->getId();
                         $persons = array_key_exists($personKey, $personDict) ? $personDict[$personKey] : array();
                         $personByUserId = array();
                         foreach ($persons as $person) {
                             $personById[$person->getUserId()];
                         }
                         foreach ($userIds as $userId) {
                             if (!array_key_exists($userId, $personByUserId)) {
                                 $groupPersonEntity = new ECP\GroupPerson();
                                 $groupPersonEntity->setGroup($group);
                                 $groupPersonEntity->setGroupPersonTypeId($groupEvePerson->getGroupPersonTypeId());
                                 $groupPersonEntity->setGroupEvePersonId($groupEvePerson->getId());
                                 $groupPersonEntity->setUserId($userId);
                                 $this->prepareSubentitySave2($connection, $group, 'GroupPerson', $groupPersonEntity, true);
                             }
                         }
                         foreach ($persons as $groupPersonEntity) {
                             if (!in_array($groupPersonEntity->getUserId(), $userIds)) {
                                 $group->removeGroupPerson($groupPersonEntity);
                             }
                         }
                     }
                     $group->save($connection);
                     $connection->commit();
                 } catch (Exception $e) {
                     $connection->rollBack();
                     throw $e;
                 }
             }
         }
         sleep(30);
     }
 }