Hey trying to get an id from one Controller (Template) to another (Field). Trying to get Template's id to automatically appear in the add (view) input field in the form.
FieldsController with the segment which we have tried:
$template = $this->Template->find('first');
$current_template = $this->Template->request->data($template['Template']['id']);
Input field from the add Fields view:
echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text', 'default' => $current_template['templates_id']));
So what can we do to get this task done? I thought what I tried would have worked, but doesn't, comes up with "Call to a member function find() on a non-object."
'templates_id' is the foreign key in the 'Field' table which links to 'id' in the 'Template' table.
EDIT:
Added LoadModel (below) and spits out an error: Call to a member function data() on a non-object.
Are we doing the below correctly? Or should it be $this->request->data($template['Template']['id']);
$this->loadModel('Template');
$template = $this->Template->find('first');
$current_template = $this->Template->request->data($template['Template']['id']);
EDIT 2:
FieldsController with add function:
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.");
$this->loadModel('Template');
$template = $this->Template->find('first');
//$current_template = $template['Template']['id'];
// right way to do it, but Template is undefined, and says undefined var
//$current_template = $this->request->data['Template']['id'];
// makes sense with the find, no errors, but still doesnt print in form, says undefined var
$current_template = $this->request->data($template['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.');
}
}
}
Form from the view called add:
<?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('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();
?>
$this->Template->request->data($template['Template']['id']);
is wrong.
Look at the MVC pattern:
The dispatcher sends data to a controller.
Now look at your code:
$this->Template->request->data($template['Template']['id']);
$this
is a controller.
->Template
calls the associated model.
request->data()
calls a function from this Model with data from the View.
In short: you're passing data form the View to the Model, which breaks the MVC pattern.
It should be:
$this->request->data['Template']['id'];
EDIT: Two problems I think:
I think it should be:
$current_template = $this->request->data['Field']['template_id'];
$current_template = $this->request->data($template['Template']['id']);
This variable is not set.
Put something like this:
$this->set(compact('current_template'));