Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to Save BelongsTo Data Using Checkboxes in CakePHP
    primarykey
    data
    text
    <p>I have two models with the relationship <code>Advisor belongsTo Room</code>, <code>Room hasMany Advisor</code>. <code>Advisor</code> has a foreignKey constraint in the database (<code>Advisor.room_id</code>) which points to a specific room. The default value for this is the <code>NULL</code> value (representing an advisor without a room).</p> <p>Suppose I had an <code>Advisor</code>, with <code>room_id</code> set to <code>n</code>. I now wish to unassign the the <code>nth</code> room from <code>Advisor</code> - using a select field, I can reset <code>room_id</code> to <code>NULL</code>, with the following <code>request-&gt;data</code> structure:</p> <pre><code>[Room] =&gt; Array ( [name] =&gt; TestRoom2 [type] =&gt; single suite [id] =&gt; 4 ) [Advisor] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 14 [room_id] =&gt; [name] =&gt; foo ) ) </code></pre> <p>However, when I attempt to do this through the use of checkboxes with the same generated <code>$this-&gt;request-&gt;data</code>, MySQL refuses to update the <code>NULL</code> value.</p> <p>In addition, it seems that changing the value of <code>room_id</code> explicitly in <code>request-&gt;data</code> in the second case has no effect. However, if I were to change <code>Advisor.0.name</code> to <code>hax</code>, (by modifying <code>request-&gt;data</code> directly) the <code>name</code> field <em>does</em> save.</p> <p>I save via calling <code>$this-&gt;Room-&gt;saveAll($request-&gt;data, array('deep' =&gt; true))</code> - this is true both in the case of the select field and the checkbox.</p> <p>I am generating the series of checkboxes by repeatedly calling the <code>Form</code> helper:</p> <pre><code>$count = 0; // $key is the Advisor id, and $attributes is an array of the form // array('name' =&gt; (string), 'disabled' =&gt; (bool)) foreach($options['advisorList'] as $key =&gt; $attributes) { $form[] = $this-&gt;Form-&gt;hidden(sprintf('Advisor.%s.id', $count), array('value' =&gt; $key)); $form[] = $this-&gt;Form-&gt;input(sprintf('Advisor.%s.room_id', $count), array( 'type' =&gt; 'checkbox', 'label' =&gt; $attributes['name'], 'disabled' =&gt; $attributes['disabled'])); $count++; } </code></pre> <p><em>Moreover</em>, if <code>room_id</code> has already been set to <code>NULL</code>, there is no problem setting <code>room_id</code> - except, <code>room_id</code> will be set regardless if the checkbox is checked or not.</p> <p>Any help would be greatly appreciated - thanks!</p>
    singulars
    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.
 

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