2015-11-06 9 views
8

Ich habe eine Tabellenstruktur, wie folgend:MySql Pivot-Tabelle in zu Yü CActiveDataProvider

CREATE TABLE IF NOT EXISTS `CustomValue` (
    `id` int(11) NOT NULL, 
    `customFieldId` int(11) NOT NULL, 
    `relatedId` int(11) NOT NULL, 
    `fieldValue` text COLLATE utf8_unicode_ci, 
    `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

CREATE TABLE IF NOT EXISTS `CustomField` (
    `id` int(11) NOT NULL, 
    `customTypeId` int(11) NOT NULL, 
    `fieldName` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `relatedTable` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `defaultValue` text COLLATE utf8_unicode_ci, 
    `sortOrder` int(11) NOT NULL DEFAULT '0', 
    `enabled` char(1) COLLATE utf8_unicode_ci DEFAULT '1', 
    `listItemTag` char(1) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `required` char(1) COLLATE utf8_unicode_ci DEFAULT '0', 
    `onCreate` char(1) COLLATE utf8_unicode_ci DEFAULT '1', 
    `onEdit` char(1) COLLATE utf8_unicode_ci DEFAULT '1', 
    `onView` char(1) COLLATE utf8_unicode_ci DEFAULT '1', 
    `listValues` text COLLATE utf8_unicode_ci, 
    `label` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `htmlOptions` text COLLATE utf8_unicode_ci 
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 


CREATE TABLE IF NOT EXISTS `User` (
    `id` bigint(20) NOT NULL, 
    `address1` text COLLATE utf8_unicode_ci, 
    `mobile` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `firstName` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `lastName` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL 
) ENGINE=MyISAM AUTO_INCREMENT=4034 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

Bitte geben Sie die SQL-Geige als auch überprüfen. http://sqlfiddle.com/#!9/1a579/3

Die Daten werden mit dieser Abfrage geladen.

SET @Colvalues = NULL; 
SET @sql = NULL; 

SELECT 
    GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(f.fieldName = ''', 
     f.fieldName, ''', COALESCE(v.fieldValue, f.defaultValue) , NULL)) AS ', '''', f.fieldName , '''') 
) INTO @Colvalues 
FROM customField AS f 
INNER JOIN Customvalue AS v ON f.Id = v.customFieldId; 


SET @sql = CONCAT('SELECT 
    u.*, v.relatedId, v.CreatedAt, ', @Colvalues , ' 
FROM customField AS f 
INNER JOIN Customvalue AS v ON f.Id = v.customFieldId RIGHT JOIN User u on u.id = v.relatedId 
GROUP BY v.relatedId, v.CreatedAt;'); 

PREPARE stmt 
FROM @sql; 

EXECUTE stmt; 

Wie kann ich bilden diese zu einem CDbCriteria Objekt und CActiveDataProvider? Ich muss diese Daten in eine cgridview laden und benutzerdefinierte Spalten mit cgridview filters suchen lassen.

derzeit ist es das, was ich getan habe:

public function searchPeople($customFields) 
    { 
     $criteria = new CDbCriteria; 
     $criteria->together = true; 

     $criteria->compare('address1', $this->address1, true); 
     $criteria->compare('mobile', $this->mobile, true); 
     $criteria->compare('t.firstName', $this->firstName, true); 
     $criteria->compare('t.lastName', $this->lastName, true); 


     if (!empty($customFields)) { 
      $criteria->join .= ' LEFT OUTER JOIN CustomValue cv ON cv.relatedId=t.id'; 
      //foreach ($customFields as $k => $customField) { 
      //print_r($customField); exit; 
      //} 
     } 

Ausgabe von print_r ($ custom):

CustomValue Object 
(
    [fieldStyle] => 
    [fieldName] => ALTERNATEEMAIL 
    [fieldLabel] => Alternate Email 
    [fieldType] => text 
    [fieldTag] => 
    [fieldIsRequired] => 1 
    [fieldDefaultValue] => 
    [listValues] => 
    [_new:CActiveRecord:private] => 1 
    [_attributes:CActiveRecord:private] => Array 
     (
      [customFieldId] => 14 
      [fieldValue] => 
     ) 
+0

Es könnte helfen zu erklären, was nicht funktioniert mit dem, was Sie bisher haben ... Oder genauer zu dem Problem, das Sie bei der Implementierung haben –

+0

@clickstefan das ist meine Abfrage. http://sqlfiddle.com/#!9/1a579/3 Ich muss die Datensätze basierend auf dynamischen Spalten filtern. Ich versuche zuerst, dieses Format zu einem CDbCriteria einzurichten. – dev1234

Antwort

2

Warum Sie nicht über eine SQL-Ansicht und dann ein neues Modell erstellen davon?

Die SQL enthält keinen dynamischen Wert, sie sollte in kompakterer Form geschrieben werden.

+0

Erstellen einer Sicht für dynamische Abfragen ist in MYSQL nicht möglich. – dev1234