Created
May 29, 2017 10:43
-
-
Save joshuaso91/462efc32cc1cbcbb6bba5ba2bcf3e52b 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; | |
/** | |
* Products Model | |
* | |
* @property \Cake\ORM\Association\HasMany $Orders | |
* @property \Cake\ORM\Association\HasMany $SameDayOrders | |
* @property \Cake\ORM\Association\BelongsToMany $Stores | |
* @property \Cake\ORM\Association\BelongsToMany $Suppliers | |
* | |
* @method \App\Model\Entity\Product get($primaryKey, $options = []) | |
* @method \App\Model\Entity\Product newEntity($data = null, array $options = []) | |
* @method \App\Model\Entity\Product[] newEntities(array $data, array $options = []) | |
* @method \App\Model\Entity\Product|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \App\Model\Entity\Product patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \App\Model\Entity\Product[] patchEntities($entities, array $data, array $options = []) | |
* @method \App\Model\Entity\Product findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class ProductsTable 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'); | |
$this->displayField('name'); | |
$this->primaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->hasMany('Orders', [ | |
'foreignKey' => 'product_id' | |
]); | |
$this->hasMany('SameDayOrders', [ | |
'foreignKey' => 'product_id' | |
]); | |
$this->belongsToMany('Stores', [ | |
'foreignKey' => 'product_id', | |
'targetForeignKey' => 'store_id', | |
'joinTable' => 'products_stores' | |
]); | |
$this->belongsToMany('Suppliers', [ | |
'foreignKey' => 'product_id', | |
'targetForeignKey' => 'supplier_id', | |
'joinTable' => 'products_suppliers' | |
]); | |
} | |
/** | |
* 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 | |
->requirePresence('name', 'create') | |
->notEmpty('name'); | |
$validator | |
->allowEmpty('unit'); | |
$validator | |
->allowEmpty('notes'); | |
return $validator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment