0

im trying to use Cakephp version 3.10 to fire a beforeSave event but it's not doing anything.

This is the only Controller / Table that dont wa

Controller

public function edit($id = null)
{
    $company = $this->Companies->get($id, [
        'contain' => [],
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $company = $this->Companies->patchEntity($company, $this->request->getData());
        if ($this->Companies->save($company)) {
            $this->Flash->success(__('The company has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The company could not be saved. Please, try again.'));
    }
    $users = $this->Companies->Users->find('list', ['limit' => 200]);
    $this->set(compact('company', 'users'));
}

Table:

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Companies Model
 *
 * @property \App\Model\Table\UsersTable&\Cake\ORM\Association\BelongsTo $Users
 * @property \App\Model\Table\OcaServicesTable&\Cake\ORM\Association\BelongsTo $OcaServices
 *
 * @method \App\Model\Entity\Company get($primaryKey, $options = [])
 * @method \App\Model\Entity\Company newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Company[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Company|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Company saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Company patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Company[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Company findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class CompaniesTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('companies');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        // $this->addBehavior('Common');
        // $this->addBehavior('Timestamp');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', null, 'create');

        $validator
            ->scalar('name')
            ->maxLength('name', 255)
            ->allowEmptyString('name');

        $validator
            ->scalar('oca_url')
            ->maxLength('oca_url', 255)
            ->allowEmptyString('oca_url');

        $validator
            ->scalar('oca_user')
            ->maxLength('oca_user', 255)
            ->allowEmptyString('oca_user');

        $validator
            ->scalar('oca_password')
            ->maxLength('oca_password', 255)
            ->allowEmptyString('oca_password');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        return $rules;
    }

    public function beforeSave($event, $entity, $options) {
        \Cake\Log\Log::debug('CommonBehavior loaded');
        debug($event);
    }

}

The beforeSave is not firing, only with this Table / Controller, other controllers works well.

I followed the guide here: https://book.cakephp.org/3/en/orm/behaviors.html

Cakephp version: 3.10 PHP Version: 7.4

What im doing wrong ? Thanks

2
  • I've tried just your beforeSave() on my local setup and it works. You said "...but it's not doing anything", so even your controller isn't working? Is your patchEntity() writing the data and giving the success message? Commented Jun 28, 2024 at 6:08
  • 1
    In your controller, debug(get_class($this->Companies)); just to make sure that it's using the CompaniesTable class that you think it is. Commented Jun 28, 2024 at 6:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.