在带有 Doctrine2 的 Symfony2 上,Object = Entity 吗?
假设我有一个 User 类:
$user = new User(1);
$user->setName('Bob'); // save "bob" to database with ID 1
$user->setGender('Male'); // save "male" to database with ID 1
echo $user->getName(); // display bob
echo $user->getGender(); // display "male";
echo $user->getDesignation() // display "Mr. Bob"
现在,在 Symfony2 和 Doctrine2 中,Entity 似乎是一个用于与数据库建立链接的对象。所以我认为所有的 setName()
、setGender()
、getName()
& getGender()
函数应该位于 Bundle 的 Entity
目录中的文件中(因为这些函数从数据库中更新或选择数据)。
但是 getDesignation()
又如何呢?
public function getDesignation() {
if ($this->getGender() == 'Male') return "Mr. ".$this->getName();
else return "Ms. ".$this->getName();
}
将与数据库完全没有链接的函数放在实体中可以吗?这不是一个不好的做法吗?
Let's say I have a User
class :
$user = new User(1);
$user->setName('Bob'); // save "bob" to database with ID 1
$user->setGender('Male'); // save "male" to database with ID 1
echo $user->getName(); // display bob
echo $user->getGender(); // display "male";
echo $user->getDesignation() // display "Mr. Bob"
Now, in Symfony2, with Doctrine2, it seems that Entity
is an object which is used to made link with the database. So I think all the setName()
, setGender()
, getName()
& getGender()
functions should go inside a file which is in the Entity
directory of a Bundle (because those functions UPDATE or SELECT data from the database).
But what about getDesignation()
?
public function getDesignation() {
if ($this->getGender() == 'Male') return "Mr. ".$this->getName();
else return "Ms. ".$this->getName();
}
Is it OK to put a function which has absolutely no link with the database in an Entity ? Is it not a bad practice ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,没关系。但仔细观察,它仍然在某种程度上与数据库“链接”,因为它使用了最初来自持久层(数据库)的性别和姓名数据。
这根本不是坏习惯,事实上这是非常有用的。它可以帮助您利用模型对象中的持久层,同时将代码与数据库访问分离。
Yes that's okay. But look closely, it is still somewhat "linked" to the database as it makes use of the gender and name data which originally comes from the persistence layer (database).
That's not at all bad practice, in fact it's something very useful. It helps you to make use of the persistence layer in your model objects while decoupling your code from the database access.
如果您的页面始终以英文显示,则它可以工作,如果您需要将网站翻译成另一种语言,也许您应该创建一个额外的类来处理 Symfony 翻译器和实体,以获得多语言标题。另一种更简单的方法是仅返回人称“先生”。在一个单独的函数中,例如:
因为您可以轻松地在模板中转换它 {{entity.designation|trans}}
无论如何,我只是想贡献一点,但简短的答案是:是的,您可以使用实体来处理额外的内容-超越基本吸气剂的时尚方法
问候
It works if your page is always displayed in english, if you need to translate the website into another language, maybe you should create an additional class to handle with the Symfony translator and the entity in order to get multi-langague titles. Another and simpler approach would be to return only the person title "mr." in a separate function like:
Because you could easily transale it in your template {{entity.designation|trans}}
Anyway I just wanted to contribute a little, but the short answer would be: Yes, you can use the entity to handle extra-fashion-methods beyond the basic getters
Regards