ORM ManyToOne单向fetchAll对象数组不在phtml Zend Framework 3中呈现

问题描述 投票:2回答:1

该应用程序内置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....
}

另一类

/**
 * 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()

orm zend-framework3
1个回答
0
投票

你的关系映射不正确。更正了以下属性注释:

/**
 * @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。虽然,JoinColumns最常见的用途是JoinTable

© www.soinside.com 2019 - 2024. All rights reserved.