gpt4 book ai didi

php - 整数字段上的 Doctrine 关系返回字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:41 25 4
gpt4 key购买 nike

我的 schema.yml

Organisation:
columns:
id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true }
name: { type: string(100), notnull: true, unique: true }
parent_organisation_id: { type: integer(4), notnull: false }

relations:
ParentOrganisation: { class: Organisation, local: parent_organisation_id, foreignAlias: ChildOrganisations }

一些组织存储了整数值 0 并且没有这样的 organization_id。令我惊讶的是,当我运行这段代码时

class organisationActions extends autoOrganisationActions{

public function executeEdit(sfWebRequest $request){

$this->organisation = $this->getRoute()->getObject();
$p = $this->organisation->getParentOrganisationId();
var_dump($p);

结果为string(1) "0"

为什么这不返回整数,所以我可以比较=== 0

最佳答案

我做了一些测试,我发现实体的每个值都是通过魔术调用返回的,每个实体模型 sfDoctrineRecord 的父类在 _call 方法中执行。所以 call_user_func_array 的返回类型似乎与 string 或 int 等没有区别。我们在每个如此实现的实体的每个字段上都有相同的行为,id 字段也.

因此,作为变通方法,您可以实现自定义 getter 以检查记录是否为空或第一个 (id=0) 以进行比较操作,如下所示:

class Organisation extends BaseOrganisation
{

public function getParentIdAsIntegerOrNullOtherwise()
{
$an_id = $this->getParentOrganisationId();

if (! is_null($an_id))
{
return intval($an_id);
}

return NULL;
}
}

在 Controller 中:

    $p = $this->organisation->getParentIdAsIntegerOrNullOtherwise();

var_dump($p);

它会倾倒

NULL

如果没有链接到任何父节点

会倾倒

int(0)

如果它链接到 id = 0 的元素

希望对你有帮助

告诉我你的想法

关于php - 整数字段上的 Doctrine 关系返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16410906/

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