无法访问null变量的属性。懒加载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法访问null变量的属性。懒加载相关的知识,希望对你有一定的参考价值。

我正在使用Symfony 4.0。

我有一个具有子实体的实体(ManyToOne),它也有一个子实体(ManyToOne)。

documentAccess> documentType> currency

我想访问twig中的currency.symbol属性。如果我只是调用{{ documentAccess.documentType.currency.symbol }}我有一个Impossible to access an attribute ("symbol") on a null variable.但是当我之前调用documentType属性(例如{{ documentAccess.documentType.name }})时,我没有得到错误。这是因为延迟加载吗?我怎么能只为这个树枝模板“初始化”子实体(不添加fetch="EAGER"

谢谢你,最好!

实体 DocumentAccess:

    class DocumentAccess extends EntitySuperclass
{

    /**
     * @ORMManyToOne(targetEntity="DocumentType", cascade={"persist"})
     * @AssertNotBlank()     
     */
    private $documentType;

    /**
     * @ORMManyToMany(targetEntity="Examiner", cascade={"persist"})
     * @ORMJoinTable(name="DocumentAccess_Examiner",
     *      joinColumns={@ORMJoinColumn(name="document_access_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="examiner_id", referencedColumnName="id")}
     *      )
     */
    private $examiners;

    /**
     * @ORMColumn(type="integer")
     */
    protected $access;

    /**
     * @ORMManyToMany(targetEntity="Payment", cascade={"persist"})
     * @ORMJoinTable(name="DocumentAccess_Payment",
     *      joinColumns={@ORMJoinColumn(name="document_access_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="payment_id", referencedColumnName="id")}
     * )
     * @AssertValid
     */
    private $payments;

    /**
     * @ORMManyToMany(targetEntity="Refund", cascade={"persist"})
     * @ORMJoinTable(name="DocumentAccess_Refund",
     *      joinColumns={@ORMJoinColumn(name="document_access_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="refund_id", referencedColumnName="id")}
     * )
     * @AssertValid
     */
    private $refunds;

    public function __construct()
    {
        parent::__construct();
        $this->documentType = NULL;
        $this->access = 0;
        $this->examiners = new ArrayCollection();
        $this->payments = new ArrayCollection();
        $this->refunds = new ArrayCollection();
    }


    /**
     * Set access
     *
     * @param integer $access
     *
     * @return DocumentAccess
     */
    public function setAccess($access)
    {
        $this->access = $access;

        return $this;
    }

    /**
     * Get access
     *
     * @return integer
     */
    public function getAccess()
    {
        return $this->access;
    }

    /**
     * Set documentType
     *
     * @param AppBundleEntityDocumentType $documentType
     *
     * @return DocumentAccess
     */
    public function setDocumentType(AppBundleEntityDocumentType $documentType = null)
    {
        $this->documentType = $documentType;

        return $this;
    }

    /**
     * Get documentType
     *
     * @return AppBundleEntityDocumentType
     */
    public function getDocumentType()
    {
        return $this->documentType;
    }

    /**
     * Add examiner
     *
     * @param AppBundleEntityExaminer $examiner
     *
     * @return DocumentAccess
     */
    public function addExaminer(AppBundleEntityExaminer $examiner)
    {
        $this->examiners[] = $examiner;

        return $this;
    }

    /**
     * Remove examiner
     *
     * @param AppBundleEntityExaminer $examiner
     */
    public function removeExaminer(AppBundleEntityExaminer $examiner)
    {
        $this->examiners->removeElement($examiner);
    }

    /**
     * Unset examiners
     *
     * @return DocumentAccess
     * 
     */
    public function unsetExaminers()
    {
        $this->examiners = new ArrayCollection();

        return $this;
    }

    /**
     * Get examiners
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getExaminers()
    {
        return $this->examiners;
    }


    /**
     * Add payment
     *
     * @param AppBundleEntityPayment $payment
     *
     * @return DocumentAccess
     */
    public function addPayment(AppBundleEntityPayment $payment)
    {
        $this->payments[] = $payment;

        return $this;
    }

    /**
     * Add payment only if not still added
     *
     * @param AppBundleEntityPayment $payment
     *
     * @return DocumentAccess
     */
    public function addPaymentOnce(AppBundleEntityPayment $payment)
    {
        if($this->payments !== NULL and (
            (is_array($this->payments) and in_array($payment, $this->payments)) or (is_object($this->payments) and $this->payments->contains($payment))
        )) {
            return $this;
        }

        $this->payments[] = $payment;

        return $this;
    }

    /**
     * Remove payment
     *
     * @param AppBundleEntityPayment $payment
     */
    public function removePayment(AppBundleEntityPayment $payment)
    {
        $this->payments->removeElement($payment);
    }

    /**
     * Unset payments
     *
     * @return DocumentAccess
     * 
     */
    public function unsetPayments()
    {
        $this->payments = new ArrayCollection();

        return $this;
    }

    /**
     * Get payments
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getPayments()
    {
        return $this->payments;
    }

    /**
     * Add refund
     *
     * @param AppBundleEntityRefund $refund
     *
     * @return DocumentAccess
     */
    public function addRefund(AppBundleEntityRefund $refund)
    {
        $this->refunds[] = $refund;

        return $this;
    }

    /**
     * Add refund only if not still added
     *
     * @param AppBundleEntityRefund $refund
     *
     * @return DocumentAccess
     */
    public function addRefundOnce(AppBundleEntityRefund $refund)
    {
        if($this->refunds !== NULL and (
            (is_array($this->refunds) and in_array($refund, $this->refunds)) or (is_object($this->refunds) and $this->refunds->contains($refund))
        )) {
            return $this;
        }

        $this->refunds[] = $refund;

        return $this;
    }

    /**
     * Remove refund
     *
     * @param AppBundleEntityRefund $refund
     */
    public function removeRefund(AppBundleEntityRefund $refund)
    {
        $this->refunds->removeElement($refund);
    }

    /**
     * Unset refunds
     *
     * @return DocumentAccess
     * 
     */
    public function unsetRefunds()
    {
        $this->refunds = new ArrayCollection();

        return $this;
    }

    /**
     * Get refunds
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getRefunds()
    {
        return $this->refunds;
    }

实体 DocumentType:

    class DocumentType extends EntitySuperclass
{

   /**
     * @ORMColumn(type="text")
     * @AssertNotBlank()
     */
    private $name;

    /**
     * @ORMColumn(type="boolean")
     */
    private $main;

    /**
     * @ORMColumn(type="boolean")
     */
    private $documentAccessRestriction;

    /**
     * @ORMColumn(type="integer")
     */
    protected $maximumNumberOfExaminers;

    /**
     * @ORMColumn(type="decimal", precision=11, scale=2)
     */
    private $amountOfMoneyPerDocument;

    /**
     * @ORMColumn(type="decimal", precision=11, scale=2)
     */
    private $amountOfMoneyForAdministration;

    /**
     * @ORMManyToOne(targetEntity="Currency", cascade={"persist"})
     */
    private $amountOfMoneyCurrency;

    /**
     * @ORMManyToMany(targetEntity="StoreItem", cascade={"persist"})
     * @ORMJoinTable(name="DocumentType_DocumentAccessPaymentStoreItem",
     *      joinColumns={@ORMJoinColumn(name="document_type_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="store_item_id", referencedColumnName="id")}
     * )
     */
    protected $documentAccessPaymentStoreItems;

    /**
     * @ORMManyToMany(targetEntity="StoreItem", cascade={"persist"})
     * @ORMJoinTable(name="DocumentType_DocumentAccessRefundStoreItem",
     *      joinColumns={@ORMJoinColumn(name="document_type_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="store_item_id", referencedColumnName="id")}
     * )
     */
    protected $documentAccessRefundStoreItems;

    /**
     * @ORMColumn(type="text")
     */
    private $nameUrl;

    /**
     * @ORMOneToMany(targetEntity="DocumentType", mappedBy="parent")
     */
    protected $children;

    /**
     * @ORMManyToOne(targetEntity="DocumentType", inversedBy="children")
     * @ORMJoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;

    /**
     * @ORMManyToMany(targetEntity="UserGroup")
     * @ORMJoinTable(name="DocumentType_UserGroup",
     *      joinColumns={@ORMJoinColumn(name="document_type_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="group_id", referencedColumnName="id")}
     *      )
     */
    private $userGroups;

    public function __construct()
    {
        parent::__construct();
        $this->children = new ArrayCollection();
        $this->main = false;
        $this->documentAccessRestriction = false;
        $this->maximumNumberOfExaminers = 0;
        $this->documentAccessPaymentStoreItems = new ArrayCollection();
        $this->documentAccessRefundStoreItems = new ArrayCollection();
        $this->amountOfMoneyForAdministration = 0;
        $this->amountOfMoneyPerDocument = 0;
    }

    /**
     * @ORMPrePersist
     * @ORMPreUpdate
     */
    public function documentTypePrePersistPreUpdate()
    {
        if($this->getMaximumNumberOfExaminers() === NULL) {
            $this->setMaximumNumberOfExaminers(0);
        }
    }

    /**
     * Set parent
     *
     * @param AppBundleEntityDocumentType $parent
     *
     * @return DocumentType
     */
    public function setParent(AppBundleEntityDocumentType $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return AppBundleEntityDocumentType
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return DocumentType
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Add child
     *
     * @param AppBundleEntityDocumentType $child
     *
     * @return DocumentType
     */
    public function addChild(AppBundleEntityDocumentType $child)
    {
        $this->children[] = $child;
        $child->setParent($this);
        return $this;
    }

    /**
     * Remove child
     *
     * @param AppBundleEntityDocumentType $child
     */
    public function removeChild(AppBundleEntityDocumentType $child)
    {
        $this->children->removeElement($child);
    }

    /**
     * Get children
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Add userGroup
     *
     * @param AppBundleEntityUserGroup $userGroup
     *
     * @return DocumentType
     */
    public function addUserGroup(AppBundleEntityUserGroup $userGroup)
    {
        $this->userGroups[] = $userGroup;

        return $this;
    }

    /**
     * Remove userGroup
     *
     * @param AppBundleEntityUserGroup $userGroup
     */
    public function removeUserGroup(AppBundleEntityUserGroup $userGroup)
    {
        $this->userGroups->removeElement($userGroup);
    }

    /**
     * Get userGroups
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getUserGroups()
    {
        return $this->userGroups;
    }


    /**
     * Set nameUrl
     *
     * @param string $nameUrl
     *
     * @return DocumentType
     */
    public function setNameUrl($nameUrl)
    {
        $this->nameUrl = $nameUrl;

        return $this;
    }

    /**
     * Get nameUrl
     *
     * @return string
     */
    public function getNameUrl()
    {
        return $this->nameUrl;
    }

    /**
     * Set main
     *
     * @param boolean $main
     *
     * @return DocumentType
     */
    public function setMain($main)
    {
        $this->main = $main;

        return $this;
    }

    /**
     * Get main
     *
     * @return boolean
     */
    public function getMain()
    {
        return $this->main;
    }

    /**
     * Set maximumNumberOfExaminers
     *
     * @param integer $maximumNumberOfExaminers
     *
     * @return DocumentType
     */
    public function setMaximumNumberOfExaminers($maximumNumberOfExaminers)
    {
        $this->maximumNumberOfExaminers = $maximumNumberOfExaminers;

        return $this;
    }

    /**
     * Get maximumNumberOfExaminers
     *
     * @return integer
     */
    public function getMaximumNumberOfExaminers()
    {
        return $this->maximumNumberOfExaminers;
    }

    /**
     * Add documentAccessPaymentStoreItem
     *
     * @param AppBundleEntityStoreItem $documentAccessPaymentStoreItem
     *
     * @return DocumentType
     */
    public function addDocumentAccessPaymentStoreItem(AppBundleEntityStoreItem $documentAccessPaymentStoreItem)
    {
        $this->documentAccessPaymentStoreItems[] = $documentAccessPaymentStoreItem;

        return $this;
    }

    /**
     * Remove documentAccessPaymentStoreItem
     *
     * @param AppBundleEntityStoreItem $documentAccessPaymentStoreItem
     */
    public function removeDocumentAccessPaymentStoreItem(AppBundleEntityStoreItem $documentAccessPaymentStoreItem)
    {
        $this->documentAccessPaymentStoreItems->removeElement($documentAccessPaymentStoreItem);
    }

    /**
     * Get documentAccessPaymentStoreItems
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getDocumentAccessPaymentStoreItems()
    {
        return $this->documentAccessPaymentStoreItems;
    }

    /**
     * Add documentAccessRefundStoreItem
     *
     * @param AppBundleEntityStoreItem $documentAccessRefundStoreItem
     *
     * @return DocumentType
     */
    public function addDocumentAccessRefundStoreItem(AppBundleEntityStoreItem $documentAccessRefundStoreItem)
    {
        $this->documentAccessRefundStoreItems[] = $documentAccessRefundStoreItem;

        return $this;
    }

    /**
     * Remove documentAccessRefundStoreItem
     *
     * @param AppBundleEntityStoreItem $documentAccessRefundStoreItem
     */
    public function removeDocumentAccessRefundStoreItem(AppBundleEntityStoreItem $documentAccessRefundStoreItem)
    {
        $this->documentAccessRefundStoreItems->removeElement($documentAccessRefundStoreItem);
    }

    /**
     * Get documentAccessRefundStoreItems
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getDocumentAccessRefundStoreItems()
    {
        return $this->documentAccessRefundStoreItems;
    }

    /**
     * Set amountOfMoneyPerDocument
     *
     * @param string $amountOfMoneyPerDocument
     *
     * @return DocumentType
     */
    public function setAmountOfMoneyPerDocument($amountOfMoneyPerDocument)
    {
        $this->amountOfMoneyPerDocument = $amountOfMoneyPerDocument;

        return $this;
    }

    /**
     * Get amountOfMoneyPerDocument
     *
     * @return string
     */
    public function getAmountOfMoneyPerDocument()
    {
        return $this->amountOfMoneyPerDocument;
    }


    /**
     * Set amountOfMoneyForAdministration
     *
     * @param string $amountOfMoneyForAdministration
     *
     * @return DocumentType
     */
    public function setAmountOfMoneyForAdministration($amountOfMoneyForAdministration)
    {
        $this->amountOfMoneyForAdministration = $amountOfMoneyForAdministration;

        return $this;
    }

    /**
     * Get amountOfMoneyForAdministration
     *
     * @return string
     */
    public function getAmountOfMoneyForAdministration()
    {
        return $this->amountOfMoneyForAdministration;
    }

    /**
     * Set amountOfMoneyCurrency
     *
     * @param AppBundleEntityCurrency $amountOfMoneyCurrency
     *
     * @return DocumentType
     */
    public function setAmountOfMoneyCurrency(AppBundleEntityCurrency $amountOfMoneyCurrency = null)
    {
        $this->amountOfMoneyCurrency = $amountOfMoneyCurrency;

        return $this;
    }

    /**
     * Get amountOfMoneyCurrency
     *
     * @return AppBundleEntityCurrency
     */
    public function getAmountOfMoneyCurrency()
    {
        return $this->amountOfMoneyCurrency;
    }

    /**
     * Set documentAccessRestriction
     *
     * @param boolean $documentAccessRestriction
     *
     * @return DocumentType
     */
    public function setDocumentAccessRestriction($documentAccessRestriction)
    {
        $this->documentAccessRestriction = $documentAccessRestriction;

        return $this;
    }

    /**
     * Get documentAccessRestriction
     *
     * @return boolean
     */
    public function getDocumentAccessRestriction()
    {
        return $this->documentAccessRestriction;
    }
}

实体货币:

    class Currency extends EntitySuperclass
{

    /**
     * @ORMColumn(type="string")
     */
    private $symbol;

    /**
     * Set symbol
     *
     * @param string $symbol
     *
     * @return Currency
     */
    public function setSymbol($symbol)
    {
        $this->symbolLeft = $symbol;

        return $this;
    }

    /**
     * Get symbol
     *
     * @return string
     */
    public function getSymbol()
    {
        return $this->symbol;
    }

        }

{{ dump(documentAccess.documentType) }}在我打电话给{% set documentTypeName = parameters.documentAccess.documentType.name %}之前

DocumentType {#1219
  +__isInitialized__: false
  -name: null
  -main: null
  -documentAccessRestriction: null
  #maximumNumberOfExaminers: null
  -amountOfMoneyPerDocument: null
  -amountOfMoneyForAdministration: null
  -amountOfMoneyCurrency: null
  #documentAccessPaymentStoreItems: null
  #documentAccessRefundStoreItems: null
  -nameUrl: null
  #children: null
  -parent: null
  -userGroups: null
  #id: 2
  #hidden: null
  #deleted: null
  #created: null
  #modified: null
  #creator: null
  #modifier: null
  #sorting: null
   …2
}

{{ dump(documentAccess.documentType) }}在我打电话给{% set documentTypeName = parameters.documentAccess.documentType.name %}之后

DocumentType {#1219
  +__isInitialized__: true
  -name: "M3"
  -main: true
  -documentAccessRestriction: true
  #maximumNumberOfExaminers: 4
  -amountOfMoneyPerDocument: "4.00"
  -amountOfMoneyForAdministration: "2.00"
  -amountOfMoneyCurrency: Currency {#1987
    +__isInitialized__: false
    -iso3: null
    #isoNr: null
    -nameEn: null
    -nameDe: null
    -symbol: null
    -symbolRight: null
    -thousandsPoint: null
    -decimalPoint: null
    -decimalDigits: null
    -subNameEn: null
    -subNameDe: null
    -subDivisor: null
    -subSymbolLeft: null
    -subSymbolRight: null
    #id: 49
    #hidden: null
    #deleted: null
    #created: null
    #modified: null
    #creator: null
    #modifier: null
    #sorting: null
     …2
  }
答案

你的错误是你的Symbol中不存在DocumentType

尝试:

{{ documentAccess.documentType.amountOfMoneyCurrency.symbol }}

以上是关于无法访问null变量的属性。懒加载的主要内容,如果未能解决你的问题,请参考以下文章

swift学习第十六天:懒加载和tableView

java 的ViewPage +片段懒加载

类和对象的方法与属性---懒加载与私有事件---单例与私有化构造函数

懒加载和预加载

java反射机制动态获取hibernate懒加载对象

img 的data-src 属性及懒加载