-
-
Save swthate/10985144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: /app/Model/AppModel.php --> | |
App::uses('Model', 'Model'); | |
/** | |
* Application model for Cake. | |
* | |
* Add your application-wide methods in the class below, your models | |
* will inherit them. | |
* | |
* @package app.Model | |
*/ | |
class AppModel extends Model { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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->findAll()); | |
} | |
# ----------------------------------------- # | |
# - 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: /app/View/Jobs/index.ctp | |
<h1>Jobs</h1> | |
<p> | |
<?php echo $this->Html->link("Add Post", array('action' => 'add')); ?> | |
</p> | |
<table> | |
<tr> | |
<th>ID</th> | |
<th>Job Title</th> | |
<th>Client</th> | |
<th>Action</th> | |
<th>Date</th> | |
</tr> | |
<!-- Here is where we loop through out $jobs array, printing info --> | |
<?php | |
debug($job['Client']['name']); | |
?> | |
<?php foreach ($jobs as $job): ?> | |
<tr> | |
<td><?php echo $job['Job']['id']; ?></td> | |
<td> | |
<?php | |
echo $this->Html->link( | |
$job['Job']['title'], | |
array('action' => 'view', $job['Job']['id']) | |
); | |
?> | |
</td> | |
<td> | |
<?php | |
echo $this->Html->link( | |
$job['Client']['name'], | |
array( | |
'controller' => 'clients', | |
'action' => 'view', | |
$job['Client']['id'] | |
) | |
); | |
?> | |
</td> | |
<td> | |
<?php | |
echo $this->Form->postLink( | |
'Delete', | |
array('action' => 'delete', $job['Job']['id']), | |
array('confirm' => 'Are you sure?') | |
); | |
?> | |
<?php | |
echo $this->Html->link( | |
'Edit', | |
array('action' => 'edit', $job['Job']['id']) | |
); | |
?> | |
</td> | |
<td> | |
<?php | |
echo $this->Time->format($job['Job']['datein'],'%b %e, %Y'); | |
?> | |
</td> | |
<!--<td><?php echo $job['Job']['datein']; ?></td>--> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
</div><!-- / content --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: app/Model/Job.php | |
App::uses('AppModel','Model'); // Let cake know you need the AppModel file | |
class Job extends AppModel | |
{ | |
// Relation with Client Model -------------- | |
public $belongsTo = array('Client'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: /app/Controller/JobsController.php | |
class JobsController extends AppController | |
{ | |
public $helpers = array('Html', 'Form'); | |
# ----------------------------------------- # | |
# - INDEX action -------------------------- # | |
# ----------------------------------------- # | |
public function index() | |
{ | |
$this->set('jobs', $this->Job->find('all', array('Client'))); | |
} // End "Index" function | |
# ----------------------------------------- # | |
# - VIEW action --------------------------- # | |
# ----------------------------------------- # | |
public function view($id = null) | |
{ | |
if (!$id) | |
{ | |
throw new NotFoundException(__('Invalid job')); | |
} | |
$job = $this->Job->findById($id); | |
if (!$job) | |
{ | |
throw new NotFoundException(__('Invalid job')); | |
} | |
$this->set('job', $job); | |
} // End "View Job" function | |
# ----------------------------------------- # | |
# - ADD action ---------------------------- # | |
# ----------------------------------------- # | |
public function add() | |
{ | |
if ($this->request->is('post')) | |
{ | |
$this->Job->create(); | |
$this->set->('clients', $this->Job->Client->find('list')); | |
if ($this->Job->save($this->request->data)) | |
{ | |
$this->Session->setFlash(__('Your job has been saved.')); | |
return $this->redirect(array('action' => 'index')); | |
} | |
$this->Session->setFlash(__('Unable to add your job.')); | |
} | |
} // End "Add Job" function | |
# ----------------------------------------- # | |
# - EDIT action --------------------------- # | |
# ----------------------------------------- # | |
public function edit($id = null) | |
{ | |
// You silly man, you can't edit a job that | |
// does not even exist! | |
if (!$id) | |
{ | |
throw new NotFoundException(__('Invalid job')); | |
} | |
$job = $this->Job->findById($id); | |
if (!$job) | |
{ | |
throw new NotFoundException(__('Invalid job')); | |
} | |
if ($this->request->is(array('job', 'put'))) | |
{ | |
$this->Job->id = $id; | |
if ($this->Job->save($this->request->data)) | |
{ | |
$this->Session->setFlash(__('Your Job has been updated.')); | |
return $this->redirect(array('action' => 'index')); | |
} | |
$this->Session->setFlash(__('Unable to update your Job.')); | |
} | |
if (!$this->request->data) | |
{ | |
$this->request->data = $job; | |
} | |
} // End "Edit Job" function | |
# ----------------------------------------- # | |
# - DELETE action ------------------------- # | |
# ----------------------------------------- # | |
public function delete($id) | |
{ | |
if ($this->request->is('get')) | |
{ | |
throw new MethodNotAllowedException(); | |
} | |
if ($this->Job->delete($id)) | |
{ | |
$this->Session->setFlash( | |
__('The job with id: %s has been deleted.', h($id)) | |
); | |
return $this->redirect(array('action' => 'index')); | |
} | |
} // End "Delete Job" function | |
} // End class JobsController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- File: /app/View/Clients/view.ctp --> | |
<h1><?php echo $client['Client']['name']; ?></h1> | |
<ul> | |
<?php foreach($client['Job'] as $job): ?> | |
<li><?php echo $job['title']; ?></li> | |
<?php endforeach; ?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment