CakePHP实体名称与表名称不是多元化

时间:2018-03-29 13:16:30

标签: php cakephp cakephp-3.0

我正在试图弄清楚如何让CakePHP识别我的表名和模型的复数/单数形式的faqs / faq。

我尝试过添加Inflector规则,但似乎没有工作:

Inflector::rules('plural', ['faq' => 'faqs']);

它一直在告诉我正在寻找dbname.faq,它何时应该寻找dbname.faqs

感谢您的帮助。

添加了代码:

\模型\实体\ Faq.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * Faq Entity
 *
 * @property int $id
 * @property string $question
 * @property string $answer
 * @property string $category
 * @property \Cake\I18n\FrozenTime $created
 */
//this should be "Faq" but its not workign for some reason
class Faq 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 = [
        'question' => true,
        'answer' => true,
        'category' => true,
        'created' => false
    ];
}

\模型\表\ FaqsTable.php

<?php
namespace App\Model\Table;

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

/**
 * Faqs Model
 *
 * @method \App\Model\Entity\Faq get($primaryKey, $options = [])
 * @method \App\Model\Entity\Faq newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Faq[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Faq|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Faq patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Faq[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Faq findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class FaqsTable extends Table
{

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

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

        $this->addBehavior('Timestamp');
    }

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

        $validator
            ->scalar('question')
            ->maxLength('question', 500)
            ->allowEmpty('question');

        $validator
            ->scalar('answer')
            ->maxLength('answer', 1000)
            ->allowEmpty('answer');

        $validator
            ->scalar('category')
            ->maxLength('category', 50)
            ->allowEmpty('category');

        return $validator;
    }
}

HelpController.php:

<?php

// src/Controller/HelpController.php

namespace App\Controller;

class HelpController extends AppController 
{
    public function index()
    {
    }

    public function faq()
    {
        $this->loadModel('Faq');
        $faqs = $this->Faq->find();
        $this->set(compact('faq'));
    }
}

错误:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.faq' doesn't exist Cake\Database\Exception

0 个答案:

没有答案