Created
June 16, 2018 08:32
-
-
Save yakubenko/32af536e2bd103b52b56db9ef9eb9ea3 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 | |
Type::map('file', 'App\Database\Type\FileType'); |
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\Database\Type; | |
use Cake\Database\Driver; | |
use Cake\Database\Type; | |
class FileType extends Type | |
{ | |
/** | |
* Marshalls flat data into PHP objects. | |
* | |
* Most useful for converting request data into PHP objects | |
* that make sense for the rest of the ORM/Database layers. | |
* | |
* @param mixed $value The value to convert. | |
* @return mixed Converted value. | |
*/ | |
public function marshal($value) | |
{ | |
return $value; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function toDatabase($value, Driver $driver) | |
{ | |
return $value; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function toPHP($value, Driver $driver) | |
{ | |
return $value; | |
} | |
} |
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\Controller; | |
use App\Controller\AppController; | |
use Cake\Event\Event; | |
class SomeController extends AppController | |
{ | |
function someMethod() | |
{ | |
$id = 'some'; | |
$user = $this->Users->find()->where(['Users.id' => $id]) | |
->contain([])->first(); | |
if ($this->request->is(['post', 'put'])) { | |
$data = [ | |
// Here is our field with a custom type | |
'pic' => $this->request->getData('pic'), | |
]; | |
// LOOK MA, NO LOGIC HERE. Everything has been moved to the Model | |
$this->Users->patchEntity($user, $data); | |
if ($this->Users->save($user)) { | |
$this->Flash->success(__('Message.')); | |
return $this->redirect('/your/url'); | |
} | |
} | |
} |
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; | |
use Cake\Database\Schema\TableSchema; | |
use App\Model\Validation\CommonValidator; | |
class UsersTable extends Table | |
{ | |
protected function _initializeSchema(TableSchema $schema) | |
{ | |
$schema->setColumnType('pic', 'file'); | |
return $schema; | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator Validator instance. | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) | |
{ | |
// Your custom validator here | |
$validator->setProvider('common', new CommonValidator()); | |
$validator->allowEmpty('pic'); | |
$validator->add('pic', 'valid-image', [ | |
'rule' => 'jpegPicture', | |
// Apply a method from your custom validator | |
'provider' => 'common', | |
'message' => __('Only JPEG images are allowed') | |
]); | |
return $validator; | |
} | |
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) | |
{ | |
if (!empty($entity->pic['tmp_name']) && is_uploaded_file($entity->pic['tmp_name'])) { | |
try { | |
// My custom uploader. Use your own logic | |
$imager = new ImagerUtility(); | |
$name = $imager->generateMd5Filename($entity->pic['tmp_name']) . '.jpg'; | |
$imager->resizeAvatar($entity->pic['tmp_name'], $name); | |
$entity->set('pic', $name); | |
} catch (\Exception $e) { | |
Log::debug($e->getMessage()); | |
$entity->set('pic', $entity->getOriginal('pic')); | |
} | |
} else { | |
$entity->set('pic', $entity->getOriginal('pic')); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment