/var/www/vhosts/ihelp.ro/httpdocs/src/Model/Table
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Model/Table/ProductsTable.php (6987B)
setTable('products');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('CounterCache', [
'Causes' => ['product_count' => [
'conditions' => ['Products.stock >' => 0, 'Products.is_active' => true]
]],
'Categories' => ['product_count' => [
'conditions' => ['Products.stock >' => 0, 'Products.is_active' => true]
]],
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'LEFT',
]);
$this->belongsTo('Causes', [
'foreignKey' => 'cause_id',
'joinType' => 'LEFT',
]);
$this->belongsToMany('Tags', [
'foreignKey' => 'product_id',
'targetForeignKey' => 'tag_id',
'joinTable' => 'products_tags',
]);
$this->hasMany('CartProducts', [
'foreignKey' => 'product_id',
]);
// $this->hasMany('ProductFavorites', [
// 'foreignKey' => 'product_id',
// ]);
$this->belongsToMany('ProductsTags', [
'foreignKey' => 'product_id',
'joinTable' => 'products_tags',
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
]);
$this->hasMany('OrderProducts', [
'foreignKey' => 'product_id',
]);
$this->belongsTo('UserAddresses', [
'foreignKey' => 'user_address_id',
]);
$this->hasMany('GalleryImages', [
'foreignKey' => 'foreign_key',
])->setConditions(['GalleryImages.model' => 'Products']);
$this->hasOne('FeaturedImage', [
'className' => 'GalleryImages',
'foreignKey' => 'foreign_id',
])
->setForeignKey([
'foreign_key',
'foreign_uuid'
])
->setBindingKey([
'id',
'uuid'
])
->setConditions(['FeaturedImage.model' => 'Products', 'FeaturedImage.is_featured' => true]);
}
/**
* Default validation rules.
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->integer('uuid');
$validator
->integer('user_address_id')
->requirePresence('user_address_id', 'create')
->notEmptyString('user_address_id', __('Câmpul este obligatoriu.'));
$validator
->integer('category_id')
->requirePresence('category_id', 'create')
->notEmptyString('category_id', __('Câmpul este obligatoriu.'));
$validator
->scalar('name')
->maxLength('name', 255)
->requirePresence('name', 'create')
->notEmptyString('name', __('Câmpul este obligatoriu.'));
$validator
->scalar('description')
->maxLength('description', 2000,__('Maxim 2000 de caractere.'))
->requirePresence('description', 'create')
->notEmptyString('description', __('Câmpul este obligatoriu.'));
$validator
->integer('cause_id')
->requirePresence('cause_id', 'create')
->notEmptyString('cause_id', __('Câmpul este obligatoriu.'));
$validator->setProvider('tools', 'App\Model\Validation\ToolsValidation');
$validator
->numeric('price')
->requirePresence('price', 'create')
->notEmptyString('price', __('Câmpul este obligatoriu.'))
->add('price', 'isPositiveNumber', [
'rule' => 'isPositiveNumber',
'provider' => 'tools',
]);
//$validator
//->numeric('price_min')
//->requirePresence('price_min', 'create')
//->notEmptyString('price_min', __('Câmpul este obligatoriu.'))
// ->add('price_min', 'isPositiveNumber', [
// 'rule' => 'isPositiveNumber',
// 'provider' => 'tools',
// ]);
// $validator
// ->add('price_min', 'skypPriceMin', [
// 'rule' => function ($value, $context) {
// // if is set price fix return true
// if ($context['data']['is_price_fixed'] == true && $value != 0) {
// return true;
// }
// if (!empty($value && $value && $value != 0)) {
// return true;
// }
// return false;
// },
// 'message' => __('Câmpul este obligatoriu.')
// ])
// ->add('price_min', 'isPositiveNumber', [
// 'rule' => function ($value, $context) {
// // if is positive number retunr true
// if (preg_match('/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/', $value)) {
// return true;
// }
// if (empty($value)) {
// return true;
// }
// return false;
// },
// 'message' => __('Valoare incorectă.')
// ]);
$validator
->numeric('stock')
->requirePresence('price', 'create')
->notEmptyString('stock', __('Câmpul este obligatoriu.'))
->add('stock', 'isPositiveNumber', [
'rule' => 'isPositiveNumber',
'provider' => 'tools',
]);
// $validator
// ->boolean('is_price_fixed')
// ->requirePresence('is_price_fixed', 'create')
// ->notEmptyString('is_price_fixed');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['user_id'], 'Users'), ['errorField' => 'user_id']);
$rules->add($rules->existsIn(['cause_id'], 'Causes'), ['errorField' => 'cause_id']);
$rules->add($rules->existsIn(['category_id'], 'Categories'), ['errorField' => 'category_id']);
return $rules;
}
}