Skip to content

Instantly share code, notes, and snippets.

@rafabarbosa
Created February 26, 2017 01:39
Show Gist options
  • Save rafabarbosa/c13e8e762ed698f3a6d7082db4b21c74 to your computer and use it in GitHub Desktop.
Save rafabarbosa/c13e8e762ed698f3a6d7082db4b21c74 to your computer and use it in GitHub Desktop.
<?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