没有表格保存cakePHP HABTM数据

时间:2011-03-19 13:20:01

标签: cakephp has-and-belongs-to-many

我想构建自己的数据数组,以便在HABTM关系中保存所有。

型号:

var $hasAndBelongsToMany = array(
    'TweetSearch' => array(
        'className' => 'TweetSearch',
        'joinTable' => 'favorites_tweet_searches',
        'foreignKey' => 'favorite_id',
        'associationForeignKey' => 'tweet_search_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

var $hasAndBelongsToMany = array(
    'Favorite' => array(
        'className' => 'Favorite',
        'joinTable' => 'favorites_tweet_searches',
        'foreignKey' => 'tweet_search_id',
        'associationForeignKey' => 'favorite_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

然后在favorites_controller中:

function add($searchText='lala') 
{
    $tweetSearch = trim($searchText);
    //Save tweet search
    $tweet = $this->Favorite->TweetSearch->find('first',array(
        'conditions'=>array('TweetSearch.search' => $tweetSearch),
        'recursive' => -1));

    if(empty($tweet))
    {
            if(!$this->Favorite->TweetSearch->save(array('TweetSearch.search' => $tweetSearch)))
            {
                echo 'Search cannot be saved. Please try again.';
                return ;
            }
            //debug('ok');
            $tweetID = $this->Favorite->TweetSearch->id;
            $toSave = array('TweetSearch' => array('id' => $tweetID));
    }
    else
    {
        $tweetID = $tweet['TweetSearch']['id'];
        $toSave = $tweet;
    }

    $links = array();
    $this->params['form']['favlinks'] = array('http://www.lala.gr','http://www.anarxeio.gr');
    $i=0;   
    foreach($this->params['form']['favlinks'] as $link)
    {
        $links['user_id'] = $this->_userDetails['id'];
        $links['link'] = trim($link);
        $links['id'] = $i++;

        //$links['FavoritesTweetSearch']=array('tweet_search_id' => $tweetID);

        $toSave['Favorite'][] = $links;
    }

    if(!$this->Favorite->saveAll($toSave))
    {
            echo 'Search cannot be saved. Please try again.';
            //return ;
        }

    debug($toSave);

    //exit();
}

正如您所看到的,我在这里手动创建数据只是为了测试它。我没有提交的表单。 但数据没有保存,sql转储显示:

9   START TRANSACTION       0       0
10  SELECT `FavoritesTweetSearch`.`tweet_search_id` FROM `favorites_tweet_searches` AS `FavoritesTweetSearch` WHERE `FavoritesTweetSearch`.`favorite_id` = ''       1   1   0
11  DELETE `FavoritesTweetSearch` FROM `favorites_tweet_searches` AS `FavoritesTweetSearch` WHERE `FavoritesTweetSearch`.`favorite_id` = '' AND `FavoritesTweetSearch`.`tweet_search_id` = (0)      1       0
12  INSERT INTO `favorites_tweet_searches` (`favorite_id`,`tweet_search_id`) VALUES ('','1'), ('','0')      2       0
13  COMMIT

1 个答案:

答案 0 :(得分:0)

删除此行。

$links['id'] = $i++;

Id字段是自动递增的,因此无需为其指定值。 然后改变这一部分:

if(!$this->Favorite->saveAll($toSave['Favorite']))
{
    echo 'Search cannot be saved. Please try again.';
    //return ;
}