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
beforeSave()on my local setup and it works. You said "...but it's not doing anything", so even your controller isn't working? Is yourpatchEntity()writing the data and giving the success message?debug(get_class($this->Companies));just to make sure that it's using the CompaniesTable class that you think it is.