Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP saveAssociated not saving HasMany Through Model Data
    text
    copied!<p>I'm trying to get my associated models in CakePHP 2.3 to save properly, but I'm having issues. I'm storing posts, and I want to know what links are in those posts. For each of those links, I'd like to store anchor text if it is available. My database is set up as in the following diagram.</p> <p><a href="http://derekperkins.com/wp-content/uploads/cakephp-database.png">Database Diagram http://derekperkins.com/wp-content/uploads/cakephp-database.png</a></p> <p><strong>Anchor Model</strong></p> <pre><code>class Anchor extends AppModel { public $hasMany = array( 'PostsUrl' =&gt; array( 'className' =&gt; 'PostsUrl', 'foreignKey' =&gt; 'anchor_id', 'dependent' =&gt; false ) ); public function save($data = NULL, $validate = true, $fieldList = array()) { $id = Anchor::find('first', array( 'fields' =&gt; array('id'), 'recursive' =&gt; -1, 'conditions' =&gt; array('anchor' =&gt; $data['anchor']) )); if( $id ) $data['id'] = $id['Anchor']['id']; return parent::save($data, $validate, $fieldList); } } </code></pre> <p><strong>URL Model</strong></p> <pre><code>class Url extends AppModel { public $hasMany = array( 'PostsUrl' =&gt; array( 'className' =&gt; 'PostsUrl', 'foreignKey' =&gt; 'url_id', 'dependent' =&gt; false ) ); public function save($data = NULL, $validate = true, $fieldList = array()) { $id = Url::find('first', array( 'fields' =&gt; array('id'), 'recursive' =&gt; -1, 'conditions' =&gt; array('url' =&gt; $data['url']) )); if( $id ) $data['id'] = $id['Url']['id']; return parent::save($data, $validate, $fieldList); } } </code></pre> <p><strong>PostsUrl Model</strong></p> <pre><code>class PostsUrl extends AppModel { public $belongsTo = array( 'Post' =&gt; array( 'className' =&gt; 'Post', 'foreignKey' =&gt; 'post_id' ), 'Url' =&gt; array( 'className' =&gt; 'Url', 'foreignKey' =&gt; 'url_id' 'Anchor' =&gt; array( 'className' =&gt; 'Url', 'foreignKey' =&gt; 'anchor_id' )*/ ); } </code></pre> <p><strong>Post Model</strong></p> <pre><code>class Post extends AppModel { public $hasMany = array( 'PostsUrl' =&gt; array( 'className' =&gt; 'PostsUrl', 'foreignKey' =&gt; 'post_id', 'dependent' =&gt; false ) ); public function save($data = NULL, $validate = true, $fieldList = array()) { $id = Post::find('first', array( 'fields' =&gt; array('id'), 'recursive' =&gt; -1, 'conditions' =&gt; array('external_post_id' =&gt; $data['external_post_id']) )); if( $id ) $data['id'] = $id['Post']['id']; return parent::save($data, $validate, $fieldList); } } </code></pre> <p><strong>Submitting Data</strong></p> <p>I've created a form to test my model. This is the code I'm using to save the array created by the form. I am getting a message saying that things saved successfully, but only the post saves. Nothing is entered into the other three tables. I'm also using DebugKit and no SQL calls reference any of that data.</p> <pre><code>$this-&gt;Post-&gt;saveAssociated($this-&gt;request-&gt;data, array('deep' =&gt; true)) Array ( [Post] =&gt; Array ( [external_post_id] =&gt; 12345 [sentiment_score] =&gt; 3.3 ) [URL] =&gt; Array ( [url] =&gt; http://test.com ) [Anchor] =&gt; Array ( [anchor] =&gt; Test Anchor ) ) </code></pre> <p>I've also tried formatting my arrays to have the URL and Anchor underneath PostsUrl as a subarray, but that didn't work either.</p> <p>My Model::save functions are there to keep me from duplicating data, and they work properly in other models I have used in the past (though I'm open to suggestions on a better way to do this, as this uses a database call for each check). I've also tried commenting them out, and it doesn't affect my code. How should I structure this to save properly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload