/**
  * gets the previous change if the action is 'modified'
  *
  * @return ConceptPropertyHistory object
  * @param  string $historyTimestamp
  * @param  string $propertyId
  */
 function getPrevious()
 {
     $propertyId = $this->getSchemaPropertyElementId();
     $timestamp = $this->getCreatedAt();
     //build the query string
     $c = new Criteria();
     $crit0 = $c->getNewCriterion(SchemaPropertyElementHistoryPeer::SCHEMA_PROPERTY_ELEMENT_ID, $propertyId);
     $crit1 = $c->getNewCriterion(SchemaPropertyElementHistoryPeer::CREATED_AT, $timestamp, Criteria::LESS_THAN);
     // Perform AND at level 0 ($crit0 $crit1 )
     $crit0->addAnd($crit1);
     $c->add($crit0);
     //set order and limits
     $c->setLimit(1);
     $c->addDescendingOrderByColumn(SchemaPropertyElementHistoryPeer::CREATED_AT);
     $result = SchemaPropertyElementHistoryPeer::doSelect($c);
     //return the resulting object
     if (count($result)) {
         $result = $result[0];
     }
     return $result;
 }
 /**
  * Retrieve multiple objects by pkey.
  *
  * @param      array $pks List of primary keys
  * @param      Connection $con the connection to use
  * @throws     PropelException Any exceptions caught during processing will be
  *		 rethrown wrapped into a PropelException.
  */
 public static function retrieveByPKs($pks, $con = null)
 {
     if ($con === null) {
         $con = Propel::getConnection(self::DATABASE_NAME);
     }
     $objs = null;
     if (empty($pks)) {
         $objs = array();
     } else {
         $criteria = new Criteria();
         $criteria->add(SchemaPropertyElementHistoryPeer::ID, $pks, Criteria::IN);
         $objs = SchemaPropertyElementHistoryPeer::doSelect($criteria, $con);
     }
     return $objs;
 }
    /**
     * @param $args
     *
     * @throws \PropelException
     */
    public function perform($args)
    {
        list($environment, $importId) = $args;

        if ( ! defined('SF_ENVIRONMENT')) {
            //todo: this part really should be in a _bootstrapDbJob include
            // Set up environment for this job
            define('SF_ROOT_DIR', realpath(dirname(__file__) . '/../../../..'));
            define('SF_APP', 'frontend');
            define('SF_ENVIRONMENT', $environment);
            define('SF_DEBUG', false);

//initialize composer
            require_once(SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
// initialize symfony
            require_once(SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR
                         . 'config' . DIRECTORY_SEPARATOR . 'config.php');
// initialize database manager
            $databaseManager = new \sfDatabaseManager();
            $databaseManager->initialize();
        }

        $connection = \Propel::getConnection();

        //todo: this should be setup to run on a cron

        //update the reciprocals and inverse
        if ($importId) {
            $c = new \Criteria();
            $c->add(\SchemaPropertyElementHistoryPeer::IMPORT_ID, $importId);
            $historyList = \SchemaPropertyElementHistoryPeer::doSelect($c);
            /** @var \SchemaPropertyElementHistory $history */
            foreach ($historyList as $history) {
                $element = $history->getSchemaPropertyElement();
                if ($element) {
                    //don't generate reciprocals for generated elements
                    if (!$element->getIsGenerated()) {
                        $element->importId = $importId;
                        $element->updateReciprocal($history->getAction(), $history->getCreatedUserId(),
                              $history->getSchemaId());
                    }
                }
            }
        }

        //update lexical aliases

        $query = <<<SQL
update reg_schema_property_element
set related_schema_property_id = schema_property_id
WHERE profile_property_id = 27
and related_schema_property_id is NULL
SQL;
        $statement = $connection->prepareStatement($query);
        $affectedRows = $statement->executeUpdate();
        echo $affectedRows;

        //update all of the related_schema_property_id

        $query = <<<SQL
update reg_schema_property_element as e, reg_schema_property as p
set e.related_schema_property_id = p.id
WHERE e.object = p.uri
and (e.related_schema_property_id <> p.id or e.related_schema_property_id is NULL)
SQL;
        $statement = $connection->prepareStatement($query);
        $affectedRows = $statement->executeUpdate();
        echo $affectedRows;

        //update all of the property parent_id

        $query = <<<SQL
UPDATE reg_schema_property as propa, reg_schema_property as propb
set propa.is_subproperty_of = propb.id
where propa.parent_uri is not NULL
and propa.is_subproperty_of is NULL
and propa.parent_uri = propb.uri;
SQL;
        $statement = $connection->prepareStatement($query);
        $affectedRows = $statement->executeUpdate();
        echo $affectedRows;
    }
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this SchemaProperty has previously
  * been saved, it will retrieve related SchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId from storage.
  * If this SchemaProperty is new, it will return
  * an empty collection or the current collection, the criteria
  * is ignored on a new object.
  *
  * @param      Connection $con
  * @param      Criteria $criteria
  * @throws     PropelException
  */
 public function getSchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId($criteria = null, $con = null)
 {
     // include the Peer class
     include_once 'lib/model/om/BaseSchemaPropertyElementHistoryPeer.php';
     if ($criteria === null) {
         $criteria = new Criteria();
     } elseif ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
     }
     if ($this->collSchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId === null) {
         if ($this->isNew()) {
             $this->collSchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId = array();
         } else {
             $criteria->add(SchemaPropertyElementHistoryPeer::RELATED_SCHEMA_PROPERTY_ID, $this->getId());
             SchemaPropertyElementHistoryPeer::addSelectColumns($criteria);
             $this->collSchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId = SchemaPropertyElementHistoryPeer::doSelect($criteria, $con);
         }
     } else {
         // criteria has no effect for a new object
         if (!$this->isNew()) {
             // the following code is to determine if a new query is
             // called for.  If the criteria is the same as the last
             // one, just return the collection.
             $criteria->add(SchemaPropertyElementHistoryPeer::RELATED_SCHEMA_PROPERTY_ID, $this->getId());
             SchemaPropertyElementHistoryPeer::addSelectColumns($criteria);
             if (!isset($this->lastSchemaPropertyElementHistoryRelatedByRelatedSchemaPropertyIdCriteria) || !$this->lastSchemaPropertyElementHistoryRelatedByRelatedSchemaPropertyIdCriteria->equals($criteria)) {
                 $this->collSchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId = SchemaPropertyElementHistoryPeer::doSelect($criteria, $con);
             }
         }
     }
     $this->lastSchemaPropertyElementHistoryRelatedByRelatedSchemaPropertyIdCriteria = $criteria;
     return $this->collSchemaPropertyElementHistorysRelatedByRelatedSchemaPropertyId;
 }