Cakephp 3未找到基本表或视图,表不是复数

时间:2019-03-19 18:27:14

标签: php cakephp cakephp-3.0

嗨,我制作了一个名为health_records的表,烘焙了该模型,并生成了一个名为HealthRecord.php的实体和表HealthRecordsTables.php。试图将数据保存到它,这让我犯了这个错误

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sparkplug.health_record' doesn't exist

下面是我对应文件的代码

控制器

 public function test() {
    $this->loadModel('HealthRecord');

    $HealthRecord =$this->HealthRecord->newEntity();

    $data = [
        'user_id' => $this->Auth->user('id'),
        'type' => 'test',
        'entry' => ['test' => '1'],
        'meta' => json_encode(['test' => '1']),
    ];

    $HealthRecord = $this->HealthRecord->patchEntity($HealthRecord,$data);
    dd($HealthRecord);
    //$this->HealthRecord->save($healthRecord);

}

HealthRecord.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * HealthRecord Entity
 *
 * @property int $id
 * @property int $user_id
 * @property string $type
 * @property array $entry
 * @property array $meta
 * @property \Cake\I18n\FrozenTime $created
 * @property \Cake\I18n\FrozenTime $modified
 *
 * @property \App\Model\Entity\User $user
 */
class HealthRecord extends Entity
{

/**
 * Fields that can be mass assigned using newEntity() or patchEntity().
 *
 * Note that when '*' is set to true, this allows all unspecified fields to
 * be mass assigned. For security purposes, it is advised to set '*' to false
 * (or remove it), and explicitly make individual fields accessible as needed.
 *
 * @var array
 */
protected $_accessible = [
    'user_id' => true,
    'type' => true,
    'entry' => true,
    'meta' => true,
    'created' => true,
    'modified' => true,
    'user' => true
];
}

HealthRecordsTables

<?php
namespace App\Model\Table;

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

/**
 * HealthRecords Model
 *
 * @property \App\Model\Table\UsersTable|\Cake\ORM\Association\BelongsTo $Users
 *
 * @method \App\Model\Entity\HealthRecord get($primaryKey, $options = [])
 * @method \App\Model\Entity\HealthRecord newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\HealthRecord[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\HealthRecord|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\HealthRecord|bool saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\HealthRecord patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\HealthRecord[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\HealthRecord findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class HealthRecordsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

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

        $this->addBehavior('Timestamp');

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

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

        $validator
            ->scalar('type')
            ->maxLength('type', 255)
            ->requirePresence('type', 'create')
            ->allowEmptyString('type', false);

        $validator
            ->requirePresence('entry', 'create')
            ->allowEmptyString('entry', false);

        $validator
            ->requirePresence('meta', 'create')
            ->allowEmptyString('meta', false);

        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)
    {
        $rules->add($rules->existsIn(['user_id'], 'Users'));

        return $rules;
    }
}

我很累要清除模型缓存,甚至要进入tmp文件并在那里清除缓存。 还尝试删除模型文件并重新烘焙,但到目前为止似乎没有任何效果。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

解决了,在这种情况下,我还必须对模型进行多重调用

代替

$this->loadModel('HealthRecord');

必须是

$this->loadModel('HealthRecords');

,而不是

$HealthRecord =$this->HealthRecord->newEntity();

必须是

$HealthRecord =$this->HealthRecords->newEntity();