不使用表单保存cakePHP HABTM数据
我想构建我自己的数据数组,以便在HABTM关系中保存所有数据 型号:不使用表单保存cakePHP HABTM数据,cakephp,has-and-belongs-to-many,Cakephp,Has And Belongs To Many,我想构建我自己的数据数组,以便在HABTM关系中保存所有数据 型号: var $hasAndBelongsToMany = array( 'TweetSearch' => array( 'className' => 'TweetSearch', 'joinTable' => 'favorites_tweet_searches', 'foreignKey' => 'favorite_id', 'asso
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' => ''
)
);
然后在U控制器中:
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
拆下这条线
$links['id'] = $i++;
Id字段是自动递增的,因此无需为其赋值。
然后更改此部分:
if(!$this->Favorite->saveAll($toSave['Favorite']))
{
echo 'Search cannot be saved. Please try again.';
//return ;
}
拆下这条线
$links['id'] = $i++;
Id字段是自动递增的,因此无需为其赋值。
然后更改此部分:
if(!$this->Favorite->saveAll($toSave['Favorite']))
{
echo 'Search cannot be saved. Please try again.';
//return ;
}
发布您的数据库模式和数据。第一个SELECT语句正在搜索空白值!favorite_id=您可以发布debug$toSave的输出吗?发布您的数据库模式和数据。第一个SELECT语句正在搜索空白值!favorite_id=您可以将debug$toSave的输出发布到存储吗?