Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created December 26, 2011 19:06
Show Gist options
  • Save patrickmaciel/1521941 to your computer and use it in GitHub Desktop.
Save patrickmaciel/1521941 to your computer and use it in GitHub Desktop.
CakePHP 1.3 | Selecionar (list) todos os Gerentes do Sistema (Users -> Groups (id = 4))
<?php
class User extends AppModel {
var $name = 'User';
var $displayField = 'name';
var $actsAs = array('Containable');
var $belongsTo = array(
'UserCreate' => array(
'className' => 'User',
'foreignKey' => 'who_create',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Gerente' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $hasMany = array(
'PollsPollAnswer' => array(
'className' => 'PollsPollAnswer',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
var $hasAndBelongsToMany = array(
'Group' => array(
'className' => 'Group',
'joinTable' => 'groups_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
public function beforeSave() {
if(isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
}
?>
// ADMIN_ADD
function admin_add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
$groups = $this->User->Group->find('list', array(
'conditions' => array('and' => array(
'Group.id !=' => '10',
'Group.id !=' => '21'
)
), 'order' => array('Group.name'=>'ASC')
));
$gerentes = $this->User->Gerente->find('list', array(
'contain' => array(
'Grupo' => array(
'conditions' => array(
'Grupo.id' => 4
)
)
),
'conditions' => array(
'Gerente.active' => true
),
'order' => array(
'Gerente.name' => 'ASC'
)
));
$this->set(compact('groups', 'gerentes'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment