Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well HATBM works in an very strange way. If you really need HATBM I would suggest NOT changing a model association, but using an interesting shorthand for saving additional data fields in the join table.</p> <p>I've encountered this a lot of times and this is the best solution: Actually, just for saving your additional fields you can unbind all HATBM relations and add a new hasMany binding "on the fly" pointing to the join model. Firstly you should unbind the existing HATBM relation.</p> <pre><code>$this-&gt;Order-&gt;unbindModel(array('hasAndBelongsToMany' =&gt; array('Product'))); </code></pre> <p>Then add a new "on-the-fly" hasMany binding:</p> <pre><code>$this-&gt;Order-&gt;bindModel(array('hasMany' =&gt; array('OrdersPost'))); </code></pre> <p>Then if your data is:</p> <pre><code>$this-&gt;data['Order']['id'] = '1'; $this-&gt;data['OrdersPost'][0]['product_id'] = '15'; $this-&gt;data['PostsTag'][0]['price'] = '5000.00'; $this-&gt;data['PostsTag'][0]['quantity'] = '1'; $this-&gt;data['OrdersPost'][1]['product_id'] = '16'; $this-&gt;data['PostsTag'][1]['price'] = '4000.00'; $this-&gt;data['PostsTag'][1]['quantity'] = '2'; </code></pre> <p>As mentioned above, HATBM always erases all existing records before inserting new ones. So maybe you should take care about that before you do:</p> <pre><code>$this-&gt;Order-&gt;saveAll($this-&gt;data); </code></pre> <p>This works when you dont want to change your datamodel, but still need additional functionality. Hope this helps. <a href="http://nuts-and-bolts-of-cakephp.com/2008/09/24/saving-extra-fields-in-the-join-table-for-habtm-models/" rel="nofollow">Original Solution and Credits HERE</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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