【问题标题】:Problem with CakePHP using autoModel when it shouldn'tCakePHP 在不应该使用 autoModel 时出现问题
【发布时间】:2009-10-02 02:17:23
【问题描述】:

我的视图出现错误:

警告 (512):SQL 错误:1054:“where 子句”中的未知列“Model.id”[CORE\cake\libs\model\datasources\dbo_source.php,第 525 行] $sql = "SELECT Model.model_id, Model.pic_location, Model.model_name FROM models AS Model WHERE Model.@207654330@LI =' " $error = "1054: 'where 子句'中的未知列 'Model.id'" $out = null

不幸的是,由于我正在使用的旧数据库,我的模型被命名为“模型”。尽管我的模型代码(见下文)将primaryKey显式设置为'model_id',但我已经使用调试并确定它使用'id'的primaryKey(也显示在上面的SQL中)。

数据库表是model_id主键的模型,所以我只能假设系统使用的是autoModel。

我的模型代码是'model.php':

    <?php class Model extends AppModel {

var $name = 'Model';
var $primaryKey = 'model_id';

//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasMany = array(
    'Pic' => array(
        'className' => 'Pic',
        'foreignKey' => 'model_name',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);}?>

我最初是手动创建的,当我遇到这个错误时,我烘焙了它......结果相同(除了我没有在 hasMany 关系中包含空字段)。

我暂时关闭了应用程序级缓存,并清除了缓存,但没有任何帮助。如何让它使用我的模型代码?

【问题讨论】:

    标签: php cakephp model


    【解决方案1】:

    恐怕您必须将您的 Model 模型重命名为其他名称。您的Model 类扩展了AppModel,后者又扩展了基础模型类,也称为Model。这样你肯定会惹上麻烦的。

    如果您有旧数据库,只需将 $useTable attribute 设置为使用名称与您的模型不同的表。

    【讨论】:

    • 我认为可能是这种情况,但不确定。已经做到了,它已经修复了它。谢谢。
    猜你喜欢
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2013-04-20
    • 2014-06-05
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多