【问题标题】:Cakephp 2.0 hasAndBelongsToMany Model errorCakephp 2.0 hasAndBelongsToMany 模型错误
【发布时间】:2012-03-20 09:41:01
【问题描述】:

我烘焙了一个图像、画廊和 gallery_images 模型并将它们与 HABTM 关联相关联,但我相信我在某个地方犯了错误。

尝试访问 images/view/1 页面时返回以下错误。

错误:SQLSTATE[42S22]:未找到列:1054 Unknown column 'GalleriesImage.image_id' in 'on clause'

SQL查询:SELECT Gallery.id, Gallery.category, GalleriesImage.id, GalleriesImage.galleries_id, GalleriesImage@@ROM @987654330 AS Gallery JOIN galleries_images AS GalleriesImage ON (GalleriesImage.image_id = 1 AND GalleriesImage.gallery_id = Gallery.id)

我相信最后一个GalleriesImage.gallery_id = Gallery.id 应该是 GalleriesImage.gallery_id = 1

我对此很陌生,所以任何关于我应该在哪里寻找的提示或任何解释都将不胜感激。

谢谢!

【问题讨论】:

    标签: cakephp-2.0


    【解决方案1】:

    hasAndBelongsToMany 基本上只是两组不同的 hasMany 和 belongsTo。

    我自己的 cakephp webapp 有超过 30 张桌子,这让我放弃了使用 hasAndBelongsToMany。

    我通过 JOin 模型选择了 hasMany。见here

    另外,为了更好地回答您的问题,最好在您的问题中重现您的模型关系和 ->save() 代码。

    只有 sql 语句是不够的。

    【讨论】:

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