Ejemplo n.º 1
0
 private function whenIFindOrCreateByPrimaryKey()
 {
     $this->resultRecord = $this->table->findOrCreateRecord($this->storedRecord->getIdentifierFieldIndexed());
 }
Ejemplo n.º 2
0
 /**
  * @param Record $record
  * @param array  $uniqueIndexesToCheck
  * @return \Dive\Query\Query
  * @throws \Dive\Exception
  */
 private function getUniqueIndexesQuery(Record $record, array $uniqueIndexesToCheck)
 {
     $table = $record->getTable();
     $conn = $table->getConnection();
     $conditions = array();
     $queryParams = array();
     $recordExists = $record->exists();
     $identifier = array();
     if ($recordExists) {
         $condition = '';
         foreach ($record->getIdentifierFieldIndexed() as $idField => $idValue) {
             $condition .= $conn->quoteIdentifier($idField) . ' != ? AND ';
             $identifier[] = $idValue;
         }
         // strip last AND from string
         $condition = substr($condition, 0, -4);
         $conditions['primary'] = $condition;
     }
     foreach ($uniqueIndexesToCheck as $uniqueName => $uniqueIndexToCheck) {
         $isNullConstrained = $table->isUniqueIndexNullConstrained($uniqueName);
         $conditionParams = array();
         $condition = '';
         $fieldNames = $uniqueIndexToCheck['fields'];
         foreach ($fieldNames as $fieldName) {
             $fieldNameQuoted = $conn->quoteIdentifier($fieldName);
             $fieldValue = $record->get($fieldName);
             if ($fieldValue !== null) {
                 $condition .= $fieldNameQuoted . ' = ? AND ';
                 $conditionParams[] = $fieldValue;
             } else {
                 if ($isNullConstrained) {
                     $condition .= $fieldNameQuoted . ' IS NULL AND ';
                 } else {
                     throw new Exception("Cannot process unique index for creating query to check whether the record is unique, or not!");
                 }
             }
         }
         // strip last AND from string
         $condition = substr($condition, 0, -4);
         $conditions[$uniqueName] = $condition;
         $queryParams = array_merge($queryParams, $conditionParams);
     }
     $whereCondition = ($recordExists ? array_shift($conditions) . ' AND (' : '') . implode(' OR ', $conditions) . ($recordExists ? ')' : '');
     $query = $table->createQuery();
     $query->where($whereCondition);
     foreach ($conditions as $uniqueName => $condition) {
         $query->addSelect("({$condition}) AS " . $conn->quoteIdentifier($uniqueName));
     }
     $whereParams = $recordExists ? array_merge($identifier, $queryParams) : $queryParams;
     $query->setParams(Query::PART_SELECT, $queryParams);
     $query->setParams(Query::PART_WHERE, $whereParams);
     $query->limit(1);
     return $query;
 }