与Doctrine的oneToOne问题

时间:2013-06-21 12:54:32

标签: doctrine zend-framework2

我有2个表,我正在尝试使用zend框架创建与doctrine的OneToOne关联,我的错误如下,包含控制器代码的表结构和实体代码可以在下面找到,任何想法为什么我收到此错误?

Fatal error: Call to undefined method Closure::getDate() in C:\htdocs\ea2\module\Easchnitstelle\view\easchnitstelle\transactions\index.phtml on line 132

表格结构

CREATE TABLE `transaction` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `file_id` int(10) unsigned NOT NULL,
  `meta_data_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10386 DEFAULT CHARSET=utf8$$

CREATE TABLE `transaction_meta_data` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `meta_data_sender_id` int(10) unsigned NOT NULL,
  `sender_account` float NOT NULL,
  `sum_amounts` float NOT NULL,
  `count` smallint(5) unsigned NOT NULL,
  `date` date DEFAULT NULL,
  `sum_bankcodes` float NOT NULL,
  `sum_accounts` float NOT NULL,
  `type` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10386 DEFAULT CHARSET=utf8$$

以下是我的两个实体:

namespace Easchnitstelle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
* Entity Class representing a Post of our Zend Framework 2 Blogging Application
*
* @ORM\Entity
* @ORM\Table(name="transaction")
* @property int $id
* @property string $file_id
* @property string $meta_data_id
*/
class Transaction
{
   /**
     * @ORM\Id @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
  protected $id;

  /** @ORM\Column(type="integer") */
  protected $file_id;

  /** @ORM\Column(type="integer") */
  protected $meta_data_id;

    /**
     * @ORM\OneToMany(targetEntity="TransactionData", mappedBy="transaction")
     * @ORM\JoinColumn(name="transaction_id", referencedColumnName="id")
     **/
    private $TransactionData;

    /**
     * @ORM\OneToOne(targetEntity="TransactionMetaData", mappedBy="transaction")
     * @ORM\JoinColumn(name="meta_data_id", referencedColumnName="id")
     **/

    private $TransactionMetaData;

    public function __construct() {
        $this->TransactionData = new ArrayCollection();
        $this->TransactionMetaData = new ArrayCollection();
    }

    public function getTransactionData() {
        return $this->TransactionData;
    }

    public function setTransactionData($TransactionData){
        $this->TransactionData = $TransactionData;
        return $this;
    }

    public function getTransactionMetaData() {
        return $this->TransactionMetaData;
    }

    public function setTransactionMetaData($TransactionMetaData){
        $this->TransactionMetaData = $TransactionMetaData;
        return $this;
    }

      public function setId($id){
        $this->id = $id;
        return $this;
      }

      public function getId(){
        return $this->id;
      }

      public function setFileId($file_id){
        $this->file_id = $file_id;
        return $this;
      }

      public function getFileId(){
        return $this->file_id;
      }

      public function setMetaDataId($meta_data_id){
        $this->meta_data_id = $meta_data_id;
        return $this;
      }

      public function getMetaDataId(){
        return $this->meta_data_id;
      }

}

   <?php
namespace Easchnitstelle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

    /**
    * This class is somewhere in your library
    * @ORM\Entity
    * @ORM\Table(name="transaction_meta_data")
    */

    class TransactionMetaData {

         /**
         * @ORM\Id @ORM\Column(type="integer")
         * @ORM\GeneratedValue
         */
        protected $id;

        /** @ORM\Column(type="integer") */
        protected $meta_data_sender_id;

        /** @ORM\Column(type="float") */
        protected $sum_amounts;

        /** @ORM\Column(type="float") */
        protected $count;

        /** @ORM\Column(type="date") */
        protected $date;

        /** @ORM\Column(type="float") */
        protected $sum_bankcodes;

        /** @ORM\Column(type="float") */
        protected $sum_accounts;

        /** @ORM\Column(type="integer") */
        protected $type;

        /**
         * @ORM\OneToOne(targetEntity="Transaction", mappedBy="TransactionMetaData")
         * @ORM\JoinColumn(name="meta_data_id", referencedColumnName="id")
         **/

        protected $transaction;

        public function setId($id){
            $this->id = $id;
            return $this;
        }

        public function getId(){
            return $this->id;
        }

        public function getSumAmounts() {
            return $this->sum_amounts;
        }

        public function setSumAmounts($sum_amounts) {
            $this->sum_amounts = $sum_amounts;
        }

        public function getCount() {
            return $this->count;
        }

        public function setCount($count) {
            $this->count = $count;
        }

        public function getDate() {
            return $this->date;
        }

        public function setDate($date) {
            $this->date = $date;
        }

        public function getSumBankcodes() {
            return $this->sum_bankcodes;
        }

        public function setSumBankcodes($sum_bankcodes) {
            $this->sum_bankcodes = $sum_bankcodes;
        }

        public function getSumAccounts() {
            return $this->sum_accounts;
        }

        public function setSumAccounts($sum_accounts) {
            $this->sum_accounts = $sum_accounts;
        }

        public function getType() {
            return $this->type;
        }

        public function setType($type) {
            $this->type = $type;
        }

}

以下是我视图中的代码:

        foreach($Transactions as $t){
        $amount = '';
        foreach($t->getTransactionData() as $t_data){
            //echo $amount = $t_data->getPurposes();
        }
        //die;
        //echo get_class($t->getTransactionData());
        //$tm_data = $t->getTransactionMetaData();

        foreach($t->getTransactionMetaData() as $tm_data){
        //echo get_class($t->getTransactionMetaData());

            $date = $tm_data->getDate();
            echo $date;
            die;
        }
        $data[] = array($amount);
    }   

1 个答案:

答案 0 :(得分:1)

关系是1:1,而不是1:n。为什么使用foreach关键字?

在我看到您的代码时,它应该看起来像这样:

foreach($Transactions as $transaction) {

  $metadata = $transaction->getTransactionMetaData();

  echo $metadata->getDate();
}

getTransactionMetaData()

不需要foreach