Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2015 00:47
Show Gist options
  • Save anonymous/832690f4402afdc80367 to your computer and use it in GitHub Desktop.
Save anonymous/832690f4402afdc80367 to your computer and use it in GitHub Desktop.
PeopleTable
____________________
<?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;
}}
___________________
identification_types_peopleTable
<?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[];
}
}
_______________________
IDentification_types_table
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment