phalcon model使用报错:Table 'xxx' doesn't exist in database when dumping meta-data for xxxx
今天上班撸码, 发现一张数据表一直遇到这个问题,搜索引擎找都没有找到合适的解释。
完整错误信息:
Table '1' doesn't exist in database when dumping meta-data for TeacherModel
#0 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(TeacherModel), Object(Phalcon\Di\FactoryDefault))
#1 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(TeacherModel), 'teachermodel-1', '1', '')
#2 /www/songguo-organization/app/logics/Teacher.php(204): Phalcon\Mvc\Model\MetaData->readMetaData(Object(TeacherModel))
后来在发现 , 1 是在 Phalcon\Mvc\Model\MetaData->_initialize()
第三个参数中传入的,查看代码可发现,传的是table名
/**
* Initialize the metadata for certain table
*
* @param \Phalcon\Mvc\ModelInterface $model
* @param mixed $key
* @param mixed $table
* @param mixed $schema
*/
protected final function _initialize(\Phalcon\Mvc\ModelInterface $model, $key, $table, $schema) {}
瞬间破案了.. model 的表名设置,就是填入 source
, 我的 model 如下
<?php
class TeacherModel extends BaseModel
{
public function initialize()
{
parent::initialize();
$this->setSource('org_teacher');
}
}
而正好,我数据表里有 source
这个字段,其值为 1 ,导致最终覆盖了表名。
折腾了半小时,怎么调都不行... 所以写出来希望可以被其他中坑的朋友可以搜索到。
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。