【问题标题】:Yii framework new column in database not showing in output of $model->getAttributes() method call?Yii 框架数据库中的新列未显示在 $model->getAttributes() 方法调用的输出中?
【发布时间】:2013-09-24 05:54:49
【问题描述】:

Yii 1.1.8 版

我在 mysql 数据库的表中添加了一列, 但新列未显示在 $model->getAttributes() 方法调用的输出中

我删除了受保护/运行时文件夹中的所有文件,但仍然没有列

config: 'schemaCachingDuration' => 0, // 以秒为单位。

我可以直接在数据库中向新列添加数据。

我还能做些什么来调试它吗?

index.php

<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following line when in production mode
    //debug
        defined('YII_DEBUG') or define('YII_DEBUG',true );
        //show profiler
        defined('YII_DEBUG_SHOW_PROFILER') or define('YII_DEBUG_SHOW_PROFILER',true);
        //enable profiling
        defined('YII_DEBUG_PROFILING') or define('YII_DEBUG_PROFILING',true);
        //trace level
        defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',0);
        //execution time
        defined('YII_DEBUG_DISPLAY_TIME') or define('YII_DEBUG_DISPLAY_TIME',false);

require_once($yii);
Yii::createWebApplication($config)->run();

main.php

<?php
return CMap::mergeArray(array(
    'timeZone'=>'UTC',
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'catchAllRequest' => null, // null if online, array('site/offline') if offline,
    'sourceLanguage' => 'en_ca',
    'theme' => 'td',
    'charset' => 'UTF-8',
    'preload' => array('log'),
    'import' => array(
    'application.models.*',
    'application.components.*',
    'application.extensions.*'
    ),
    'modules' => array(
    ),
    // application components
    'components' => array(
    'format' => array(
    ),
    'user' => array(
        // enable cookie-based authentication
        'allowAutoLogin' => true,
        'autoRenewCookie' => true,
    ),
    'widgetFactory' => array(
        'enableSkin' => true,
    ),
    'urlManager' => array(
        ),
    ),
    'db' => array(
        'class' => 'CDbConnection',
        'connectionString' => 'mysql:host=localhost;dbname=mydb',
        'emulatePrepare' => true,
        'initSQLs'=>array("set time_zone='+00:00'"),
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
        'tablePrefix' => 'tbl_',
        'enableParamLogging' => true, //show parameter values in log
//      'schemaCachingDuration' => 0, // in seconds.  <1 means off
        'enableProfiling' => YII_DEBUG_PROFILING, //show sql profile info in log
        'nullConversion' => true,
        //'initSQLs'=>array('set time_zone="+00:00"')
    ),
    'errorHandler' => array(
    ),
    'log' => array(
        'class' => 'CLogRouter',
        'routes' => array(
        array(
            'class' => 'CFileLogRoute',
            'levels' => 'error, warning',
            'filter' => 'CLogFilter',
            'enabled' => !YII_DEBUG
        ),
        array(
            'class' => 'CPhpMailerLogRoute',
            'levels' => 'error',
            'emails' => 'neilmcguigan+tderror@gmail.com',
            'filter' => 'CLogFilter',
            'enabled' => false,
        ),
        array(
            'class' => 'CWebLogRoute', // show log messages on web pages
            'enabled' => YII_DEBUG,
            'filter' => 'CLogFilter',
        //'showInFireBug' => false
        ),
        array(
            'class' => 'CProfileLogRoute',
            'enabled' => YII_DEBUG,
            'showInFireBug' => false
        )
        ),
    ),
    'request' => array(

    ),
    'securityManager'=>array(

    )
    ),
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params' => array(
    ),
), require(dirname( __FILE__ ) . '/override.php'));

【问题讨论】:

  • 我认为你也必须更新你的模态类。
  • 您确定您删除了运行时文件夹中的所有文件 - 并且它是您正在使用的运行时文件夹吗?当我更改我的数据库表时 - 我必须删除一个名为“cache-1.1.8.db”的文件你是否配置过 Yii 以使用不同的运行时目录?
  • Yii 正在使用受保护/运行时,即在此处创建 application.log
  • 尝试从您的配置文件中删除 schemaCachingDuration - 它不应该是必需的。如果这不起作用,请发布您的 index.php 和 main.php 配置文件。
  • 好吧,我不熟悉您在 main.php 文件中使用的一些选项,尝试从 db 条目中删除所有非必要选项,/override.php 中还有什么?因为它正在被合并,顾名思义,它可能会覆盖这里使用的任何值。

标签: php mysql sql yii database-schema


【解决方案1】:

刚刚解决了这个问题——我发现新的字段名需要在模型的attributeLabels函数中

public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'newfield' => 'New Field',
        ...

如果您像我一样并且在您的模型中有很多自定义代码,请谨慎使用“最好还是更新覆盖您的整个模型”建议。正如 Sankalp Singha 建议的那样,只需从使用 gii 复制“差异”(绿色部分)并将属性标签中的部分添加到您的代码中。

【讨论】:

    【解决方案2】:

    在表格中添加新列后,只需转到 Gii,然后单击模型生成器。在那里再次输入您的表格名称,然后单击预览。将有一个 diff 选项单击它,然后复制突出显示的绿色代码并将其复制粘贴到您的原始模式中,或者更好地更新覆盖您的整个模式。然后检查您是否能够获取内部的值并能够获取属性。这应该可以。

    【讨论】:

    • 这不会改变getAttributes() 中属性的存在,您可以创建一个新模型并传入表的名称,仍然可以获得正确的输出 - 无需使用 Gii。这里的问题是由于模型被创建为视图的表示,而不是表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多