duanjing1023 2013-05-02 20:55
浏览 31
已采纳

函数名称用作SQL查询

I've got a problem very similar to the ones found here and here, but these solutions aren't helping me fix my version of the problem. I'm using CakePHP 1.3 and I'm trying to call some model functions from within a view controller:

//Class setup
var $uses = array('StudentsTelephone', 'AddressesStudent');

//...

function someFunction(){
    $this->set('telephones', $this->StudentsTelephone->getActiveStudentTelepone($id));
    $this->set('addresses', $this->AddressesStudent->getActiveByStudentId($id));
}

This first of the model function calls (StudentsTelephone) works, but the second (AddressesStudent) fails. It attempts to use the function name as the SQL call resulting in:

SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'getActiveByStudentId' at line 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]

Using a little bit of debugging, I find out that the function is being looked for in the AppModel and AddressesStudent is never checked. Based on those other solutions it looks like it should be a typo in the name. However, everything appears to be correctly named. The above code is as stated, the model file is addresses_student.php, the class name is AddressesStudent and this was working fine before I updated Cake (just recently switched from 1.1 to 1.3). Any suggestions as to what might be causing the problem is not a naming error? Or maybe some other naming error that I'm missing? Thank you much!

EDIT: Also, here's the backtrace telling me that the model PHP file was never found:

DboSource::showQuery() - CORE\cake\libs\model\datasources\dbo_source.php, line 684
DboSource::execute() - CORE\cake\libs\model\datasources\dbo_source.php, line 266
DboSource::fetchAll() - CORE\cake\libs\model\datasources\dbo_source.php, line 410
DboSource::query() - CORE\cake\libs\model\datasources\dbo_source.php, line 364
Model::call__() - CORE\cake\libs\model\model.php, line 502
Overloadable::__call() - CORE\cake\libs\overloadable_php5.php, line 50
AppModel::getActiveByStudentId() - [internal], line ??
StudentsController::edit() - APP\controllers\students_controller.php, line 277
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 204
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
[main] - APP\webroot\index.php, line 83
  • 写回答

2条回答 默认 最新

  • dongtaijiao7140 2013-05-02 21:37
    关注

    CakePHP uses the 'cached' auto-generated Model

    For HABTM relations, CakePHP will automatically generate a 'generic' model for the JOIN-table (by creating an instance AppModel) even if a Model exists for the join-table (don't ask me why!)

    CakePHP stores this auto-generated model in its cache so that it can be reused in other locations.

    In your example, you try to use the AddressesStudent model. CakePHP will look for a model with that name, and finds a model with that name inside the cache. This is, however the auto-generated model for the HABTM relation, not your actual model!

    Tell CakePHP to use your model and not an auto-generated model

    To prevent CakePHP from auto-generating a model for HABTM relations, add the with option to those relations; be sure to specify this at both sides of the relation (if you defined it at both sides, that is)

    Inside the Student model;

    $public hasAndBelongsToMany = array(
        'Address' => array(
            // tell CakePHP to use the AddressesStudent model,
            // NOT and auto-generated model
            'with' => 'AddressesStudent',
        ),
    );
    

    And/or inside the Address model;

    $public hasAndBelongsToMany = array(
        'Student' => array(
            'with' => 'AddressesStudent',
        ),
    );
    

    By doing this, you're preventing that the cache contains auto-generated models.;

    with: Defines the name of the model for the join table. By default CakePHP will auto-create a model for you. Using the example above it would be called RecipesTag. By using this key you can override this default name. The join table model can be used just like any “regular” model to access the join table directly.

    More information; hasAndBelongsToMany (HABTM)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行