laminas 中模型的自定义 table 名称
Custom table name for a model in laminas
我已经通过阅读其文档(Album
模型和 table)开始学习 laminas。我有一个 product
模型,我想获取记录,但问题是我的 table 的名称是 tbl_product
,我收到此错误:
Statement could not be executed (42S02 - 1146 - Table
'project.product' doesn't exist)
在哪里可以定义 table 名称?
假设您通过“阅读它的文档”谈论 Laminas' tutorial, then the answer can be found under the the Database and Models section
定义新模型时,必须更新模块的配置,指定应用程序将在何处找到 model's factory and gateway:
// Add this method:
public function getServiceConfig()
{
return [
'factories' => [
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
],
];
}
包含db table信息的class是TableGateway
,其构造函数是:
public function __construct(
$table,
AdapterInterface $adapter,
$features = null,
?ResultSetInterface $resultSetPrototype = null,
?Sql $sql = null
)
第一个参数是table的名字。
因此,在您的配置中,您需要类似以下内容:
Model\ProductTable::class => function($container) {
$tableGateway = $container->get(Model\ProductTableGateway::class);
return new Model\ProductTable($tableGateway);
},
Model\ProductTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Product());
return new TableGateway('tbl_product', $dbAdapter, null, $resultSetPrototype);
},
我已经通过阅读其文档(Album
模型和 table)开始学习 laminas。我有一个 product
模型,我想获取记录,但问题是我的 table 的名称是 tbl_product
,我收到此错误:
Statement could not be executed (42S02 - 1146 - Table 'project.product' doesn't exist)
在哪里可以定义 table 名称?
假设您通过“阅读它的文档”谈论 Laminas' tutorial, then the answer can be found under the the Database and Models section
定义新模型时,必须更新模块的配置,指定应用程序将在何处找到 model's factory and gateway:
// Add this method:
public function getServiceConfig()
{
return [
'factories' => [
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
],
];
}
包含db table信息的class是TableGateway
,其构造函数是:
public function __construct(
$table,
AdapterInterface $adapter,
$features = null,
?ResultSetInterface $resultSetPrototype = null,
?Sql $sql = null
)
第一个参数是table的名字。
因此,在您的配置中,您需要类似以下内容:
Model\ProductTable::class => function($container) {
$tableGateway = $container->get(Model\ProductTableGateway::class);
return new Model\ProductTable($tableGateway);
},
Model\ProductTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Product());
return new TableGateway('tbl_product', $dbAdapter, null, $resultSetPrototype);
},