Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp save multiple records in one model
    primarykey
    data
    text
    <p>I have problem to save multiple records in a single table. This is my form:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Attendance');?&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Student&lt;/th&gt; &lt;th&gt;Attendance&lt;/th&gt; &lt;/tr&gt; &lt;?php foreach ($students as $key =&gt; $student): ?&gt; &lt;tr&gt; &lt;td&gt; &lt;?php echo $student['LectureParticipant']['name']; ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('Attendance.'.$key.'.student_id', array('type'=&gt;'hidden', 'value'=&gt;$student['LectureParticipant']['student_id'])); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('Attendance.'.$key.'.lecture_id', array('type'=&gt;'hidden', 'value'=&gt;$student['LectureParticipant']['lecture_id'])); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('Attendance.'.$key.'.date', array('type'=&gt;'hidden', 'value'=&gt;date("Y-m-d"))); ?&gt; &lt;/td&gt; &lt;td&gt; &lt;?php echo $this-&gt;Form-&gt;input('Attendance.'.$key.'.participant', array( 'label' =&gt; false, 'div' =&gt; false, 'type' =&gt; 'select', 'options' =&gt; array( '1' =&gt; 'Present', '0' =&gt; 'Absent', ), )); ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach; ?&gt; &lt;/table&gt; &lt;center&gt; &lt;?php echo $this-&gt;Form-&gt;submit('Save', array('class'=&gt;'greenBtn', 'div'=&gt;false));?&gt; &lt;?php echo $this-&gt;Html-&gt;link("Cancel", array('action' =&gt; 'viewTeacherClass'), array( 'class' =&gt; 'button redBtn')); ?&gt; &lt;?php echo $this-&gt;Form-&gt;end();?&gt; &lt;/center&gt; </code></pre> <p>and this is my controller:</p> <pre><code>if ($this-&gt;request-&gt;is('post')) { if ($this-&gt;Attendance-&gt;saveAll($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash('Attendance has been saved.'); $this-&gt;redirect(array('controller'=&gt;'programmes', 'action' =&gt; 'viewTeacherClass')); } else { $this-&gt;Session-&gt;setFlash('Unable to add attendance.'); } } </code></pre> <p>This is my data structure:</p> <pre><code>array( 'Attendance' =&gt; array( (int) 0 =&gt; array( 'student_id' =&gt; '1', 'lecture_id' =&gt; '2', 'date' =&gt; '2013-08-02', 'participant' =&gt; '1' ), (int) 1 =&gt; array( 'student_id' =&gt; '2', 'lecture_id' =&gt; '2', 'date' =&gt; '2013-08-02', 'participant' =&gt; '1' ) ) ) </code></pre> <p>I got this warning:</p> <p><code>Warning (2): array_keys() expects parameter 1 to be array, null given [CORE\Cake\Model\Model.php, line 2045]</code></p> <p><code>Warning (4096): Argument 1 passed to Hash::numeric() must be an array, null given, called in C:\wamp\www\ibae\lib\Cake\Model\Model.php on line 2045 and defined [CORE\Cake\Utility\Hash.php, l</code></p> <p>I'm not sure what goes wrong here. I guess because of this warning, I cannot save all the data.</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.
 

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