Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP declaring variables
    primarykey
    data
    text
    <p>Hey guys trying to declare a variable in CakePHP in the Fields Controller. This variable will display the template id from the template table, but the view is saying the variable is undefined even though we delared it in the controller. Temaplates has many fields and fields belongs to templates.</p> <p>Here is the Fields Controller:</p> <pre><code>&lt;?php class FieldsController extends AppController{ public $uses = array('Template'); function add(){ $this-&gt;set('title_for_layout', 'Please Enter Your Invoice Headings'); $this-&gt;set('stylesheet_used', 'style'); $this-&gt;set('image_used', 'eBOXLogo.jpg'); $this-&gt;Session-&gt;setFlash("Please create your required fields."); $templates = $this-&gt;Template-&gt;find('list'); //$current_template = $this-&gt;request-&gt;data['Field']['template_id']; // right way to do it, but Template is undefined, and says undefined var //$template = $this-&gt;request-&gt;data['Field']['template_id']; // makes sense with the find, no errors, but still doesnt print in form, says undefined var //$current_template = $this-&gt;request-&gt;data($template['Field']['template_id']); if($this-&gt;request-&gt;is('post')) { $this-&gt;Field-&gt;create(); if ($this-&gt;Field-&gt;save($this-&gt;request-&gt;data)) { if($this-&gt;request-&gt;data['submit'] == "type_1") { $this-&gt;Session-&gt;setFlash('The field has been saved'); $this-&gt;redirect( array('controller' =&gt; 'fields','action' =&gt; 'add')); } if($this-&gt;request-&gt;data['submit'] == "type_2") { $this-&gt;Session-&gt;setFlash('The template has been saved'); $this-&gt;redirect( array('controller' =&gt; 'templates','action' =&gt; 'index')); } } else { $this-&gt;Session-&gt;setFlash('The field could not be saved. Please, try again.'); } } } } </code></pre> <p>And here is our add view which adds fields:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Field', array('action'=&gt;'add')); echo $this-&gt;Form-&gt;create('Field', array('action'=&gt;'add')); echo $this-&gt;Form-&gt;input('name', array('label'=&gt;'Name: ')); echo $this-&gt;Form-&gt;input('description', array('label'=&gt;'Description: ')); //echo $this-&gt;Form-&gt;input('template_id',array('label'=&gt;'Template ID: ', 'type' =&gt; 'select', 'options' =&gt; $templates)); echo $this-&gt;Form-&gt;input('template_id',array('label'=&gt;'Template ID: ', 'type' =&gt; 'text', 'default'=&gt; $templates)); //echo $this-&gt;Form-&gt;input('templates_id', array('label'=&gt;'Template ID: ', 'type' =&gt; 'text', 'default' =&gt; $current_template['templates_id']));//this would be the conventional fk fieldname echo $this-&gt;Form-&gt;button('Continue adding fields', array('name' =&gt; 'submit', 'value' =&gt; 'type_1')); echo $this-&gt;Form-&gt;button('Finish adding fields', array('name' =&gt; 'submit', 'value' =&gt; 'type_2')); echo $this-&gt;Form-&gt;end(); ?&gt; </code></pre>
    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.
 

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