我有一个现有的数据库,其中有大约37个表,它们都是单数名称。现在,我必须制作一个cakepp应用程序,它的控制器名称应该是单数&table_name已经是单数。我的cakepp版本是2.5.5
我可以使用public$useTable="table_name"或影响因素::规则('flural',array('irregular'=>array('表名称'=>'表名称'));
但我认为对于这样多的表来说,这不是一个更好的解决方案。
是否有任何快捷方式可以让所有模型在默认情况下将table_name作为单数语法?
文档显示:
public $useTable = 'your_singular';
在你的模型中就可以了。
使用烘焙外壳
您可以简单地烘焙模型,shell将为与模型名称的默认表化变体不匹配的不规则表名称正确设置Model::$useTable
属性。
另请参阅http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
或AppModel
默认情况下,模型使用模型名称创建表名,使用Inflector::tabelize()
。
因此,一种方法是执行类似的操作,并在AppModel
构造函数中动态设置$useTable
的奇异表名。
下面是一个(未经测试的)例子,说明了我所说的
class AppModel extends Model {
public function __construct($id = false, $table = null, $ds = null) {
if (is_array($id)) {
extract(array_merge(array('name' => $this->name), $id));
}
if ($this->name === null) {
$this->name = (isset($name) ? $name : get_class($this));
}
// set underscored model name as table name
// ex TableName results in table_name
$this->useTable = Inflector::underscore($this->name);
parent::__construct($id, $table, $ds);
}
}