foriegn key model/entity + remove logging on add invalid
This commit is contained in:
parent
3360ef8f7b
commit
4bdb411c38
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Address> 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<Address> patchEntities(iterable $entities, array $data, array $options = [])
|
||||
* @method Address|false save(EntityInterface $entity, array $options = [])
|
||||
* @method Address saveOrFail(EntityInterface $entity, array $options = [])
|
||||
* @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 = [])
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue