Created
May 29, 2017 10:41
-
-
Save joshuaso91/f219c6ce90ac9abb9ea4a4d81bed3d7a 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; | |
/** | |
* Orders Model | |
* | |
* @property \Cake\ORM\Association\BelongsTo $Stores | |
* @property \Cake\ORM\Association\BelongsTo $Users | |
* @property \Cake\ORM\Association\BelongsTo $Products | |
* @property \Cake\ORM\Association\HasMany $Invoices | |
* | |
* @method \App\Model\Entity\Order get($primaryKey, $options = []) | |
* @method \App\Model\Entity\Order newEntity($data = null, array $options = []) | |
* @method \App\Model\Entity\Order[] newEntities(array $data, array $options = []) | |
* @method \App\Model\Entity\Order|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \App\Model\Entity\Order patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \App\Model\Entity\Order[] patchEntities($entities, array $data, array $options = []) | |
* @method \App\Model\Entity\Order findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class OrdersTable extends Table | |
{ | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->table('orders'); | |
$this->displayField('id'); | |
$this->primaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsTo('Stores', [ | |
'foreignKey' => 'store_id', | |
'joinType' => 'INNER' | |
]); | |
$this->belongsTo('Users', [ | |
'foreignKey' => 'user_id', | |
'joinType' => 'INNER' | |
]); | |
$this->belongsTo('Products', [ | |
'foreignKey' => 'product_id', | |
'joinType' => 'INNER' | |
]); | |
$this->hasMany('Invoices', [ | |
'foreignKey' => 'order_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('ordered_qty') | |
->allowEmpty('ordered_qty'); | |
$validator | |
->dateTime('ordered_date') | |
->allowEmpty('ordered_date'); | |
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(['user_id'], 'Users')); | |
$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