5

私の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 が格納されており、そのような組織 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 を比較できますか?

4

1 に答える 1

3

sfDoctrineRecordいくつかのテストを行ったところ、すべてのエンティティ モデルの親クラスがメソッドで行うマジック コールを介して、エンティティのすべての値が返されることがわかりました_call。したがって、見えたの戻り値の型は、call_user_func_arraystring や int などと区別されません。そのように実装されたすべてのエンティティのすべてのフィールド、idフィールドでも同じ動作をします。

したがって、回避策として、レコードが 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

親ノードにリンクされていない場合

そしてダンプします

int(0)

これが id = 0 の要素にリンクされている場合

この助けを願っています

あなたがそれについてどう思うか教えてください

于 2015-03-16T16:03:32.177 に答える