[英]Namespaces with Phalcon models
我有一个使用命令phalcon project simple --type=simple
。
我根本没有改变结构。
我难倒的部分是我有两个数据库。
我想访问数据库A和B的帐户模型, $account = AAccount::find();
像$account = AAccount::find();
和$account = BAccount::find();
。
我目前有这个代码:
模型/ AAccount.php
class AAccount extends Phalcon\Mvc\Model {
// use DB A
}
模型/ BAccount.php
class BAccount extends Phalcon\Mvc\Model {
// use DB B
}
这样做的最佳方式是什么? 命名空间? 我无法更改两者的表名帐户 。
我不知道我是否理解你的问题:你有两个同名的表,但它们有两个不同的模式(数据库)? 如果是,我有同样的问题,我用以下结构解决它(你可以在我的bauhaus项目中看到这部分代码)(参见这个参考: 指向其他模式(Phalcon - 使用模型) ):
(1)位于models/
基础模型类:
namespace MyApp\Model;
class Base extends \Phalcon\Mvc\Model
{
// code for your model base class
}
(2)位于models/schema-a/
模式A的基类:
namespace MyApp\Model\SchemaA;
class Base extends MyApp\Model\Base
{
// ...
// returns the name of the schema A
public function getSchema()
{
return `schema_a_name`;
}
// ...
}
(3)位于models/schema-b/
模式B的基类:
namespace MyApp\Model\SchemaB;
class Base extends MyApp\Model\Base
{
// ...
// returns the name of the schema B
public function getSchema()
{
return `schema_b_name`;
}
// ...
}
(4)模型A中的帐户模型位于models/schema-a/
:
namespace MyApp\Model\SchemaA;
class Account extends Base
{
// ...
}
(5)位于models/schema-b/
的模式B中的Account Model:
namespace MyApp\Model\SchemaB;
class Account extends Base
{
// ...
}
当您拥有固定数量的模式时 ,此解决方案很有效,但是如果您没有固定数量的模式,我认为更好的解决方案是在模型库的getSchema
函数中创建逻辑。 就像是:
public function getSchema()
{
// this is just a suggest
return $this->getDI()->scope->currentSchema;
}
我希望这可以帮到你。
注意:您必须小心地在具有命名空间的模型之间创建关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.