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']);
|
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.'));
|
$this->Flash->error(__('The address could not be saved. Please, try again.'));
|
||||||
}
|
}
|
||||||
$countries = $this->Addresses->Countries->find('list')->all();
|
$countries = $this->Addresses->Countries->find('list')->all();
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,12 @@ use Cake\ORM\Entity;
|
||||||
* @property string|null $phone_number
|
* @property string|null $phone_number
|
||||||
* @property string|null $email
|
* @property string|null $email
|
||||||
* @property string|null $notes
|
* @property string|null $notes
|
||||||
|
* @property string|null $foreign_key
|
||||||
|
* @property string|null $model
|
||||||
*
|
*
|
||||||
* @property \CakeAddresses\Model\Entity\City $city
|
* @property City $city
|
||||||
* @property \CakeAddresses\Model\Entity\State $state
|
* @property State $state
|
||||||
* @property \CakeAddresses\Model\Entity\Country $country
|
* @property Country $country
|
||||||
*/
|
*/
|
||||||
class Address extends Entity
|
class Address extends Entity
|
||||||
{
|
{
|
||||||
|
|
@ -51,5 +53,7 @@ class Address extends Entity
|
||||||
'phone_number' => true,
|
'phone_number' => true,
|
||||||
'email' => true,
|
'email' => true,
|
||||||
'notes' => true,
|
'notes' => true,
|
||||||
|
'foreign_key' => true,
|
||||||
|
'model' => true,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,34 +5,39 @@ namespace CakeAddresses\Model\Table;
|
||||||
|
|
||||||
use ArrayObject;
|
use ArrayObject;
|
||||||
use Cake\Datasource\EntityInterface;
|
use Cake\Datasource\EntityInterface;
|
||||||
|
use Cake\Datasource\ResultSetInterface;
|
||||||
use Cake\Event\EventInterface;
|
use Cake\Event\EventInterface;
|
||||||
use Cake\Log\Log;
|
use Cake\Log\Log;
|
||||||
|
use Cake\ORM\Association\BelongsTo;
|
||||||
use Cake\ORM\Query\SelectQuery;
|
use Cake\ORM\Query\SelectQuery;
|
||||||
use Cake\ORM\RulesChecker;
|
use Cake\ORM\RulesChecker;
|
||||||
use Cake\ORM\Table;
|
use Cake\ORM\Table;
|
||||||
use Cake\Validation\Validator;
|
use Cake\Validation\Validator;
|
||||||
|
use CakeAddresses\Model\Entity\Address;
|
||||||
use CakeAddresses\Model\Entity\State;
|
use CakeAddresses\Model\Entity\State;
|
||||||
|
use Closure;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Addresses Model
|
* Addresses Model
|
||||||
*
|
*
|
||||||
* @property \CakeAddresses\Model\Table\CitiesTable&\Cake\ORM\Association\BelongsTo $Cities
|
* @property CitiesTable&BelongsTo $Cities
|
||||||
* @property \CakeAddresses\Model\Table\StatesTable&\Cake\ORM\Association\BelongsTo $States
|
* @property StatesTable&BelongsTo $States
|
||||||
* @property \CakeAddresses\Model\Table\CountriesTable&\Cake\ORM\Association\BelongsTo $Countries
|
* @property CountriesTable&BelongsTo $Countries
|
||||||
*
|
*
|
||||||
* @method \CakeAddresses\Model\Entity\Address newEmptyEntity()
|
* @method Address newEmptyEntity()
|
||||||
* @method \CakeAddresses\Model\Entity\Address newEntity(array $data, array $options = [])
|
* @method Address newEntity(array $data, array $options = [])
|
||||||
* @method array<\CakeAddresses\Model\Entity\Address> newEntities(array $data, array $options = [])
|
* @method array<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 Address get(mixed $primaryKey, array|string $finder = 'all', 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 Address findOrCreate($search, ?callable $callback = null, array $options = [])
|
||||||
* @method \CakeAddresses\Model\Entity\Address patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
|
* @method Address patchEntity(EntityInterface $entity, array $data, array $options = [])
|
||||||
* @method array<\CakeAddresses\Model\Entity\Address> patchEntities(iterable $entities, array $data, array $options = [])
|
* @method array<Address> patchEntities(iterable $entities, array $data, array $options = [])
|
||||||
* @method \CakeAddresses\Model\Entity\Address|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
|
* @method Address|false save(EntityInterface $entity, array $options = [])
|
||||||
* @method \CakeAddresses\Model\Entity\Address saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
|
* @method Address saveOrFail(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<Address>|ResultSetInterface<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<Address>|ResultSetInterface<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<Address>|ResultSetInterface<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 iterable<Address>|ResultSetInterface<Address> deleteManyOrFail(iterable $entities, array $options = [])
|
||||||
*/
|
*/
|
||||||
class AddressesTable extends Table
|
class AddressesTable extends Table
|
||||||
{
|
{
|
||||||
|
|
@ -70,8 +75,8 @@ class AddressesTable extends Table
|
||||||
/**
|
/**
|
||||||
* Default validation rules.
|
* Default validation rules.
|
||||||
*
|
*
|
||||||
* @param \Cake\Validation\Validator $validator Validator instance.
|
* @param Validator $validator Validator instance.
|
||||||
* @return \Cake\Validation\Validator
|
* @return Validator
|
||||||
*/
|
*/
|
||||||
public function validationDefault(Validator $validator): Validator
|
public function validationDefault(Validator $validator): Validator
|
||||||
{
|
{
|
||||||
|
|
@ -145,6 +150,16 @@ class AddressesTable extends Table
|
||||||
->scalar('notes')
|
->scalar('notes')
|
||||||
->allowEmptyString('notes');
|
->allowEmptyString('notes');
|
||||||
|
|
||||||
|
$validator
|
||||||
|
->scalar('foreign_key')
|
||||||
|
->maxLength('foreign_key', 45)
|
||||||
|
->allowEmptyString('foreign_key');
|
||||||
|
|
||||||
|
$validator
|
||||||
|
->scalar('model')
|
||||||
|
->maxLength('model', 255)
|
||||||
|
->allowEmptyString('model');
|
||||||
|
|
||||||
return $validator;
|
return $validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,8 +167,8 @@ class AddressesTable extends Table
|
||||||
* Returns a rules checker object that will be used for validating
|
* Returns a rules checker object that will be used for validating
|
||||||
* application integrity.
|
* application integrity.
|
||||||
*
|
*
|
||||||
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
|
* @param RulesChecker $rules The rules object to be modified.
|
||||||
* @return \Cake\ORM\RulesChecker
|
* @return RulesChecker
|
||||||
*/
|
*/
|
||||||
public function buildRules(RulesChecker $rules): RulesChecker
|
public function buildRules(RulesChecker $rules): RulesChecker
|
||||||
{
|
{
|
||||||
|
|
@ -175,11 +190,11 @@ class AddressesTable extends Table
|
||||||
{
|
{
|
||||||
if ($data['state_id'] && !isset($data['state'])) {
|
if ($data['state_id'] && !isset($data['state'])) {
|
||||||
$state = $this->States->find()->where(['id' => $data['state_id']])->first();
|
$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'])) {
|
if ($data['country_id'] && !isset($data['country'])) {
|
||||||
$country = $this->Countries->find()->where(['id' => $data['country_id']])->first();
|
$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