How to use beforeSave in Cakephp 3?

Asked

Viewed 1,337 times

2

I’m trying to get the image array to handle

In Cakephp 2, I was like this:

    public function beforeSave($options = array())
    {
    if(!empty($this->data['User']['picture']['name'])...

Could someone explain the new syntax of Cake 3:

beforeSave
Cake\ORM\Table::beforeSave(Event $event, Entity $entity, ArrayObject $options)

?

Thank you

1 answer

1

This should help your problem in implementing

// Uses da class - além dos demais necessários
use Cake\Event\Event;
use Cake\ORM\Entity;

// Seu método
public function beforeSave(Event $event, Entity $entity, \ArrayObject $options)
{
    if (isset($entity->picture) && !empty($entity->picture)) {
        $entity->set('picture', trim($entity->picture));
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.