【问题标题】:CakePHP Tree behavior errorCakePHP 树行为错误
【发布时间】:2013-10-03 14:21:15
【问题描述】:

我得到的实际错误:

语法错误或访问冲突:1064 您的 SQL 中有错误 句法;检查与您的 MySQL 服务器版本相对应的手册 在第 1 行的“孩子”附近使用正确的语法

这让我发疯,甚至无法想象它为什么会被提高。

在我的控制器CategoryController.php 我有:

public function index($id = null) {
    $this->set('categories', $this->Category->children($id, true));

在模型Category.php

public $actsAs = array('Containable', 'Tree');

CakePHP 版本:2.4.1

【问题讨论】:

  • 请激活调试模式并分享 Cake 所做的 SQL 查询。正如错误消息中明确指出的那样,存在 SQL 语法错误。
  • 还没到生成sql查询的地步
  • @Mopheus 很明显,如果它抛出 MySQL 语法错误。
  • 如果它是实际(有效)模型,您是否调试()$this->Category?如果由于配置错误而退回到 AppModel,这将解释您看到的错误。
  • @mark 是的,它回退到 AppModel。添加您的评论作为答案,我会接受。

标签: php mysql cakephp


【解决方案1】:

根据我的经验,只有当您的模型配置错误并且您的模型回退到 AppModel 时才会发生这种情况。

您应该调试$this->Category。 例如,在插件内部,您需要定义包括插件名称在内的关系:

public $belongsTo = array(
    'Category' => array(
        'className' => 'PluginName.Category',
        'foreignKey' => 'category_id',
    )
);   

专业提示:为了避免这种意外发生在我身上,我在开发设置中包含了以下 sn-p:https://github.com/dereuromark/tools/blob/master/Model/MyModel.php#L45

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多