Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:32:17+00:00 2026-06-09T06:32:17+00:00

Hey guys trying to declare a variable in CakePHP in the Fields Controller. This

  • 0

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.

Here is the Fields Controller:

<?php
class FieldsController extends AppController{
public $uses = array('Template');


 function add(){

    $this->set('title_for_layout', 'Please Enter Your Invoice Headings');
    $this->set('stylesheet_used', 'style');
    $this->set('image_used', 'eBOXLogo.jpg');   

    $this->Session->setFlash("Please create your required fields.");
    $templates = $this->Template->find('list');
    //$current_template = $this->request->data['Field']['template_id'];

    // right way to do it, but Template is undefined, and says undefined var
    //$template = $this->request->data['Field']['template_id'];

    // makes sense with the find, no errors, but still doesnt print in form, says undefined var
    //$current_template = $this->request->data($template['Field']['template_id']);

        if($this->request->is('post'))
        {

        $this->Field->create(); 

        if ($this->Field->save($this->request->data)) 
        {   
            if($this->request->data['submit'] == "type_1") 
                { 
                    $this->Session->setFlash('The field has been saved');  
                    $this->redirect( array('controller' => 'fields','action' => 'add'));
                } 
                if($this->request->data['submit'] == "type_2") 
                { 
                    $this->Session->setFlash('The template has been saved'); 
                    $this->redirect( array('controller' => 'templates','action' => 'index'));
                } 


        }
        else
        {
            $this->Session->setFlash('The field could not be saved. Please, try again.'); 
        } 
    } 
  }

} 

And here is our add view which adds fields:

<?php


    echo $this->Form->create('Field', array('action'=>'add'));


    echo $this->Form->create('Field', array('action'=>'add'));
    echo $this->Form->input('name', array('label'=>'Name: '));
    echo $this->Form->input('description', array('label'=>'Description: '));
    //echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'type' => 'select', 'options' => $templates));
    echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'type' => 'text', 'default'=> $templates));
    //echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text', 'default' => $current_template['templates_id']));//this would be the conventional fk fieldname
    echo $this->Form->button('Continue adding fields', array('name' => 'submit', 'value' => 'type_1'));
    echo $this->Form->button('Finish adding fields', array('name' => 'submit', 'value' => 'type_2'));
    echo $this->Form->end();


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:32:18+00:00Added an answer on June 9, 2026 at 6:32 am

    You should try the following code:

    <?php
    class FieldsController extends AppController{
      public $uses = array('Template', 'Field');
      function add(){
    $this->set('title_for_layout', 'Please Enter Your Invoice Headings');
    $this->set('stylesheet_used', 'style');
    $this->set('image_used', 'eBOXLogo.jpg');   
    
    $this->Session->setFlash("Please create your required fields.");
    $templates = $this->Template->find('list', array('fields' => array('Template.id, Template.template_name' );
    $this->set('templates', $templates);
    
    //$current_template = $this->request->data['Field']['template_id'];
    
    // right way to do it, but Template is undefined, and says undefined var
    //comment: You should check the request data with in if condition 
    //$template = $this->request->data['Field']['template_id'];
    
    // makes sense with the find, no errors, but still doesnt print in form, says undefined var
    //$current_template = $this->request->data($template['Field']['template_id']);
    
        if($this->request->is('post'))
        {
    
        $this->Field->create(); 
    
        if ($this->Field->save($this->request->data)) 
        {   
            if($this->request->data['submit'] == "type_1") 
                { 
                    $this->Session->setFlash('The field has been saved');  
                    $this->redirect( array('controller' => 'fields','action' => 'add'));
                } 
                if($this->request->data['submit'] == "type_2") 
                { 
                    $this->Session->setFlash('The template has been saved'); 
                    $this->redirect( array('controller' => 'templates','action' => 'index'));
                } 
    
    
        }
        else
        {
            $this->Session->setFlash('The field could not be saved. Please, try again.'); 
        } 
     } 
      }
    
    } 
    

    You view should looks like:

    <?php
    
    echo $this->Form->create('Field', array('action'=>'add'));
    echo $this->Form->input('name', array('label'=>'Name: '));
    echo $this->Form->input('description', array('label'=>'Description: '));
    echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'options' => $templates));
    //echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'type' => 'text', 'default'=> $templates));
    //echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text', 'default' => $current_template['templates_id']));//this would be the conventional fk fieldname
    echo $this->Form->button('Continue adding fields', array('name' => 'submit', 'value' => 'type_1'));
    echo $this->Form->button('Finish adding fields', array('name' => 'submit', 'value' => 'type_2'));
    echo $this->Form->end();
    
     ?>
    

    Kindly check and verify if it is working for you or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I'm trying to work my way through this but am having an
Hey guys, i'm trying this example from Dave Ward blog about jQuery Templates and
Hey guys I am getting this exception when trying to run the query Column
Hey guys, I am just trying to pull all the records from my database
hey guys im trying to do a SELECT Statement from a local SQLDB so
hey guys im trying to save a url link in a html form but
Hey guys, trying to optimize this query to solve a duplicate user issue: SELECT
Hey guys I am having a lot of trouble trying to understand this and
Hey guys I'm trying to make a basic calculator (polish style) but can't figure
Hey guys i have been trying to download it from the below link http://www.enterprisedb.com/products-services-training/pgdownload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.