public function SyncWithDimension($sender, $param)
 {
     $cleanmodul = '';
     $SQL = "SELECT * FROM " . $this->Tedsend_tabelle->Text . " WHERE parent_id" . $this->Tedsend_tabelle->Text . " = " . $this->Tedsend_id->Text;
     $cleanmodul = preg_replace("/(^t[a-z]\\_)/", "", $this->Tedsend_tabelle->Text);
     preg_match("/(_[a-z])/", $cleanmodul, $matches);
     if (count($matches) >= 1) {
         $cleanmodul = preg_replace("/(_[a-z])/", ucfirst(substr($matches[1], 1, 1)), $cleanmodul);
     }
     $finderclass = ucfirst($cleanmodul) . "Record";
     $AllRecordsToWrite = TActiveRecord::finder($finderclass)->findAllBySql($SQL);
     $fieldToTransfer = $this->Tedsend_field->Text;
     if (count($AllRecordsToWrite) > 0) {
         foreach ($AllRecordsToWrite as $SingleRecord) {
             $criteria = new TActiveRecordCriteria();
             $criteria->Condition = 'idta_stammdaten_group = :stammdaten_group AND stammdaten_key_extern = :key_extern';
             $criteria->Parameters[':stammdaten_group'] = $this->Tedidta_stammdaten_group->Text;
             $criteria->Parameters[':key_extern'] = $this->Tedsend_tabelle->Text . $SingleRecord->{'id' . $this->Tedsend_tabelle->Text};
             $RecordToChange = StammdatenRecord::finder()->find($criteria);
             if (count($RecordToChange) == 0) {
                 $RecordToChange = new StammdatenRecord();
             }
             $RecordToChange->idta_stammdaten_group = $this->Tedidta_stammdaten_group->Text;
             $RecordToChange->stammdaten_key_extern = $this->Tedsend_tabelle->Text . $SingleRecord->{'id' . $this->Tedsend_tabelle->Text};
             $RecordToChange->stammdaten_name = $SingleRecord->{$fieldToTransfer};
             $RecordToChange->save();
             unset($RecordToChange);
         }
     }
 }
 public function suggestUser($sender, $param)
 {
     // Get the token
     $token = $param->getToken();
     // Sender is the Suggestions repeater
     $mySQL = "SELECT idtm_user,user_name FROM tm_user WHERE user_name LIKE '%" . $token . "%'";
     $sender->DataSource = PFH::convertdbObjectSuggest(TActiveRecord::finder('UserRecord')->findAllBySQL($mySQL), array('idtm_user', 'user_name'));
     $sender->dataBind();
 }
 public function suggestOrganisation($sender, $param)
 {
     // Get the token
     $token = $param->getToken();
     // Sender is the Suggestions repeater
     $mySQL = "SELECT idtm_organisation,org_name,org_vorname FROM tm_organisation WHERE org_name LIKE '%" . $token . "%'";
     $sender->DataSource = PFH::convertdbObjectSuggest(TActiveRecord::finder('OrganisationRecord')->findAllBySQL($mySQL), array('idtm_organisation', 'org_name', 'org_vorname'));
     $sender->dataBind();
 }
