使用Yii2将Android表情符号从Firebase保存到MySQL

时间:2016-11-21 10:09:33

标签: php mysql firebase yii yii2

我尝试将数据从Firebase导入MySQL数据库。

我的Firebase数据如下所示:

enter image description here

现在,我尝试获取此记录并保存到MySQL:

$model = new DbModel();
$wpath = "/messages/". $id;
$wvalue = $firebase->get($wpath);

$wjson = json_decode($wvalue, true);            
$model->message = $wjson["message"]; //this is ok, if I print it'll show the emoji
$model->save();

保存模型后,我在我的数据库中得到了这个:

I'm going to dance ????

我在表格中为我的列设置了utf8mb4_general_ci排序规则,当我通过脚本或用户界面手动插入相同的文字时,似乎工作,但是当我输入时,表情符号不会保存通过php保存我的模型。

我的服务器版本:10.1.16-MariaDB - mariadb.org binary distribution

网络服务器:Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9

任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

终于找到了解决方案here 在数据库连接设置中更改字符集也很重要:

DECLARE @SchemaName VARCHAR(100)
 DECLARE @TableName VARCHAR(256)
 DECLARE @IndexName VARCHAR(256)
 DECLARE @ColumnName VARCHAR(100)
 DECLARE @is_unique VARCHAR(100)
 DECLARE @IndexTypeDesc VARCHAR(100)
 DECLARE @FileGroupName VARCHAR(100)
 DECLARE @is_disabled VARCHAR(100)
 DECLARE @IndexOptions VARCHAR(MAX)
 DECLARE @IndexColumnId INT
 DECLARE @IsDescendingKey INT 
 DECLARE @IsIncludedColumn INT
 DECLARE @TSQLScripCreationIndex VARCHAR(MAX)
 DECLARE @TSQLScripDisableIndex VARCHAR(MAX)

 DECLARE CursorIndex CURSOR
 FOR
    SELECT  SCHEMA_NAME(t.schema_id) [schema_name] ,
            t.name ,
            ix.name ,
            CASE WHEN ix.is_unique = 1 THEN 'UNIQUE '
                 ELSE ''
            END ,
            ix.type_desc ,
            CASE WHEN ix.is_padded = 1 THEN 'PAD_INDEX = ON, '
                 ELSE 'PAD_INDEX = OFF, '
            END
            + CASE WHEN ix.allow_page_locks = 1 THEN 'ALLOW_PAGE_LOCKS = ON, '
                   ELSE 'ALLOW_PAGE_LOCKS = OFF, '
              END
            + CASE WHEN ix.allow_row_locks = 1 THEN 'ALLOW_ROW_LOCKS = ON, '
                   ELSE 'ALLOW_ROW_LOCKS = OFF, '
              END
            + CASE WHEN INDEXPROPERTY(t.object_id, ix.name, 'IsStatistics') = 1
                   THEN 'STATISTICS_NORECOMPUTE = ON, '
                   ELSE 'STATISTICS_NORECOMPUTE = OFF, '
              END
            + CASE WHEN ix.ignore_dup_key = 1 THEN 'IGNORE_DUP_KEY = ON, '
                   ELSE 'IGNORE_DUP_KEY = OFF, '
              END + 'SORT_IN_TEMPDB = OFF,DROP_EXISTING = ON' AS IndexOptions ,
            ix.is_disabled ,
            FILEGROUP_NAME(ix.data_space_id) FileGroupName
    FROM    sys.tables t
            INNER JOIN sys.indexes ix ON t.object_id = ix.object_id
    WHERE   ix.type > 0
            AND ix.is_primary_key = 0
            AND ix.is_unique_constraint = 0 --and schema_name(tb.schema_id)= @SchemaName and tb.name=@TableName
            AND t.is_ms_shipped = 0
            AND t.name <> 'sysdiagrams'
            AND t.name = 'LegalEntity'
    ORDER BY SCHEMA_NAME(t.schema_id) ,
            t.name ,
            ix.name

 OPEN CursorIndex
 FETCH NEXT FROM CursorIndex INTO @SchemaName, @TableName, @IndexName,
    @is_unique, @IndexTypeDesc, @IndexOptions, @is_disabled, @FileGroupName

 WHILE ( @@fetch_status = 0 )
    BEGIN
        DECLARE @IndexColumns VARCHAR(MAX)
        DECLARE @IncludedColumns VARCHAR(MAX)

        SET @IndexColumns = ''
        SET @IncludedColumns = ''

        DECLARE CursorIndexColumn CURSOR
        FOR
            SELECT  col.name ,
                    ixc.is_descending_key ,
                    ixc.is_included_column
            FROM    sys.tables tb
                    INNER JOIN sys.indexes ix ON tb.object_id = ix.object_id
                    INNER JOIN sys.index_columns ixc ON ix.object_id = ixc.object_id
                                                        AND ix.index_id = ixc.index_id
                    INNER JOIN sys.columns col ON ixc.object_id = col.object_id
                                                  AND ixc.column_id = col.column_id
            WHERE   ix.type > 0
                    AND ( ix.is_primary_key = 0
                          OR ix.is_unique_constraint = 0
                        )
                    AND SCHEMA_NAME(tb.schema_id) = @SchemaName
                    AND tb.name = @TableName
                    AND ix.name = @IndexName
            ORDER BY ixc.index_column_id

        OPEN CursorIndexColumn 
        FETCH NEXT FROM CursorIndexColumn INTO @ColumnName, @IsDescendingKey,
            @IsIncludedColumn

        WHILE ( @@fetch_status = 0 )
            BEGIN
                IF @IsIncludedColumn = 0
                    SET @IndexColumns = @IndexColumns + @ColumnName
                        + CASE WHEN @IsDescendingKey = 1 THEN ' DESC, '
                               ELSE ' ASC, '
                          END
                ELSE
                    SET @IncludedColumns = @IncludedColumns + @ColumnName
                        + ', ' 

                FETCH NEXT FROM CursorIndexColumn INTO @ColumnName,
                    @IsDescendingKey, @IsIncludedColumn
            END

        CLOSE CursorIndexColumn
        DEALLOCATE CursorIndexColumn

        SET @IndexColumns = SUBSTRING(@IndexColumns, 1, LEN(@IndexColumns) - 1)
        SET @IncludedColumns = CASE WHEN LEN(@IncludedColumns) > 0
                                    THEN SUBSTRING(@IncludedColumns, 1,
                                                   LEN(@IncludedColumns) - 1)
                                    ELSE ''
                               END
 --  print @IndexColumns
 --  print @IncludedColumns

        SET @TSQLScripCreationIndex = ''
        SET @TSQLScripDisableIndex = ''
        SET @TSQLScripCreationIndex = 'CREATE ' + @is_unique + @IndexTypeDesc
            + ' INDEX ' + QUOTENAME(@IndexName) + ' ON '
            + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + '('
            + @IndexColumns + ') '
            + CASE WHEN LEN(@IncludedColumns) > 0
                   THEN CHAR(13) + 'INCLUDE (' + @IncludedColumns + ')'
                   ELSE ''
              END + CHAR(13) + 'WITH (' + @IndexOptions + ') ON '
            + '[SC_LE]([Id])' + ';'  

        IF @is_disabled = 1
            SET @TSQLScripDisableIndex = CHAR(13) + 'ALTER INDEX '
                + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName)
                + '.' + QUOTENAME(@TableName) + ' DISABLE;' + CHAR(13) 

 --print @TSQLScripCreationIndex
 --print @TSQLScripDisableIndex

        FETCH NEXT FROM CursorIndex INTO @SchemaName, @TableName, @IndexName,
            @is_unique, @IndexTypeDesc, @IndexOptions, @is_disabled,
            @FileGroupName

    END
 CLOSE CursorIndex
 DEALLOCATE CursorIndex