在带有 Doctrine2 的 Symfony2 上,Object = Entity 吗?

发布于 2024-12-16 15:00:33 字数 847 浏览 1 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-12-23 15:00:33

放置一个与数据库完全没有联系的函数可以吗?

是的,没关系。但仔细观察,它仍然在某种程度上与数据库“链接”,因为它使用了最初来自持久层(数据库)的性别和姓名数据。

这不是一个不好的做法吗?

这根本不是坏习惯,事实上这是非常有用的。它可以帮助您利用模型对象中的持久层,同时将代码与数据库访问分离。

Is it OK to put a function which has absolutely no link with the database?

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).

Is it not a bad practice?

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.

一曲琵琶半遮面シ 2024-12-23 15:00:33

如果您的页面始终以英文显示,则它可以工作,如果您需要将网站翻译成另一种语言,也许您应该创建一个额外的类来处理 Symfony 翻译器和实体,以获得多语言标题。另一种更简单的方法是仅返回人称“先生”。在一个单独的函数中,例如:

public function getDesignation() {
  if ($this->getGender() == 'Male') return "Mr." else return "Ms.";
}

因为您可以轻松地在模板中转换它 {{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:

public function getDesignation() {
  if ($this->getGender() == 'Male') return "Mr." else return "Ms.";
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文