gpt4 book ai didi

mysql - Doctrine 2 大写首字母

转载 作者:行者123 更新时间:2023-11-29 02:24:18 24 4
gpt4 key购买 nike

在 Doctrine2 中使用 Select 查询获取数据时,我需要将单词的首字母大写。

我试过 UCASE ,但 Doctrine 2 不支持它。

在 Doctrine2 中是否有其他方法将第一个字母大写?

最佳答案

您可以使用Lifecycle Callbacks 达到同样的效果。如果确实不需要在数据库级别执行此操作。

例如,在你的实体中,写一个像这样的后加载方法:

<?php 
namespace MyApp\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks <-- NOTICE THIS ANNOTATION
*/
class MyEntity
{

/**
* @ORM\PostLoad <-- AND THIS
*/
public function capitalizeField()
{
$this->field = mb_ucfirst($this->field);
}

}

Update after two years:

This approach solves the problem. Anyway, in a similar situation I would prefer to change the case before writing the data into the database, if possible. I mean that lifecycle callbacks are not free. Another option is changing the case in presentation/view layer. Today, utilizing the whole event mechanism to change the case of a single value sounds overkill to me.

关于mysql - Doctrine 2 大写首字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com