CakePHP saveAssociated not saving HasMany Through Model Data -
i'm trying associated models in cakephp 2.3 save properly, i'm having issues. i'm storing posts, , want know links in posts. each of links, i'd store anchor text if available. database set in following diagram.
database diagram http://derekperkins.com/wp-content/uploads/cakephp-database.png
anchor model
class anchor extends appmodel { public $hasmany = array( 'postsurl' => array( 'classname' => 'postsurl', 'foreignkey' => 'anchor_id', 'dependent' => false ) ); public function save($data = null, $validate = true, $fieldlist = array()) { $id = anchor::find('first', array( 'fields' => array('id'), 'recursive' => -1, 'conditions' => array('anchor' => $data['anchor']) )); if( $id ) $data['id'] = $id['anchor']['id']; return parent::save($data, $validate, $fieldlist); } } url model
class url extends appmodel { public $hasmany = array( 'postsurl' => array( 'classname' => 'postsurl', 'foreignkey' => 'url_id', 'dependent' => false ) ); public function save($data = null, $validate = true, $fieldlist = array()) { $id = url::find('first', array( 'fields' => array('id'), 'recursive' => -1, 'conditions' => array('url' => $data['url']) )); if( $id ) $data['id'] = $id['url']['id']; return parent::save($data, $validate, $fieldlist); } } postsurl model
class postsurl extends appmodel { public $belongsto = array( 'post' => array( 'classname' => 'post', 'foreignkey' => 'post_id' ), 'url' => array( 'classname' => 'url', 'foreignkey' => 'url_id' 'anchor' => array( 'classname' => 'url', 'foreignkey' => 'anchor_id' )*/ ); } post model
class post extends appmodel { public $hasmany = array( 'postsurl' => array( 'classname' => 'postsurl', 'foreignkey' => 'post_id', 'dependent' => false ) ); public function save($data = null, $validate = true, $fieldlist = array()) { $id = post::find('first', array( 'fields' => array('id'), 'recursive' => -1, 'conditions' => array('external_post_id' => $data['external_post_id']) )); if( $id ) $data['id'] = $id['post']['id']; return parent::save($data, $validate, $fieldlist); } } submitting data
i've created form test model. code i'm using save array created form. getting message saying things saved successfully, post saves. nothing entered other 3 tables. i'm using debugkit , no sql calls reference of data.
$this->post->saveassociated($this->request->data, array('deep' => true)) array ( [post] => array ( [external_post_id] => 12345 [sentiment_score] => 3.3 ) [url] => array ( [url] => http://test.com ) [anchor] => array ( [anchor] => test anchor ) ) i've tried formatting arrays have url , anchor underneath postsurl subarray, didn't work either.
my model::save functions there keep me duplicating data, , work in other models have used in past (though i'm open suggestions on better way this, uses database call each check). i've tried commenting them out, , doesn't affect code. how should structure save properly?
first of model::save functions, example:
public function save($data = null, $validate = true, $fieldlist = array()) { $id = post::find('first', array( 'fields' => array('id'), 'recursive' => -1, 'conditions' => array('external_post_id' => $data['external_post_id']) )); if( $id ) $data['id'] = $id['post']['id']; pr($data); return parent::save($data, $validate, $fieldlist); } it prints $data in way :
array ( [id] => 3 //for example 3 [post] => array ( [external_post_id] => 12345 [sentiment_score] => 3.3 ) [url] => array ( [url] => http://test.com ) [anchor] => array ( [anchor] => test anchor ) ) this $data incorrect, correct data is:
array ( [post] => array ( [id] => 3 //for example 3 [external_post_id] => 12345 [sentiment_score] => 3.3 ) [url] => array ( [url] => http://test.com ) [anchor] => array ( [anchor] => test anchor ) ) you must change model::save function way:
public function save($data = null, $validate = true, $fieldlist = array()) { $id = post::find('first', array( 'fields' => array('id'), 'recursive' => -1, 'conditions' => array('external_post_id' => $data['external_post_id']) )); if( $id ) $data[$this->name]['id'] = $id['post']['id']; return parent::save($data, $validate, $fieldlist); } second , can't save data single save, should save data way:
$postdata = array ( 'post' => array ( 'external_post_id' => 12345 'sentiment_score' => 3.3 ) ); $this->post->save($data); $posturlid = $this->postsurl->find('first', array( 'conditions' => array( 'post_id' => $this->post->id ), 'fields' => array('id') )); $urlanchordata = array( 'url' => array ( 'url' => 'http://test.com' ), 'anchor' => array ( 'anchor' => 'test anchor' ), 'postsurl' => array( 'id' => $posturlid['postsurl']['id'] ) ); $this->postsurl->saveall('$urlanchordata');
Comments
Post a Comment