Exemple #4
0
    function test_find_by_sql_arb()
    {
        $sql = 'SELECT c.name as category, i.name as item
			FROM items i, categories c
			WHERE i.category_id = c.category_id LIMIT 2';
        $items = TActiveRecord::finder('SqlTest')->findBySql($sql);
        $sql = "SELECT users.*, 'hello' as another_value FROM users LIMIT 2";
        $users = TActiveRecord::finder('UserRecord2')->findBySql($sql);
        var_dump($users);
    }
 function test_finder_throw_exception_when_save()
 {
     $obj = TActiveRecord::finder('BaseRecordTest');
     try {
         $obj->save();
         $this->fail();
     } catch (TActiveRecordException $e) {
         $this->pass();
     }
 }
 /**
  * Apply result mapping.
  * @param array a result set row retrieved from the database
  * @param object the result object, will create if necessary.
  * @return object the result filled with data, null if not filled.
  */
 protected function applyResultMap($row, &$resultObject = null)
 {
     if ($row === false) {
         return null;
     }
     $resultMapName = $this->_statement->getResultMap();
     $resultClass = $this->_statement->getResultClass();
     $obj = null;
     if ($this->getManager()->getResultMaps()->contains($resultMapName)) {
         $obj = $this->fillResultMap($resultMapName, $row, null, $resultObject);
     } else {
         if (strlen($resultClass) > 0) {
             $obj = $this->fillResultClass($resultClass, $row, $resultObject);
         } else {
             $obj = $this->fillDefaultResultMap(null, $row, $resultObject);
         }
     }
     if (class_exists('TActiveRecord', false) && $obj instanceof TActiveRecord) {
         //Create a new clean active record.
         $obj = TActiveRecord::createRecord(get_class($obj), $obj);
     }
     return $obj;
 }
 /**
  * @param string active record class name.
  * @param array row data
  * @param array foreign key column names
  * @return TActiveRecord
  */
 protected function createFkObject($type, $row, $foreignKeys)
 {
     $obj = TActiveRecord::createRecord($type, $row);
     if (count($this->_association_columns) > 0) {
         $i = 0;
         foreach ($foreignKeys as $ref => $fk) {
             $obj->setColumnValue($ref, $row[$this->_association_columns[$i++]]);
         }
     }
     return $obj;
 }
 /**
  * @param TActiveRecord $record
  * @return TDataGatewayCommand
  */
 public function getCommand(TActiveRecord $record)
 {
     $conn = $record->getDbConnection();
     $connStr = $conn->getConnectionString();
     $tableInfo = $this->getRecordTableInfo($record);
     if (!isset($this->_commandBuilders[$connStr])) {
         $builder = $tableInfo->createCommandBuilder($record->getDbConnection());
         Prado::using('System.Data.DataGateway.TDataGatewayCommand');
         $command = new TDataGatewayCommand($builder);
         $command->OnCreateCommand[] = array($this, 'onCreateCommand');
         $command->OnExecuteCommand[] = array($this, 'onExecuteCommand');
         $this->_commandBuilders[$connStr] = $command;
     }
     $this->_commandBuilders[$connStr]->getBuilder()->setTableInfo($tableInfo);
     $this->_currentRecord = $record;
     return $this->_commandBuilders[$connStr];
 }
Exemple #9
0
 /**
  * @return TActiveRecord Active Record finder instance
  */
 protected function getRecordFinder()
 {
     return TActiveRecord::finder($this->getRecordClass());
 }
 /**
  * Returns foreign keys in $fromRecord with source column names as key
  * and foreign column names in the corresponding $matchesRecord as value.
  * The method returns the first matching foreign key between these 2 records.
  * @param TActiveRecord $fromRecord
  * @param TActiveRecord $matchesRecord
  * @return array foreign keys with source column names as key and foreign column names as value.
  */
 protected function findForeignKeys($from, $matchesRecord, $loose = false)
 {
     $gateway = $matchesRecord->getRecordGateway();
     $recordTableInfo = $gateway->getRecordTableInfo($matchesRecord);
     $matchingTableName = strtolower($recordTableInfo->getTableName());
     $matchingFullTableName = strtolower($recordTableInfo->getTableFullName());
     $tableInfo = $from;
     if ($from instanceof TActiveRecord) {
         $tableInfo = $gateway->getRecordTableInfo($from);
     }
     //find first non-empty FK
     foreach ($tableInfo->getForeignKeys() as $fkeys) {
         $fkTable = strtolower($fkeys['table']);
         if ($fkTable === $matchingTableName || $fkTable === $matchingFullTableName) {
             $hasFkField = !$loose && $this->getContext()->hasFkField();
             $key = $hasFkField ? $this->getFkFields($fkeys['keys']) : $fkeys['keys'];
             if (!empty($key)) {
                 return $key;
             }
         }
     }
     //none found
     $matching = $gateway->getRecordTableInfo($matchesRecord)->getTableFullName();
     throw new TActiveRecordException('ar_relations_missing_fk', $tableInfo->getTableFullName(), $matching);
 }
 function test_finder_returns_same_instance()
 {
     $obj1 = TActiveRecord::finder('BaseRecordTest');
     $obj2 = TActiveRecord::finder('BaseRecordTest');
     $this->assertSame($obj1, $obj2);
 }
Exemple #12
0
 /**
  * Returns the static model of the specified AR class.
  * @param string $className active record class name.
  * @return MakeOrgan the static model class
  */
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }
Exemple #13
0
 public function getStartNode($idtm_user, $modul, $planungssicht = 0)
 {
     //hier muss noch eine pruefung hin, wenn es mehrere treffer gibt...
     if (count(BerechtigungRecord::finder()->find('idtm_user = ? AND xx_modul = ?', $idtm_user, $modul)) > 0) {
         return BerechtigungRecord::finder()->find('idtm_user = ? AND xx_modul = ?', $idtm_user, $modul)->xx_id;
     } else {
         $SQL = "SELECT id" . $modul . " FROM " . $modul . " WHERE parent_id" . $modul . " = 0";
         if ($planungssicht > 0) {
             $SQL .= " AND idta_stammdatensicht = " . $planungssicht;
             //gilt nur für die planung
         }
         $SQL .= " LIMIT 1";
         $cleanmodul = preg_replace("/(^t[a-z]\\_)/", "", $modul);
         preg_match("/(_[a-z])/", $cleanmodul, $matches);
         if (count($matches) >= 1) {
             $cleanmodul = preg_replace("/(_[a-z])/", ucfirst(substr($matches[1], 1, 1)), $cleanmodul);
         }
         $finderclass = ucfirst($cleanmodul) . "Record";
         $Result = TActiveRecord::finder($finderclass)->findBySql($SQL);
         if (count($Result) >= 1) {
             $tmp = "id" . $modul;
             return $Result->{$tmp};
         } else {
             return 1;
         }
     }
 }
 /**
  * Returns the database connection used by active record.
  * By default, the "db" application component is used as the database connection.
  * You may override this method if you want to use a different database connection.
  * @return CDbConnection the database connection used by active record.
  * 
  * @TODO Get default db connection if theres none set
  * 
  */
 public function getDbConnection()
 {
     if (self::$db !== null) {
         return self::$db;
     } else {
         self::$db = Prado::getApplication()->getModule($this->getConnectionId())->getDbConnection();
         if (self::$db instanceof TDbConnection) {
             self::$db->setActive(true);
             return self::$db;
         } else {
             throw new TDbException('ActiveRecord requires a db connection.');
         }
     }
 }
 /**
  * @return TActiveRecord corresponding relationship foreign object finder instance.
  */
 public function getForeignRecordFinder()
 {
     return TActiveRecord::finder($this->getForeignRecordClass());
 }
 public static function finder($className = __CLASS__)
 {
     return parent::finder($className);
 }
 private function queryManyMany($joinTableName, $keys)
 {
     $relation = $this->relation;
     $model = TActiveRecord::model($relation->className);
     $table = $model->getTableSchema();
     $builder = $model->getCommandBuilder();
     $schema = $builder->getSchema();
     $pkTable = $this->_parent->model->getTableSchema();
     if (($joinTable = $builder->getSchema()->getTable($joinTableName)) === null) {
         throw new TDbException('The relation "' . $relation->name . '" in active record class "' . get_class($this->_parent->model) . '" is not specified correctly. The join table "' . $joinTableName . '" given in the foreign key cannot be found in the database.');
     }
     $fks = preg_split('/[\\s,]+/', $keys, -1, PREG_SPLIT_NO_EMPTY);
     if (count($fks) !== count($table->primaryKey) + count($pkTable->primaryKey)) {
         throw new TDbException('The relation "' . $relation->name . '" in active record class "' . get_class($this->_parent->model) . '" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.');
     }
     $joinCondition = array();
     $map = array();
     foreach ($fks as $i => $fk) {
         if (!isset($joinTable->columns[$fk])) {
             throw new TDbException('The relation "' . $relation->name . '" in active record class "' . get_class($this->_parent->model) . '" is specified with an invalid foreign key "' . $fk . '". There is no such column in the table "' . $joinTable->name . '".');
         }
         if (isset($joinTable->foreignKeys[$fk])) {
             list($tableName, $pk) = $joinTable->foreignKeys[$fk];
             if (!isset($joinCondition[$pk]) && $schema->compareTableNames($table->rawName, $tableName)) {
                 $joinCondition[$pk] = $table->rawName . '.' . $schema->quoteColumnName($pk) . '=' . $joinTable->rawName . '.' . $schema->quoteColumnName($fk);
             } else {
                 if (!isset($map[$pk]) && $schema->compareTableNames($pkTable->rawName, $tableName)) {
                     $map[$pk] = $fk;
                 } else {
                     throw new TDbException('The relation "' . $relation->name . '" in active record class "' . get_class($this->_parent->model) . '" is specified with an invalid foreign key "' . $fk . '". The foreign key does not point to either joining table.');
                 }
             }
         } else {
             if ($i < count($pkTable->primaryKey)) {
                 $pk = is_array($pkTable->primaryKey) ? $pkTable->primaryKey[$i] : $pkTable->primaryKey;
                 $map[$pk] = $fk;
             } else {
                 $j = $i - count($pkTable->primaryKey);
                 $pk = is_array($table->primaryKey) ? $table->primaryKey[$j] : $table->primaryKey;
                 $joinCondition[$pk] = $table->rawName . '.' . $schema->quoteColumnName($pk) . '=' . $joinTable->rawName . '.' . $schema->quoteColumnName($fk);
             }
         }
     }
     if ($joinCondition === array() || $map === array()) {
         throw new TDbException('The relation "' . $relation->name . '" in active record class "' . get_class($this->_parent->model) . '" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.');
     }
     $records = $this->_parent->records;
     $cols = array();
     foreach (is_string($pkTable->primaryKey) ? array($pkTable->primaryKey) : $pkTable->primaryKey as $n => $pk) {
         $name = $joinTable->rawName . '.' . $schema->quoteColumnName($map[$pk]);
         $cols[$name] = $name . ' AS ' . $schema->quoteColumnName('c' . $n);
     }
     $keys = array_keys($records);
     if (is_array($pkTable->primaryKey)) {
         foreach ($keys as &$key) {
             $key2 = unserialize($key);
             $key = array();
             foreach ($pkTable->primaryKey as $pk) {
                 $key[$map[$pk]] = $key2[$pk];
             }
         }
     }
     $where = empty($relation->condition) ? '' : ' WHERE (' . $relation->condition . ')';
     $group = empty($relation->group) ? '' : ', ' . $relation->group;
     $having = empty($relation->having) ? '' : ' AND (' . $relation->having . ')';
     $order = empty($relation->order) ? '' : ' ORDER BY ' . $relation->order;
     $sql = 'SELECT ' . $this->relation->select . ' AS ' . $schema->quoteColumnName('s') . ', ' . implode(', ', $cols) . ' FROM ' . $table->rawName . ' INNER JOIN ' . $joinTable->rawName . ' ON (' . implode(') AND (', $joinCondition) . ')' . $where . ' GROUP BY ' . implode(', ', array_keys($cols)) . $group . ' HAVING (' . $builder->createInCondition($joinTable, $map, $keys) . ')' . $having . $order;
     $command = $builder->getDbConnection()->createCommand($sql);
     if (is_array($relation->params)) {
         $builder->bindValues($command, $relation->params);
     }
     $stats = array();
     foreach ($command->queryAll() as $row) {
         if (is_array($pkTable->primaryKey)) {
             $key = array();
             foreach ($pkTable->primaryKey as $n => $k) {
                 $key[$k] = $row['c' . $n];
             }
             $stats[serialize($key)] = $row['s'];
         } else {
             $stats[$row['c0']] = $row['s'];
         }
     }
     foreach ($records as $pk => $record) {
         $record->addRelatedRecord($relation->name, isset($stats[$pk]) ? $stats[$pk] : $this->relation->defaultValue, false);
     }
 }