2025-11-18 00:43:34 -08:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace CakeAddresses\Model\Table;
|
|
|
|
|
|
|
|
|
|
use ArrayObject;
|
2026-01-23 19:25:11 -08:00
|
|
|
use Cake\Datasource\EntityInterface;
|
|
|
|
|
use Cake\Datasource\ResultSetInterface;
|
2025-11-18 00:43:34 -08:00
|
|
|
use Cake\Event\EventInterface;
|
2026-01-23 19:25:11 -08:00
|
|
|
use Cake\ORM\Association\BelongsTo;
|
2025-11-18 00:43:34 -08:00
|
|
|
use Cake\ORM\RulesChecker;
|
|
|
|
|
use Cake\ORM\Table;
|
|
|
|
|
use Cake\Validation\Validator;
|
2026-01-23 19:25:11 -08:00
|
|
|
use CakeAddresses\Model\Entity\Address;
|
2025-11-18 00:43:34 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Addresses Model
|
|
|
|
|
*
|
2025-11-21 21:59:31 -08:00
|
|
|
* @property CitiesTable&BelongsTo $Cities
|
|
|
|
|
* @property StatesTable&BelongsTo $States
|
|
|
|
|
* @property CountriesTable&BelongsTo $Countries
|
2025-11-18 00:43:34 -08:00
|
|
|
*
|
2026-01-23 18:36:15 -08:00
|
|
|
* @method \CakeAddresses\Model\Entity\Address newEmptyEntity()
|
|
|
|
|
* @method \CakeAddresses\Model\Entity\Address newEntity()
|
2025-11-21 21:59:31 -08:00
|
|
|
* @method array<Address> newEntities(array $data, array $options = [])
|
2026-01-23 19:25:11 -08:00
|
|
|
* @method \CakeAddresses\Model\Entity\Address get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
|
2026-01-23 18:36:15 -08:00
|
|
|
* @method \CakeAddresses\Model\Entity\Address findOrCreate()
|
2026-01-23 19:25:11 -08:00
|
|
|
* @method \CakeAddresses\Model\Entity\Address patchEntity(Address $address, array $data, array $options = [])
|
2025-11-21 21:59:31 -08:00
|
|
|
* @method array<Address> patchEntities(iterable $entities, array $data, array $options = [])
|
|
|
|
|
* @method Address|false save(EntityInterface $entity, array $options = [])
|
2026-01-23 18:36:15 -08:00
|
|
|
* @method \CakeAddresses\Model\Entity\Address saveOrFail()
|
2025-11-21 21:59:31 -08:00
|
|
|
* @method iterable<Address>|ResultSetInterface<Address>|false saveMany(iterable $entities, array $options = [])
|
|
|
|
|
* @method iterable<Address>|ResultSetInterface<Address> saveManyOrFail(iterable $entities, array $options = [])
|
|
|
|
|
* @method iterable<Address>|ResultSetInterface<Address>|false deleteMany(iterable $entities, array $options = [])
|
|
|
|
|
* @method iterable<Address>|ResultSetInterface<Address> deleteManyOrFail(iterable $entities, array $options = [])
|
2025-11-18 00:43:34 -08:00
|
|
|
*/
|
2026-01-23 18:36:15 -08:00
|
|
|
class AddressesTable extends Table {
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-18 00:43:34 -08:00
|
|
|
* Initialize method
|
|
|
|
|
*
|
|
|
|
|
* @param array<string, mixed> $config The configuration for the Table.
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-01-23 18:36:15 -08:00
|
|
|
public function initialize(array $config): void {
|
|
|
|
|
parent::initialize($config);
|
|
|
|
|
|
|
|
|
|
$this->setTable('addresses');
|
|
|
|
|
$this->setDisplayField('address_line1');
|
|
|
|
|
$this->setPrimaryKey('id');
|
|
|
|
|
|
|
|
|
|
$this->belongsTo('Cities', [
|
|
|
|
|
'foreignKey' => 'city_id',
|
|
|
|
|
'className' => 'CakeAddresses.Cities',
|
|
|
|
|
'propertyName' => 'city_entity',
|
|
|
|
|
]);
|
|
|
|
|
$this->belongsTo('States', [
|
|
|
|
|
'foreignKey' => 'state_id',
|
|
|
|
|
'className' => 'CakeAddresses.States',
|
|
|
|
|
'propertyName' => 'state_entity',
|
|
|
|
|
]);
|
|
|
|
|
$this->belongsTo('Countries', [
|
|
|
|
|
'foreignKey' => 'country_id',
|
|
|
|
|
'className' => 'CakeAddresses.Countries',
|
|
|
|
|
'propertyName' => 'country_entity',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-18 00:43:34 -08:00
|
|
|
* Default validation rules.
|
|
|
|
|
*
|
2026-01-23 18:36:15 -08:00
|
|
|
* @param \Cake\Validation\Validator $validator Validator instance.
|
|
|
|
|
* @return \Cake\Validation\Validator
|
2025-11-18 00:43:34 -08:00
|
|
|
*/
|
2026-01-23 18:36:15 -08:00
|
|
|
public function validationDefault(Validator $validator): Validator {
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('address_name')
|
|
|
|
|
->maxLength('address_name', 255)
|
|
|
|
|
->allowEmptyString('address_name');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('contact_name')
|
|
|
|
|
->maxLength('contact_name', 255)
|
|
|
|
|
->allowEmptyString('contact_name');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('address_line1')
|
|
|
|
|
->maxLength('address_line1', 255)
|
|
|
|
|
->requirePresence('address_line1', 'create')
|
|
|
|
|
->notEmptyString('address_line1');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('address_line2')
|
|
|
|
|
->maxLength('address_line2', 255)
|
|
|
|
|
->allowEmptyString('address_line2');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('city')
|
|
|
|
|
->maxLength('city', 255)
|
|
|
|
|
->requirePresence('city', 'create')
|
|
|
|
|
->notEmptyString('city');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->integer('city_id')
|
|
|
|
|
->allowEmptyString('city_id');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('state')
|
|
|
|
|
->maxLength('state', 255)
|
|
|
|
|
->requirePresence('state', 'create')
|
|
|
|
|
->notEmptyString('state');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->integer('state_id')
|
|
|
|
|
->allowEmptyString('state_id');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('postal_code')
|
|
|
|
|
->maxLength('postal_code', 25)
|
|
|
|
|
->requirePresence('postal_code', 'create')
|
|
|
|
|
->notEmptyString('postal_code');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('country')
|
|
|
|
|
->maxLength('country', 255)
|
|
|
|
|
->requirePresence('country', 'create')
|
|
|
|
|
->notEmptyString('country');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->integer('country_id')
|
|
|
|
|
->allowEmptyString('country_id');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('phone_number')
|
|
|
|
|
->maxLength('phone_number', 25)
|
|
|
|
|
->allowEmptyString('phone_number');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->email('email')
|
|
|
|
|
->allowEmptyString('email');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('notes')
|
|
|
|
|
->allowEmptyString('notes');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('foreign_key')
|
|
|
|
|
->maxLength('foreign_key', 45)
|
|
|
|
|
->allowEmptyString('foreign_key');
|
|
|
|
|
|
|
|
|
|
$validator
|
|
|
|
|
->scalar('model')
|
|
|
|
|
->maxLength('model', 255)
|
|
|
|
|
->allowEmptyString('model');
|
|
|
|
|
|
|
|
|
|
return $validator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-18 00:43:34 -08:00
|
|
|
* Returns a rules checker object that will be used for validating
|
|
|
|
|
* application integrity.
|
|
|
|
|
*
|
2026-01-23 18:36:15 -08:00
|
|
|
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
|
|
|
|
|
* @return \Cake\ORM\RulesChecker
|
2025-11-18 00:43:34 -08:00
|
|
|
*/
|
2026-01-23 18:36:15 -08:00
|
|
|
public function buildRules(RulesChecker $rules): RulesChecker {
|
|
|
|
|
$rules->add($rules->existsIn('city_id', 'Cities'), ['errorField' => 'city_id']);
|
|
|
|
|
$rules->add($rules->existsIn('state_id', 'States'), ['errorField' => 'state_id']);
|
|
|
|
|
$rules->add($rules->existsIn('country_id', 'Countries'), ['errorField' => 'country_id']);
|
|
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Cake\Event\EventInterface $event
|
|
|
|
|
* @param \ArrayObject $data
|
|
|
|
|
* @param \ArrayObject $options
|
2025-11-18 00:43:34 -08:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-01-23 18:36:15 -08:00
|
|
|
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void {
|
|
|
|
|
if ($data['state_id'] && !isset($data['state'])) {
|
|
|
|
|
$state = $this->States->find()->where(['id' => $data['state_id']])->first();
|
|
|
|
|
$data['state'] = $state->name ?? null;
|
|
|
|
|
}
|
|
|
|
|
if ($data['country_id'] && !isset($data['country'])) {
|
|
|
|
|
$country = $this->Countries->find()->where(['id' => $data['country_id']])->first();
|
|
|
|
|
$data['country'] = $country->name ?? null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-18 00:43:34 -08:00
|
|
|
}
|