douduo2407 2014-10-10 06:54
浏览 48

CakePHP中的hasAndBelongsToMany关系不保存数据

I am facing a problem in CakePHP. I want hasAndBelongsToMany relationship to bs_categories and bs_listing_types tables. ( A category can have multiple listing types).

These are the table structure that i have used.

bs_categories
-------------
id             int(10)
parent_id      int(10)
lft            int(10)
rght           int(10)
title          varchar(255)
body           text
meta_title     varchar(255)
meta_keyword   text
meta_desc      text
slug           varchar(255)
status         int(1)
created        datetime
modified       datetime

bs_listing_types
-----------------
id          int(11)
title       varchar(255)
status      int(1)
created     datetime
modified    datetime

bs_listing_types_categories
---------------------------
id              int(11)
listing_type_id int(11)
category_id     int(11)

I have written following code in the model:

/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
public $hasAndBelongsToMany = array(
    'ListingType' => array(
        'className'             => 'ListingType',
        'joinTable'             => 'listing_types_categories',
        'foreignKey'            => 'listing_type_id',
        'associationForeignKey' => 'category_id',
        'unique'                => 'keepExisting',
        'conditions'            => '',
        'fields'                => '',
        'order'                 => '',
        'limit'                 => '',
        'offset'                => '',
        'finderQuery'           => '',
    )
);

At time of saving, i am getting following data.

Array(
[Category] => Array
    (
        [listing_type_id] => Array
            (
                [0] => 2
                [1] => 3
            )

        [parent_id] =>
        [id] => 1
        [title] => Advert
        [meta_title] => Advert
        [meta_keyword] => Advert
        [meta_desc] => Advert
        [status] => 1
    ))

But the problem is that it is not saving the data into the bs_listing_types_categories table. Am i doing anything wrong. Please HELP!

  • 写回答

2条回答 默认 最新

  • doulao3078 2014-10-10 08:26
    关注

    HABTM NEVER has ID column as per cakePHP convention else ,data wont save.

    bs_listing_types_categories
    

    Will have following fields only

    listing_type_id int(11)
    category_id     int(11)
    

    For more information LINK

    For Example If posts are related to tags in HABTM , then ,check out posts_to_tags Without id field.

    CREATE TABLE `posts` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(255) NOT NULL,
      `body` text NOT NULL,
      PRIMARY KEY  (`id`)
    );
    CREATE TABLE `tags` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    CREATE TABLE `posts_tags` (
      `post_id` int(11) NOT NULL,
      `tag_id` int(11) NOT NULL,
      PRIMARY KEY  (`post_id`,`tag_id`)
    );
    

    Your table structure

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条