我正在使用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
{
/**
* @ORM\ManyToOne(targetEntity="DocumentType", cascade={"persist"})
* @Assert\NotBlank()
*/
private $documentType;
/**
* @ORM\ManyToMany(targetEntity="Examiner", cascade={"persist"})
* @ORM\JoinTable(name="DocumentAccess_Examiner",
* joinColumns={@ORM\JoinColumn(name="document_access_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="examiner_id", referencedColumnName="id")}
* )
*/
private $examiners;
/**
* @ORM\Column(type="integer")
*/
protected $access;
/**
* @ORM\ManyToMany(targetEntity="Payment", cascade={"persist"})
* @ORM\JoinTable(name="DocumentAccess_Payment",
* joinColumns={@ORM\JoinColumn(name="document_access_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="payment_id", referencedColumnName="id")}
* )
* @Assert\Valid
*/
private $payments;
/**
* @ORM\ManyToMany(targetEntity="Refund", cascade={"persist"})
* @ORM\JoinTable(name="DocumentAccess_Refund",
* joinColumns={@ORM\JoinColumn(name="document_access_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="refund_id", referencedColumnName="id")}
* )
* @Assert\Valid
*/
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 \AppBundle\Entity\DocumentType $documentType
*
* @return DocumentAccess
*/
public function setDocumentType(\AppBundle\Entity\DocumentType $documentType = null)
{
$this->documentType = $documentType;
return $this;
}
/**
* Get documentType
*
* @return \AppBundle\Entity\DocumentType
*/
public function getDocumentType()
{
return $this->documentType;
}
/**
* Add examiner
*
* @param \AppBundle\Entity\Examiner $examiner
*
* @return DocumentAccess
*/
public function addExaminer(\AppBundle\Entity\Examiner $examiner)
{
$this->examiners[] = $examiner;
return $this;
}
/**
* Remove examiner
*
* @param \AppBundle\Entity\Examiner $examiner
*/
public function removeExaminer(\AppBundle\Entity\Examiner $examiner)
{
$this->examiners->removeElement($examiner);
}
/**
* Unset examiners
*
* @return DocumentAccess
*
*/
public function unsetExaminers()
{
$this->examiners = new ArrayCollection();
return $this;
}
/**
* Get examiners
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getExaminers()
{
return $this->examiners;
}
/**
* Add payment
*
* @param \AppBundle\Entity\Payment $payment
*
* @return DocumentAccess
*/
public function addPayment(\AppBundle\Entity\Payment $payment)
{
$this->payments[] = $payment;
return $this;
}
/**
* Add payment only if not still added
*
* @param \AppBundle\Entity\Payment $payment
*
* @return DocumentAccess
*/
public function addPaymentOnce(\AppBundle\Entity\Payment $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 \AppBundle\Entity\Payment $payment
*/
public function removePayment(\AppBundle\Entity\Payment $payment)
{
$this->payments->removeElement($payment);
}
/**
* Unset payments
*
* @return DocumentAccess
*
*/
public function unsetPayments()
{
$this->payments = new ArrayCollection();
return $this;
}
/**
* Get payments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPayments()
{
return $this->payments;
}
/**
* Add refund
*
* @param \AppBundle\Entity\Refund $refund
*
* @return DocumentAccess
*/
public function addRefund(\AppBundle\Entity\Refund $refund)
{
$this->refunds[] = $refund;
return $this;
}
/**
* Add refund only if not still added
*
* @param \AppBundle\Entity\Refund $refund
*
* @return DocumentAccess
*/
public function addRefundOnce(\AppBundle\Entity\Refund $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 \AppBundle\Entity\Refund $refund
*/
public function removeRefund(\AppBundle\Entity\Refund $refund)
{
$this->refunds->removeElement($refund);
}
/**
* Unset refunds
*
* @return DocumentAccess
*
*/
public function unsetRefunds()
{
$this->refunds = new ArrayCollection();
return $this;
}
/**
* Get refunds
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRefunds()
{
return $this->refunds;
}
实体\ DocumentType:
class DocumentType extends EntitySuperclass
{
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="boolean")
*/
private $main;
/**
* @ORM\Column(type="boolean")
*/
private $documentAccessRestriction;
/**
* @ORM\Column(type="integer")
*/
protected $maximumNumberOfExaminers;
/**
* @ORM\Column(type="decimal", precision=11, scale=2)
*/
private $amountOfMoneyPerDocument;
/**
* @ORM\Column(type="decimal", precision=11, scale=2)
*/
private $amountOfMoneyForAdministration;
/**
* @ORM\ManyToOne(targetEntity="Currency", cascade={"persist"})
*/
private $amountOfMoneyCurrency;
/**
* @ORM\ManyToMany(targetEntity="StoreItem", cascade={"persist"})
* @ORM\JoinTable(name="DocumentType_DocumentAccessPaymentStoreItem",
* joinColumns={@ORM\JoinColumn(name="document_type_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="store_item_id", referencedColumnName="id")}
* )
*/
protected $documentAccessPaymentStoreItems;
/**
* @ORM\ManyToMany(targetEntity="StoreItem", cascade={"persist"})
* @ORM\JoinTable(name="DocumentType_DocumentAccessRefundStoreItem",
* joinColumns={@ORM\JoinColumn(name="document_type_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="store_item_id", referencedColumnName="id")}
* )
*/
protected $documentAccessRefundStoreItems;
/**
* @ORM\Column(type="text")
*/
private $nameUrl;
/**
* @ORM\OneToMany(targetEntity="DocumentType", mappedBy="parent")
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="DocumentType", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
/**
* @ORM\ManyToMany(targetEntity="UserGroup")
* @ORM\JoinTable(name="DocumentType_UserGroup",
* joinColumns={@ORM\JoinColumn(name="document_type_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(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;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function documentTypePrePersistPreUpdate()
{
if($this->getMaximumNumberOfExaminers() === NULL) {
$this->setMaximumNumberOfExaminers(0);
}
}
/**
* Set parent
*
* @param \AppBundle\Entity\DocumentType $parent
*
* @return DocumentType
*/
public function setParent(\AppBundle\Entity\DocumentType $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return \AppBundle\Entity\DocumentType
*/
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 \AppBundle\Entity\DocumentType $child
*
* @return DocumentType
*/
public function addChild(\AppBundle\Entity\DocumentType $child)
{
$this->children[] = $child;
$child->setParent($this);
return $this;
}
/**
* Remove child
*
* @param \AppBundle\Entity\DocumentType $child
*/
public function removeChild(\AppBundle\Entity\DocumentType $child)
{
$this->children->removeElement($child);
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Add userGroup
*
* @param \AppBundle\Entity\UserGroup $userGroup
*
* @return DocumentType
*/
public function addUserGroup(\AppBundle\Entity\UserGroup $userGroup)
{
$this->userGroups[] = $userGroup;
return $this;
}
/**
* Remove userGroup
*
* @param \AppBundle\Entity\UserGroup $userGroup
*/
public function removeUserGroup(\AppBundle\Entity\UserGroup $userGroup)
{
$this->userGroups->removeElement($userGroup);
}
/**
* Get userGroups
*
* @return \Doctrine\Common\Collections\Collection
*/
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 \AppBundle\Entity\StoreItem $documentAccessPaymentStoreItem
*
* @return DocumentType
*/
public function addDocumentAccessPaymentStoreItem(\AppBundle\Entity\StoreItem $documentAccessPaymentStoreItem)
{
$this->documentAccessPaymentStoreItems[] = $documentAccessPaymentStoreItem;
return $this;
}
/**
* Remove documentAccessPaymentStoreItem
*
* @param \AppBundle\Entity\StoreItem $documentAccessPaymentStoreItem
*/
public function removeDocumentAccessPaymentStoreItem(\AppBundle\Entity\StoreItem $documentAccessPaymentStoreItem)
{
$this->documentAccessPaymentStoreItems->removeElement($documentAccessPaymentStoreItem);
}
/**
* Get documentAccessPaymentStoreItems
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDocumentAccessPaymentStoreItems()
{
return $this->documentAccessPaymentStoreItems;
}
/**
* Add documentAccessRefundStoreItem
*
* @param \AppBundle\Entity\StoreItem $documentAccessRefundStoreItem
*
* @return DocumentType
*/
public function addDocumentAccessRefundStoreItem(\AppBundle\Entity\StoreItem $documentAccessRefundStoreItem)
{
$this->documentAccessRefundStoreItems[] = $documentAccessRefundStoreItem;
return $this;
}
/**
* Remove documentAccessRefundStoreItem
*
* @param \AppBundle\Entity\StoreItem $documentAccessRefundStoreItem
*/
public function removeDocumentAccessRefundStoreItem(\AppBundle\Entity\StoreItem $documentAccessRefundStoreItem)
{
$this->documentAccessRefundStoreItems->removeElement($documentAccessRefundStoreItem);
}
/**
* Get documentAccessRefundStoreItems
*
* @return \Doctrine\Common\Collections\Collection
*/
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 \AppBundle\Entity\Currency $amountOfMoneyCurrency
*
* @return DocumentType
*/
public function setAmountOfMoneyCurrency(\AppBundle\Entity\Currency $amountOfMoneyCurrency = null)
{
$this->amountOfMoneyCurrency = $amountOfMoneyCurrency;
return $this;
}
/**
* Get amountOfMoneyCurrency
*
* @return \AppBundle\Entity\Currency
*/
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
{
/**
* @ORM\Column(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 }}