Skip to content

Instantly share code, notes, and snippets.

@swthate
Last active August 29, 2015 14:00
Show Gist options
  • Save swthate/11230725 to your computer and use it in GitHub Desktop.
Save swthate/11230725 to your computer and use it in GitHub Desktop.
Add Client (and job)
<!-- 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/Model/Client.php
App::uses('AppModel','Model'); // Let cake know you need the AppModel file
class Client extends AppModel
{
// Relation with Job Model -------------
public $hasMany = array('Job');
// Validation --------------------------
public $validate = array(
'name' => array('rule' => 'notEmpty')
);
}
<?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->saveAll($this->request->data, array('atomic' => FALSE, 'deep' => TRUE));
if (!empty($client))
{
$this->request->data['Job']['client_id'] = $this->Client->getLastInsertID();
}
}
if (!empty($this->request->data))
{
// Use the following to avoid validation errors:
unset($this->Client->Job->validate['client_id']);
$this->Client->saveAll($this->request->data, array('deep' => true));
}
} // end Add
}
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Client Name' for key 'clientname'
SQL Query: INSERT INTO `moslog`.`clients` (`name`, `contactname`) VALUES ('Client Name', 'Primary Contact Name')
array(
'Client' => array(
'name' => 'Client Name',
'contactname' => 'Primary Contact Name'
),
'Job' => array(
(int) 0 => array(
[maximum depth reached]
),
'client_id' => '59'
)
)
array(
'deep' => true
)
array(
'Job' => array(
(int) 0 => array(
[maximum depth reached]
),
'client_id' => array(
[maximum depth reached]
)
),
'name' => 'Client Name',
'contactname' => 'Primary Contact Name'
)
array(
'validate' => false,
'atomic' => true,
'deep' => true
)
array(
'Client' => array(
'name' => 'Client Name',
'contactname' => 'Primary Contact Name'
),
'Job' => array(
(int) 0 => array(
[maximum depth reached]
),
'client_id' => '59'
)
)
array(
'validate' => 'first',
'deep' => true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment