Skip to content

Instantly share code, notes, and snippets.

@swthate
Last active August 29, 2015 14:00
Show Gist options
  • Save swthate/11193868 to your computer and use it in GitHub Desktop.
Save swthate/11193868 to your computer and use it in GitHub Desktop.
Saving Related Model Data
<!-- File: /app/Views/Clients/add.ctp -->
<h2>Add Client</h2>
<?php /*
echo $this->Form->create('Client');
echo $this->Form->input('name');
echo $this->Form->input('contactname');
echo $this->Form->end('Save Client');
*/ ?>
<?php
echo $this->Form->create('Client', array('action' => 'add'));
echo $this->Form->input('Client.name', array('label' => 'Client Name'));
echo $this->Form->input('Client.contactname', array('label' => 'Client Rep Name'));
?>
<h3>Add New Job (Optional)</h3>
<?php
echo $this->Form->input('Job.0.title', array('label' => 'Job Title'));
echo $this->Form->input('Job.0.datein', array('label' => 'Job Date'));
echo $this->Form->input('Job.0.description', array('rows' => '4'));
echo $this->Form->end('Add');
?>
<?php
// File: /app/Controller/ClientsController.php
App::uses('AppController','Controller');
class ClientsController extends AppController
{
// Load the Html and Form Helper:
public $helpers = array('Html', 'Form');
// Load the SesionComponent so we can use setFlash():
public $components = array('Session');
# ----------------------------------------- #
# - INDEX action -------------------------- #
# ----------------------------------------- #
public function index()
{
// Get all the Clients and pass them to index.ctp as $clients:
$this->set('clients', $this->Client->find('all'));
}
# ----------------------------------------- #
# - VIEW Client action -------------------- #
# ----------------------------------------- #
// This gets called when people go to ../clients/view/
public function view($id = null)
{
// If there is no Client ID passed:
if(is_null($id)):
/* Display a message on next page load
(your layouts file should fetch this
somewhere - default.ctp will already do it) */
$this->Session->setFlash('Hey, you need to pick a Client to view!');
// Redirect the user to the index page to pick a Client:
$this->redirect('index');
endif;
// Tell your Client model to also contain the Job data:
$this->Client->contain(array('Job'));
// Get the data from the database and store it as $client:
$client = $this->Client->findById($id);
// Pass Client to the view.ctp file:
$this->set('client', $client);
} // end View
# ----------------------------------------- #
# - ADD Client action --------------------- #
# ----------------------------------------- #
public function add()
{
/* if ($this->request->is('post'))
{
$this->Client->create();
if ($this->Client->save($this->request->data))
{
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your client.'));
} */
if (!empty($this->request->data))
{
$client = $this->Client->save($this->request->data);
if (!empty($client))
{
$this->request->data['Job']['client_id'] = $this->Client->id;
$this->Client->Job->save($this->request->data);
}
}
if (!empty($this->request->data))
{
// Use the following to avoid validation errors:
unset($this->Client->Job->validate['client_id']);
$this->Client->saveAssociated($this->request->data);
}
} // end Add
# ----------------------------------------- #
# - EDIT Client action -------------------- #
# ----------------------------------------- #
public function edit($id = null)
{
if (!$id)
{
throw new NotFoundException(__('Invalid client'));
}
$client = $this->Client->findById($id);
if (!$client)
{
throw new NotFoundException(__('Invalid client'));
}
if ($this->request->is(array('client', 'put')))
{
$this->Client->id = $id;
if ($this->Post->save($this->request->data))
{
$this->Session->setFlash(__('Your Client has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if (!$this->request->data)
{
$this->request->data = $client;
}
} // end Edit
# ----------------------------------------- #
# - DELETE Client action ------------------ #
# ----------------------------------------- #
public function delete($id)
{
if ($this->request->is('get'))
{
throw new MethodNotAllowedException();
}
if ($this->Client->delete($id))
{
$this->Session->setFlash(
__('The Client with id: %s has been deleted.', h($id))
);
return $this->redirect(array('action' => 'index'));
}
} // end Delete
}
array(
'Client' => array(
'name' => 'Heralding',
'contactname' => 'Falken'
),
'Job' => array(
(int) 0 => array(
[maximum depth reached]
),
'client_id' => '15'
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment