From 4bdb411c387b09186ff0027b325de8bc40294ab0 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Fri, 21 Nov 2025 21:59:31 -0800 Subject: [PATCH] foriegn key model/entity + remove logging on add invalid --- src/Controller/AddressesController.php | 2 - src/Model/Entity/Address.php | 10 +++-- src/Model/Table/AddressesTable.php | 59 ++++++++++++++++---------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/Controller/AddressesController.php b/src/Controller/AddressesController.php index 35d9eb0..2e8921f 100644 --- a/src/Controller/AddressesController.php +++ b/src/Controller/AddressesController.php @@ -56,8 +56,6 @@ class AddressesController extends AppController return $this->redirect(['action' => 'index']); } - Log::alert(print_r('$address->getErrors() from addresses add', true)); - Log::alert(print_r($address->getErrors(), true)); $this->Flash->error(__('The address could not be saved. Please, try again.')); } $countries = $this->Addresses->Countries->find('list')->all(); diff --git a/src/Model/Entity/Address.php b/src/Model/Entity/Address.php index 5a2933c..763898a 100644 --- a/src/Model/Entity/Address.php +++ b/src/Model/Entity/Address.php @@ -20,10 +20,12 @@ use Cake\ORM\Entity; * @property string|null $phone_number * @property string|null $email * @property string|null $notes + * @property string|null $foreign_key + * @property string|null $model * - * @property \CakeAddresses\Model\Entity\City $city - * @property \CakeAddresses\Model\Entity\State $state - * @property \CakeAddresses\Model\Entity\Country $country + * @property City $city + * @property State $state + * @property Country $country */ class Address extends Entity { @@ -51,5 +53,7 @@ class Address extends Entity 'phone_number' => true, 'email' => true, 'notes' => true, + 'foreign_key' => true, + 'model' => true, ]; } diff --git a/src/Model/Table/AddressesTable.php b/src/Model/Table/AddressesTable.php index a0f7cf4..d03a47a 100644 --- a/src/Model/Table/AddressesTable.php +++ b/src/Model/Table/AddressesTable.php @@ -5,34 +5,39 @@ namespace CakeAddresses\Model\Table; use ArrayObject; use Cake\Datasource\EntityInterface; +use Cake\Datasource\ResultSetInterface; use Cake\Event\EventInterface; use Cake\Log\Log; +use Cake\ORM\Association\BelongsTo; use Cake\ORM\Query\SelectQuery; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; +use CakeAddresses\Model\Entity\Address; use CakeAddresses\Model\Entity\State; +use Closure; +use Psr\SimpleCache\CacheInterface; /** * Addresses Model * - * @property \CakeAddresses\Model\Table\CitiesTable&\Cake\ORM\Association\BelongsTo $Cities - * @property \CakeAddresses\Model\Table\StatesTable&\Cake\ORM\Association\BelongsTo $States - * @property \CakeAddresses\Model\Table\CountriesTable&\Cake\ORM\Association\BelongsTo $Countries + * @property CitiesTable&BelongsTo $Cities + * @property StatesTable&BelongsTo $States + * @property CountriesTable&BelongsTo $Countries * - * @method \CakeAddresses\Model\Entity\Address newEmptyEntity() - * @method \CakeAddresses\Model\Entity\Address newEntity(array $data, array $options = []) - * @method array<\CakeAddresses\Model\Entity\Address> newEntities(array $data, array $options = []) - * @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) - * @method \CakeAddresses\Model\Entity\Address findOrCreate($search, ?callable $callback = null, array $options = []) - * @method \CakeAddresses\Model\Entity\Address patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) - * @method array<\CakeAddresses\Model\Entity\Address> patchEntities(iterable $entities, array $data, array $options = []) - * @method \CakeAddresses\Model\Entity\Address|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method \CakeAddresses\Model\Entity\Address saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method iterable<\CakeAddresses\Model\Entity\Address>|\Cake\Datasource\ResultSetInterface<\CakeAddresses\Model\Entity\Address>|false saveMany(iterable $entities, array $options = []) - * @method iterable<\CakeAddresses\Model\Entity\Address>|\Cake\Datasource\ResultSetInterface<\CakeAddresses\Model\Entity\Address> saveManyOrFail(iterable $entities, array $options = []) - * @method iterable<\CakeAddresses\Model\Entity\Address>|\Cake\Datasource\ResultSetInterface<\CakeAddresses\Model\Entity\Address>|false deleteMany(iterable $entities, array $options = []) - * @method iterable<\CakeAddresses\Model\Entity\Address>|\Cake\Datasource\ResultSetInterface<\CakeAddresses\Model\Entity\Address> deleteManyOrFail(iterable $entities, array $options = []) + * @method Address newEmptyEntity() + * @method Address newEntity(array $data, array $options = []) + * @method array
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 { @@ -70,8 +75,8 @@ class AddressesTable extends Table /** * Default validation rules. * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator + * @param Validator $validator Validator instance. + * @return Validator */ public function validationDefault(Validator $validator): Validator { @@ -145,6 +150,16 @@ class AddressesTable extends Table ->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; } @@ -152,8 +167,8 @@ class AddressesTable extends Table * 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 + * @param RulesChecker $rules The rules object to be modified. + * @return RulesChecker */ public function buildRules(RulesChecker $rules): RulesChecker { @@ -175,11 +190,11 @@ class AddressesTable extends Table { if ($data['state_id'] && !isset($data['state'])) { $state = $this->States->find()->where(['id' => $data['state_id']])->first(); - $data['state'] = $state ? $state->name : null; + $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 ? $country->name : null; + $data['country'] = $country->name ?? null; } } }