Saving cakePHP HABTM data without a form?

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I want to construct my own data array to be saveAll in a HABTM relatioship. Models: 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' => '' ) ); then in 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(); } as you can see I create the data manually here just to test it. I don't have a form that is submited. But data isn't saved and sql dump shows: 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 cakephp habtm link|improve this question edited Mar 19 '11 at 13:24Mat52.7k74372 asked Mar 19 '11 at 13:20gong939 50% accept rate.

Post your DB schema and data you have. The first SELECT statement is searching for blank value! (favorite_id = '') – Ashok Apr 2 '11 at 6:42 can you post the output of debug($toSave)?

– Headshota Apr 6 '11 at 20:58.

Remove this line. $links'id' = $i++; Id fields are auto increment so there is no need to assign a value to it. Then change this part: if(!$this->Favorite->saveAll($toSave'Favorite')) { echo 'Search cannot be saved.

Please try again. '; //return ; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions