Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save commercial-hippie/e7f36c530960ad8cb666 to your computer and use it in GitHub Desktop.
Save commercial-hippie/e7f36c530960ad8cb666 to your computer and use it in GitHub Desktop.
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li><?= $this->Html->link(__('Voltar'), ['controller' => 'Opcoes', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('Logout'), ['controller' => 'Users', 'action' => 'logout']) ?></li>
</ul>
</div>
<div class="people form large-10 medium-9 columns">
<?= $this->Form->create($person) ?>
<fieldset>
<legend><?= __('Adicionar Pessoas') ?></legend>
<?php
echo $this->Form->input('name');
$gender = ['' => __('Prefiro não informar'), 'M' => __('Masculino'), 'F' => __('Feminino')];
echo $this->Form->label(__('Sexo'));
echo $this->Form->select('gender', ['options' => $gender]);
echo $this->Form->input('birthday', [
'minYear' => 1900,
'maxYear' => date('Y'),
'label' => __('Data de Nascimento')
]);
// debug($identificationTypes);
$i = 0;
foreach($identificationTypes as $it)
{
echo $this->Form->input("identification_types.$i._joinData.value", ['label'=> $it->value]);
echo $this->Form->input("identification_types.$i.id", ['label'=> $it->value]);
$i++;
}
// echo $this->Form->input('identificationTypes._ids', [
// 'options' => $identificationTypes,
// 'multiple' => 'checkbox',
// 'label' => 'identificacao',
// 'templates' => [
// 'formGroup' => "{{label}}<table><tr><th>Info</th><th>Valor</th></tr>{{input}}</table>",
// 'nestingLabel' => "<tr><td>{{input}}<label{{attrs}}>{{text}}</label></td><td><input id='???' class='???' name='???' type='text' value='' placeholder='&#37;' style='width:50%%;' /></td></tr>"
// ]
// ]);
// debug($person);
// echo $this->Form->input("identification_types.0._joinData.value");
// foreach($identT as $tipoId) {
// echo $this->Form->input("identification_types.0._joinData.$tipoId->value");
// }
// foreach($identT as $tipoId) {
// echo $this->Form->input($tipoId->id, ['label'=> $tipoId->value]);
// }
?>
</fieldset>
<?= $this->Form->button(__('Adicionar')) ?>
<?= $this->Form->end() ?>
</div>
<?php
namespace App\Model\Table;
use App\Model\Entity\IdentificationTypesPerson;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use SoftDelete\Model\Table\SoftDeleteTrait;
use Cake\Event\Event;
use ArrayObject;
/**
* IdentificationTypesPeople Model
*
* @property \Cake\ORM\Association\BelongsTo $People
* @property \Cake\ORM\Association\BelongsTo $IdentificationTypes
*/
class IdentificationTypesPeopleTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('identification_types_people');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('People', [
'foreignKey' => 'person_id',
'joinType' => 'INNER'
]);
$this->belongsTo('IdentificationTypes', [
'foreignKey' => 'identification_type_id',
'joinType' => 'INNER'
]);
}
/**
* 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');
$validator
->requirePresence('value', 'create')
->notEmpty('value');
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->existsIn(['person_id'], 'People'));
$rules->add($rules->existsIn(['identification_type_id'], 'IdentificationTypes'));
return $rules;
}
public function beforeMarshal(Event $event, ArrayObject $data)
{
// print_r($data);
// exit(0);
// $this->valor = $data[];
}
}
<?php
namespace App\Model\Table;
use App\Model\Entity\IdentificationType;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* IdentificationTypes Model
*
* @property \Cake\ORM\Association\BelongsTo $Countries
* @property \Cake\ORM\Association\BelongsToMany $People
*/
class IdentificationTypesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('identification_types');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Countries', [
'foreignKey' => 'country_id',
'joinType' => 'INNER'
]);
$this->belongsToMany('People', [
'foreignKey' => 'identification_type_id',
'targetForeignKey' => 'person_id',
'joinTable' => 'identification_types_people',
'through' => 'IdentificationTypesPeople'
]);
/*
$this->belongsToMany('People', [
'through' => 'IdentificationTypesPeople'
]);
*/
}
/**
* 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');
$validator
->requirePresence('value', 'create')
->notEmpty('value');
$validator
->requirePresence('mandatory', 'create')
->notEmpty('mandatory');
$validator
->allowEmpty('mask');
$validator
->allowEmpty('validation');
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->existsIn(['country_id'], 'Countries'));
return $rules;
}
}
<?php
public function add()
{
$session = $this->request->session();
$this->loadModel('People');
$this->loadModel('Units');
$person = $this->People->newEntity();
$unit_id = $this->request->query['unit'];
$unit = $this->Units->get($unit_id);
/*
$identT = $this->People
->IdentificationTypes
->find()
->where(['country_id' => $session->read('idCountry') ]);
*/
$identT = $this->People
->IdentificationTypes
->find()
->where(['country_id' => $session->read('idCountry') ]);
//TODO: verificar se o usuario mora mesmo nesta unidade
//TODO: trocar de get por post o envio do ID da unidade
//http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm
if ($this->request->is('post')) {
$person = $this->People->patchEntity($person, $this->request->data, ['associated' => 'IdentificationTypesPeople']);
// debug($this->request->data); die();
// $person = $this->People->patchEntity($person, $this->request->data, ['associated' => 'IdentificationTypes']);
// debug ($person); die();
//if ($this->People->save($person, ['associated' => 'Units'])) {
if ($this->People->save($person)) {
// debug($person['identification_types']); die();
// $it = $this->People->IdentificationTypes->find()->first();
// $it->_joinData = $this->People->IdentificationTypesPeople->newEntity();
// $it->_joinData->value = '789';
// $this->People->IdentificationTypes->link($person, [$it]);
// Salva Pessoa a Unidate
$this->People->Units->link($person, [$unit]);
// debug($person);
$this->Flash->success(__('The person has been saved.'));
return $this->redirect(['action' => 'showPeople']);
} else {
debug($person->errors());
debug($person);
// die();
$this->Flash->error(__('The person could not be saved. Please, try again.'));
}
}
$this->set('person', $person);
// $this->set('identT', $identT);
$this->set('identificationTypes', $identT);
}
<?php
namespace App\Model\Table;
use App\Model\Entity\Person;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use SoftDelete\Model\Table\SoftDeleteTrait;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\Event\Event;
use ArrayObject;
/**
* People Model
*
* @property \Cake\ORM\Association\BelongsTo $Clients
* @property \Cake\ORM\Association\BelongsTo $Images
* @property \Cake\ORM\Association\HasMany $Automobiles
* @property \Cake\ORM\Association\HasMany $Contacts
* @property \Cake\ORM\Association\HasMany $Users
* @property \Cake\ORM\Association\BelongsToMany $Bookings
* @property \Cake\ORM\Association\BelongsToMany $IdentificationTypes
* @property \Cake\ORM\Association\BelongsToMany $Units
*/
class PeopleTable extends Table
{
use SoftDeleteTrait;
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('people');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('Clients', [
'foreignKey' => 'client_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Images', [
'foreignKey' => 'image_id'
]);
$this->hasMany('Automobiles', [
'foreignKey' => 'person_id'
]);
$this->hasMany('Contacts', [
'foreignKey' => 'person_id'
]);
$this->hasMany('Users', [
'foreignKey' => 'person_id'
]);
$this->belongsToMany('Bookings', [
'foreignKey' => 'person_id',
'targetForeignKey' => 'booking_id',
'joinTable' => 'bookings_people'
]);
$this->belongsToMany('IdentificationTypes', [
'foreignKey' => 'person_id',
'targetForeignKey' => 'identification_type_id',
'joinTable' => 'identification_types_people',
'through' => 'IdentificationTypesPeople'
]);
/*
$this->belongsToMany('IdentificationTypes', [
'through' => 'IdentificationTypesPeople'
]);
*/
$this->belongsToMany('Units', [
'foreignKey' => 'person_id',
'targetForeignKey' => 'unit_id',
'joinTable' => 'people_units'
]);
}
/**
* 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');
$validator
->requirePresence('name', 'create')
->notEmpty('name');
$validator
->allowEmpty('gender');
$validator
->add('birthday', 'valid', ['rule' => 'date'])
->allowEmpty('birthday');
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->existsIn(['client_id'], 'Clients'));
$rules->add($rules->existsIn(['image_id'], 'Images'));
return $rules;
}
public function beforeMarshal(Event $event, ArrayObject $data)
{
$clients = TableRegistry::get('Clients');
$query = $clients->find()->where(['url' => str_replace('/','', Router::url('/'))]);
$client = $query->first();
$data['client_id'] = $client->id;
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment