Skip to content

Instantly share code, notes, and snippets.

@swthate
Created April 29, 2014 20:00
Show Gist options
  • Save swthate/f6a053697ac854b1bb4a to your computer and use it in GitHub Desktop.
Save swthate/f6a053697ac854b1bb4a to your computer and use it in GitHub Desktop.
Contain Contact
<?php
// app/Model/Client.php
App::uses('AppModel', 'Model');
/**
* Client Model
*
* @property Contact $Contact
* @property Job $Job
*/
class Client extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => 'client_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Job' => array(
'className' => 'Job',
'foreignKey' => 'client_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
<?php
// app/Model/Contact.php
App::uses('AppModel', 'Model');
/**
* Contact Model
*
* @property Client $Client
* @property Job $Job
*/
class Contact extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'phone' => array(
'phone' => array(
'rule' => array('phone'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'position' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
<?php
// app/Controller/DashboardsController.php
App::uses('AppController', 'Controller');
class DashboardsController extends AppController
{
public function index() {
// Recent Jobs ------------------------------------ //
$this->loadModel('Job');
$totalJobs = $this->Job->find('count');
$this->set('totalJobs', $totalJobs);
$this->set('job', $this->Job->find('all', array(
'contain' => 'Client',
'limit' => 4,
'order' => array('Job.id' => 'desc'))));
// Top Clients ------------------------------------ //
$this->loadModel('Client');
$totalClients = $this->Client->find('count');
$this->set('totalClients', $totalClients);
$this->set('client', $this->Client->find('all', array(
'contain' => 'Contact',
'limit' => 4,
'order' => array('job_count' => 'desc'))));
// Current User ----------------------------------- //
$this->loadModel('User');
$this->set('authUser', $this->Auth->user());
}
}
<?php
### debug($client['Contact']);
array(
(int) 0 => array(
'id' => '3',
'client_id' => '2',
'name' => 'Markus Toivonen',
'email' => 'markus@ensiferum.fi',
'phone' => '5072355104',
'position' => 'Vocals'
)
);
### debug($client['Contact']['name']);
null
<!-- app/View/Dashboards/index.ctp -->
<?php foreach ($client as $client): ?>
<tr>
<td><?php echo $client['Client']['id'];?> </td>
<td>
<?php
echo $this->Html->link(
$client['Client']['name'],
array(
'controller' => 'clients',
'action' => 'view',
$client['Client']['id']
)
);
?>
</td>
<td><?php echo $client['Contact']['name']; ?></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 $client['Client']['job_count']; ?></td>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment