加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – 整数字段上的doctrine关系返回字符串

发布时间:2020-05-25 09:00:24 所属栏目:PHP 来源:互联网
导读:我的schema.yml Organisation: columns: id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true } name: { type: string(100), notnull: true, unique: true } par

我的schema.yml

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

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

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

class organisationActions extends autoOrganisationActions{

    public function executeEdit(sfWebRequest $request){

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

结果是字符串(1)“0”

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

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

因此,作为解决方法,您可以实现自定义getter以检查记录是否为null或者是比较操作的第一个(id = 0),如下所示:

class Organisation extends BaSEOrganisation
{

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

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

            return NULL;
        }
    }

在控制器中:

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

    var_dump($p);

它会转储

NULL

if没有链接到任何父节点

并将转储

int(0)

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

希望这有帮助

让我知道你对它的看法

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读