Привет всем, я пытаюсь сохранить объект json в своей таблице UserTests, я написал код, как в Cake PHP 3.8, но получаю эту ошибку:
Неустранимая ошибка: объявление приложения \ Model \ Table \ UserTestsTable :: _ initializeSchema (схема Cake \ Database \ Schema \ TableSchema $) должна быть совместимой с Cake \ ORM \ Table :: _ initializeSchema (схема Cake \ Database \ Schema \ TableSchemaInterface $): Cake \ Database \ Schema \ TableSchemaInterface в D: \ OSPanel \ domains \ localhost \ LabTests \ src \ Model \ Table \ UserTestsTable. php в строке 115
Я не понимаю, что не так, потому что в Cake PHP 3.8 это работает, но в Cake PHP 4.0.4 нет, пожалуйста, помогите! Это мой код в UserTestsTable. php
<?php
declare(strict_types=1);
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;
/**
* UserTests Model
*
*/
class UserTestsTable extends Table
{
protected function _initializeSchema(TableSchema $schema)
{
$schema->setColumnType('options', 'json');
return $schema;
}
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('user_tests');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Laboratories', [
'foreignKey' => 'laboratory_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Brands', [
'foreignKey' => 'brand_id',
]);
$this->belongsTo('Tests', [
'foreignKey' => 'test_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Statuses', [
'foreignKey' => 'status_id',
'joinType' => 'INNER',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('title')
->maxLength('title', 211)
->requirePresence('title', 'create')
->notEmptyString('title');
$validator
->scalar('image')
->maxLength('image', 211)
->allowEmptyFile('image');
$validator
->scalar('options')
->requirePresence('options', 'create')
->notEmptyString('options');
$validator
->scalar('note')
->maxLength('note', 211)
->allowEmptyString('note');
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): RulesChecker
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
$rules->add($rules->existsIn(['laboratory_id'], 'Laboratories'));
$rules->add($rules->existsIn(['brand_id'], 'Brands'));
$rules->add($rules->existsIn(['test_id'], 'Tests'));
$rules->add($rules->existsIn(['status_id'], 'Statuses'));
return $rules;
}
}
Решено, это правильный код
<?php
declare(strict_types=1);
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Database\Schema\TableSchemaInterface;
/**
* UserTests Model
*
*/
class UserTestsTable extends Table
{
protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface
{
$schema->setColumnType('options', 'json');
return parent::_initializeSchema($schema);
}
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('user_tests');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Laboratories', [
'foreignKey' => 'laboratory_id',
'joinType' => 'INNER',
'conditions' => [
'Laboratories.active' => 1
]
]);
$this->belongsTo('Brands', [
'foreignKey' => 'brand_id',
]);
$this->belongsTo('Tests', [
'foreignKey' => 'test_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Statuses', [
'foreignKey' => 'status_id',
'joinType' => 'INNER',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('title')
->maxLength('title', 211)
->requirePresence('title', 'create')
->notEmptyString('title');
$validator
->scalar('image')
->maxLength('image', 211)
->allowEmptyFile('image');
$validator
->scalar('options')
->requirePresence('options', 'create')
->notEmptyString('options');
$validator
->scalar('note')
->maxLength('note', 211)
->allowEmptyString('note');
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): RulesChecker
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
$rules->add($rules->existsIn(['laboratory_id'], 'Laboratories'));
$rules->add($rules->existsIn(['brand_id'], 'Brands'));
$rules->add($rules->existsIn(['test_id'], 'Tests'));
$rules->add($rules->existsIn(['status_id'], 'Statuses'));
return $rules;
}
}