简体   繁体   中英

ORM ManyToOne Unidirectional fetchAll Object array does not render in phtml Zend Framework 3

The application has built in ZF3. I have to entity with ManyToOne relationship using ORM. The issue is when i render through controller and and if fetch data via index it gives okay result but when i assign that to view and trying to render at phtml it throws an error/

/**
 * 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....
}

Another 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;
    }
}

Now at controller i have wrote...

//some initiations

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
                )
         );
}

}

///i have phtml file

<?php foreach ($subscriptions as $subscription): ?>
  //below line throwing an error
  <?php echo $subscription->getSubscriptionType()->getDescription(); ?>
 <?php endforeach; ?>

when i run it throw an erro message Call to a member function getDescription() on null

You've got the relation mapping incorrect. Corrected the property annotation below:

/**
 * @var \Application\Entity\SubscriptionType
 *
 * @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
 * @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
 */
protected $subscriptionType;

You had

/**
 * @var \Application\Entity\SubscriptionType
 *
 * @ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
 * })
 */
protected $subscriptionType;

The @ORM\\JoinColumns is used for when you have a join with multiple columns, please see the docs . Though, the most common usage of JoinColumns is for a JoinTable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM