gpt4 book ai didi

php - MySql 数据透视表到 Yii CActiveDataProvider

转载 作者:可可西里 更新时间:2023-11-01 06:33:53 24 4
gpt4 key购买 nike

我有一个表结构如下:

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;

请同时检查 SQL fiddle。 http://sqlfiddle.com/#!9/1a579/3

数据是用这个查询加载的。

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;

我如何将其形成为 CDbCriteria 对象和 CActiveDataProvider ?我需要将这些数据加载到 cgridview 中,并允许使用 cgridview 过滤器 搜索自定义列。

目前这是我所做的:

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;
//}
}

print_r($customField) 的输出:

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] =>
)

最佳答案

为什么不创建一个 SQL View ,然后从中创建一个新模型?

SQL不包含动态值,应该写得更紧凑。

关于php - MySql 数据透视表到 Yii CActiveDataProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33567322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com