Created
February 26, 2017 01:39
-
-
Save rafabarbosa/c13e8e762ed698f3a6d7082db4b21c74 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; | |
class ImagesTable extends Table | |
{ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('images'); | |
$this->setDisplayField('id'); | |
$this->setPrimaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsTo('Products', [ | |
'foreignKey' => 'product_id', | |
'joinType' => 'INNER', | |
'cascadeCallbacks' => true | |
]); | |
$this->addBehavior('Xety/Cake3Upload.Upload', [ | |
'fields' => [ | |
'url' => [ | |
'path' => 'upload/images/:md5' | |
] | |
] | |
]); | |
} | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->integer('id') | |
->allowEmpty('id', 'create'); | |
$validator | |
->allowEmpty('url'); | |
return $validator; | |
} | |
public function buildRules(RulesChecker $rules) | |
{ | |
$rules->add($rules->existsIn(['product_id'], 'Products')); | |
return $rules; | |
} | |
public function afterDelete($event) | |
{ | |
$file = new File(WWW_ROOT . $event->data['entity']['url']); | |
$file->delete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment