Issue
I have a table called a_forms, and I'm trying to create a drop down menu on a page of AForms that has a list of all the rows of a_forms. I want each choice in the drop down to link directly to the view of that form. Here is my forms table:
fid int(3) unsigned auto_increment (primary key)
title varchar(100)
created timestamp CURRENT_TIMESTAMP on update
modified timestamp
I was able to get as far populating the drop down menu, however I'm having trouble with the second part - linking each choice with the view selected. I'm a beginner to CakePHP so I am probably missing something. Here is the function in AFormsController:
public function forms($id = null)
{
$query = $this->AssessmentForms->find('all');
$result = $query->toArray();
$assessmentForm = $this->AssessmentForms->get($id, [ 'contain' => [] ]);
$this->set('assessmentForm', $assessmentForm);
$this->set('_serialize', ['assessmentForm']);
$data = $this->AssessmentForms->find('list', array('fields' => array('fid', 'title')));
$this->set('forms', $data);
}
forms.ctp:
<?php $this->Form->input('aform.select one', ['type' => 'select', 'options' => $forms, 'empty' => 'Choose One', 'onchange' => 'this.form.submit();']);?>
That gives me "Record not found in table "a_forms" with primary key [NULL]"
I also tried:
<?php $this->Form->create('Forms', array( 'url' => ['controller'=>'a_forms', 'action'=>'view', $aForm->fid])); ?>
And added this to the controller:
$aForms = $this->AForms->find('list');
It doesn't throw an error but I get a "Undefined variable: aForms [APP/Template/AForms/forms.ctp, line 10" in the view
I saw other people struggling with the 'onchange' method so I'm not sure if that's the right approach. I'm using version 3.1.0. Let me know if you need any more information!
Solution
Still don't quite know how to appropriately create queries with find(), but I found a way around with:
<ul class="dropdown-menu">
<?php foreach ($aForms as $aForm): ?>
<li> <?= $this->Html->link($aForm->title, ['action' => 'fill', $aForm->fid]) ?></li>
<?php endforeach; ?>
</ul>
And this in the controller:
$this->set('assessmentForms', $this->paginate($this->AssessmentForms));
$this->set('_serialize', ['assessmentForms']);
I pulled that directly from the bake-generated index() function. If anyone has a more syntactically non-hackish way of solving this, please let me know!
Answered By - nymks
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.