Skip to content

Instantly share code, notes, and snippets.

@converge
Created April 7, 2015 18:25
Show Gist options
  • Save converge/0b56c2d995114df8a673 to your computer and use it in GitHub Desktop.
Save converge/0b56c2d995114df8a673 to your computer and use it in GitHub Desktop.
#
# ClientsController
#
public function add()
{
$client = $this->Clients->newEntity();
if ($this->request->is('post')) {
$this->request->data['user'] = [ 'username' => 'teste', 'password' => 'teste' ];
debug($this->request->data);
$client = $this->Clients->newEntity($this->request->data, [
'associated' => [ 'Users' ]
]);
if ($this->Clients->save($client)) {
$this->Flash->success('The client has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The client could not be saved. Please, try again.');
}
}
$regions = $this->getRegions();
$this->set(compact('client', 'regions'));
$this->set('_serialize', ['client']);
}
#
# ClientsTable
#
<?php
namespace App\Model\Table;
use App\Model\Entity\Client;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Clients Model
*/
class ClientsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('clients');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasOne('Users', [
'foreignKey' => 'users_id',
'dependent' => true
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->requirePresence('cnpj', 'create')
->notEmpty('cnpj')
->requirePresence('razao_social', 'create')
->notEmpty('razao_social')
->add('region', 'valid', ['rule' => 'numeric'])
->requirePresence('region', 'create')
->notEmpty('region')
->add('is_salesman', 'valid', ['rule' => 'numeric'])
->allowEmpty('is_salesman')
->allowEmpty('salesman')
->allowEmpty('contact_name')
->add('email', 'valid', ['rule' => 'email'])
->allowEmpty('email')
->allowEmpty('phone_number');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
$rules->add($rules->existsIn(['users_id'], 'Users'));
return $rules;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment