newEntities(array $data, array $options = [])
* @method Address get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
* @method Address findOrCreate($search, ?callable $callback = null, array $options = [])
* @method Address patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array
patchEntities(iterable $entities, array $data, array $options = [])
* @method Address|false save(EntityInterface $entity, array $options = [])
* @method Address saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable|ResultSetInterface|false saveMany(iterable $entities, array $options = [])
* @method iterable|ResultSetInterface saveManyOrFail(iterable $entities, array $options = [])
* @method iterable|ResultSetInterface|false deleteMany(iterable $entities, array $options = [])
* @method iterable|ResultSetInterface deleteManyOrFail(iterable $entities, array $options = [])
*/
class AddressesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
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',
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
*/
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;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param RulesChecker $rules The rules object to be modified.
* @return RulesChecker
*/
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 EventInterface $event
* @param ArrayObject $data
* @param ArrayObject $options
*
* @return void
*/
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;
}
}
}