Created
May 29, 2017 10:45
-
-
Save joshuaso91/0f24b1487cc514ded0509b943c2ed89f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Table; | |
use Cake\ORM\Query; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
/** | |
* ProductsStores Model | |
* | |
* @property \Cake\ORM\Association\BelongsTo $Stores | |
* @property \Cake\ORM\Association\BelongsTo $Products | |
* | |
* @method \App\Model\Entity\ProductsStore get($primaryKey, $options = []) | |
* @method \App\Model\Entity\ProductsStore newEntity($data = null, array $options = []) | |
* @method \App\Model\Entity\ProductsStore[] newEntities(array $data, array $options = []) | |
* @method \App\Model\Entity\ProductsStore|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \App\Model\Entity\ProductsStore patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \App\Model\Entity\ProductsStore[] patchEntities($entities, array $data, array $options = []) | |
* @method \App\Model\Entity\ProductsStore findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class ProductsStoresTable extends Table | |
{ | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->table('products_stores'); | |
$this->displayField('id'); | |
$this->primaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsTo('Stores', [ | |
'foreignKey' => 'store_id', | |
'joinType' => 'INNER' | |
]); | |
$this->belongsTo('Products', [ | |
'foreignKey' => 'product_id', | |
'joinType' => 'INNER' | |
]); | |
$this->hasMany('Orders', [ | |
'foreignKey' => 'product_store_id' | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator Validator instance. | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->integer('id') | |
->allowEmpty('id', 'create'); | |
$validator | |
->decimal('current_stock') | |
->allowEmpty('current_stock'); | |
$validator | |
->decimal('suggested_order') | |
->allowEmpty('suggested_order'); | |
$validator | |
->decimal('total_req') | |
->allowEmpty('total_req'); | |
return $validator; | |
} | |
/** | |
* 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 | |
*/ | |
public function buildRules(RulesChecker $rules) | |
{ | |
$rules->add($rules->existsIn(['store_id'], 'Stores')); | |
$rules->add($rules->existsIn(['product_id'], 'Products')); | |
return $rules; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment