ORM ManyToOne 单向 fetchAll 对象数组不在 phtml Zend Framework 3 中呈现
ORM ManyToOne Unidirectional fetchAll Object array does not render in phtml Zend Framework 3
应用程序已内置ZF3。我必须使用 ORM 实体化 ManyToOne 关系。问题是当我通过控制器呈现并且如果通过索引获取数据时它会给出好的结果但是当我将其分配给视图并尝试在 phtml 呈现时它会抛出错误/
/**
* Subscriptions
*
* @ORM\Table(name="subscriptions", indexes={@ORM\Index(name="CUST_ID", columns={"customer_id"}),@ORM\Index(name="SUB_TYPE_ID", columns={"subscription_type_id"})})
* @ORM\Entity
*/
class Subscriptions
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Application\Entity\SubscriptionType
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
* })
*/
protected $subscriptionType;
//set
public function setSubscriptionType(\Application\Entity\SubscriptionType $subscriptionType = null)
{
$this->subscriptionType = $subscriptionType;
return $this;
}
//get
public function getSubscriptionType(){
return $this->subscriptionType;
}
//other setter and getter....
}
另一个Class
/**
* SubscriptionType
*
* @ORM\Table(name="subscription_type")
* @ORM\Entity
*/
class SubscriptionType
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* Get id
*enter code here
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param string $description
*
* @return SubscriptionType
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
}
现在我在控制器上写了...
//一些启蒙
class AdminController extends AbstractActionController
{
//other initiations including __construct....
public function usersubscriptionsAction(){
$this->subscriptions = $this->entityManager->getRepository(Subscriptions::class)->findAll();
/*
foreach($this->subscriptions as $subscription){
//if i am checking with this it gives proper output
echo $subscription->getSubscriptionType()->getDescription();
die();
}
*/
return new ViewModel(
array(
"subscriptions" => $this->subscriptions
)
);
}
}
///我有 phtml 文件
<?php foreach ($subscriptions as $subscription): ?>
//below line throwing an error
<?php echo $subscription->getSubscriptionType()->getDescription(); ?>
<?php endforeach; ?>
当我 运行 它抛出一条错误消息
在 null
上调用成员函数 getDescription()
你的关系映射不正确。更正了下面的 属性 注释:
/**
* @var \Application\Entity\SubscriptionType
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
*/
protected $subscriptionType;
你有
/**
* @var \Application\Entity\SubscriptionType
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
* })
*/
protected $subscriptionType;
@ORM\JoinColumns
用于多列连接,请see the docs. Though, the most common usage of JoinColumns
is for a JoinTable
。
应用程序已内置ZF3。我必须使用 ORM 实体化 ManyToOne 关系。问题是当我通过控制器呈现并且如果通过索引获取数据时它会给出好的结果但是当我将其分配给视图并尝试在 phtml 呈现时它会抛出错误/
/**
* Subscriptions
*
* @ORM\Table(name="subscriptions", indexes={@ORM\Index(name="CUST_ID", columns={"customer_id"}),@ORM\Index(name="SUB_TYPE_ID", columns={"subscription_type_id"})})
* @ORM\Entity
*/
class Subscriptions
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Application\Entity\SubscriptionType
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
* })
*/
protected $subscriptionType;
//set
public function setSubscriptionType(\Application\Entity\SubscriptionType $subscriptionType = null)
{
$this->subscriptionType = $subscriptionType;
return $this;
}
//get
public function getSubscriptionType(){
return $this->subscriptionType;
}
//other setter and getter....
}
另一个Class
/**
* SubscriptionType
*
* @ORM\Table(name="subscription_type")
* @ORM\Entity
*/
class SubscriptionType
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* Get id
*enter code here
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param string $description
*
* @return SubscriptionType
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
}
现在我在控制器上写了...
//一些启蒙
class AdminController extends AbstractActionController
{
//other initiations including __construct....
public function usersubscriptionsAction(){
$this->subscriptions = $this->entityManager->getRepository(Subscriptions::class)->findAll();
/*
foreach($this->subscriptions as $subscription){
//if i am checking with this it gives proper output
echo $subscription->getSubscriptionType()->getDescription();
die();
}
*/
return new ViewModel(
array(
"subscriptions" => $this->subscriptions
)
);
}
}
///我有 phtml 文件
<?php foreach ($subscriptions as $subscription): ?>
//below line throwing an error
<?php echo $subscription->getSubscriptionType()->getDescription(); ?>
<?php endforeach; ?>
当我 运行 它抛出一条错误消息 在 null
上调用成员函数 getDescription()你的关系映射不正确。更正了下面的 属性 注释:
/**
* @var \Application\Entity\SubscriptionType
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
*/
protected $subscriptionType;
你有
/**
* @var \Application\Entity\SubscriptionType
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
* })
*/
protected $subscriptionType;
@ORM\JoinColumns
用于多列连接,请see the docs. Though, the most common usage of JoinColumns
is for a JoinTable
。