* What I did*
*1.- This is my model:*
*
--
*
class Account extends AppModel {
public $name = 'Account';
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id'
),
'Accounttype' => array(
'className' => 'Accounttype',
'foreignKey' => 'accounttype_id'
)
);
public $hasMany = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'account_id',
'dependent'=> true
),
'Plot' => array(
'className' => 'Plot',
'foreignKey' => 'account_id',
'dependent'=> true
),
'Widget' => array(
'className' => 'Widget',
'foreignKey' => 'account_id',
'dependent'=> true
)
);
}
*
--
*
*
*
*2.- **This is my controller:***
*
--
*
class AccountsController extends AppController {
public $helpers = array ('Html','Form');
function index($client_id) {
$this->set('accounts', $this->Account->find('all',
array('recursive' =>1, 'conditions' => 'Account.client_id ='. $client_id)));
$this->set('client_id', $client_id);
}
public function view($id = null) {
$this->Account->id = $id;
$this->set('account', $this->Account->read());
}
public function add($client_id) {
if ($this->request->is('post')) {
if ($this->Account->save($this->request->data)) {
$this->Session->setFlash('Your account has been added.');
if($this->request->data['Account']['accounttype_id'] == 2){
$this->redirect(array('controller' => 'twitteraccounts', 'action' =>
'connect'));
}
}
}
$accounts = $this->Account->find('list');
$accounttype = $this->Account->Accounttype->find('list', array('fields'
=> array('Accounttype.id', 'Accounttype.description')));
$this->set('client_id', $client_id);
$this->set('accounts', $accounts);
$this->set('accounttype', $accounttype);
}
function edit($id = null, $client_id) {
$this->Account->id = $id;
$this->set('id', $id);
if ($this->request->is('get')) {
$this->request->data = $this->Account->read();
} else {
if ($this->Account->save($this->request->data)) {
$this->Session->setFlash('Your account has been updated.');
$this->redirect(array('action' => 'index', $client_id));
}
}
$accounts = $this->Account->find('list');
$accounttype = $this->Account->Accounttype->find('list', array('fields'
=> array('Accounttype.id', 'Accounttype.description')));
$this->set('accounts', $accounts);
$this->set('accounttype', $accounttype);
$this->set('client_id', $client_id);
}
public function delete($id, $client_id) {
$id = $this->request->params['pass'][0];
//the request must be a post request
//that's why we use postLink method on our view for deleting user
if( $this->request->is('get') ){
$this->Session->setFlash('Delete method is not allowed.');
$this->redirect(array('action' => 'index',$client_id));
//since we are using php5, we can also throw an exception like:
//throw new MethodNotAllowedException();
}else{
if( !$id ) {
$this->Session->setFlash('Invalid id for Account');
$this->redirect(array('action'=>'index',$client_id));
}else{
//delete user
if( $this->Account->delete($id, $cascade = true) ){
//set to screen
$this->Session->setFlash('Account was deleted.');
//redirect to users's list
$this->redirect(array('action'=>'index',$client_id));
}else{
//if unable to delete
$this->Session->setFlash('Unable to delete Account.');
$this->redirect(array('action' => 'index'));
}
}
}
}
}
*
--
*
*
*
*3.- **This is the view:***
*
--
*
echo $this->Html->addCrumb('Clients', '/clients');
echo $this->Html->addCrumb('Accounts', '/accounts');
echo $this->Form->create('Account');
foreach($accounts as $account): ?>
Html->link('Edit', array('action' =>
'edit', $account['Account']['id'], $client_id)); ?>
Html->link('Del', array('action' =>
'delete', $account['Account']['id'], $client_id)); ?>
Html->link('Add Event',
'/events/add/'.$account['Account'