【问题标题】:Cakephp 3 Base table or view not found, table is not pluralCakephp 3 未找到基表或视图,表不是复数
【发布时间】:2019-03-20 02:45:44
【问题描述】:

您好,我创建了一个名为 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);

}

健康记录.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
];
}

健康记录表

<?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 文件并清除那里的缓存。 还尝试删除模型文件并重新烘焙,但到目前为止似乎没有任何效果。任何帮助将不胜感激!

【问题讨论】:

    标签: php cakephp cakephp-3.0


    【解决方案1】:

    已解决,我还必须复数调用模型,所以对于这种情况

    而不是

    $this->loadModel('HealthRecord');
    

    必须是

    $this->loadModel('HealthRecords');
    

    而不是

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

    必须是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-28
      • 2017-08-02
      • 2023-03-08
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 2014-02-25
      相关资源
      最近更新 更多