学说:获取相关实体记录是获取对象,获取数组

时间:2015-11-27 12:53:01

标签: php symfony doctrine-orm

我已将贷款表与出价表相关联,其中单笔贷款可以有多个出价。当我尝试获取贷款时,我应该获得投标详情,但我无法得到它。

我的实体是:

class Loan extends Base {
   //....
   /**
    * @ORM\OneToMany(targetEntity="Bid", mappedBy="loan")
    */
   protected $bids;
   //....
}

class Bid extends Base {
   //....
   /**
    * @ORM\ManyToOne(targetEntity="Loan", inversedBy="bids")
    * @ORM\JoinColumn(name="loan_id", referencedColumnName="id",
                onDelete="CASCADE")
    *
    */
     protected $loan;

   //....
}

我的输出是(参见贷款明细中的出价):

{
  "loan_amount_required": 30000,
  "interest_rate": 3,
  "loan_duration": 1,
  "loan_validity": 5,
  "status": "active",
  "loan_created": "2015-11-27 12:35:56",
  "user": {
    "fname": "",
    "lname": "",
    "email": "",
    "password": "",
    "is_active": true,
    "random_number": ,
    "role": "user",
    "balance": 0,
    "last_seen": "........",
    "notify_me": true,
    "id": 34
  },
  "bids": [
    {}
  ],
  "id": 71
}

1 个答案:

答案 0 :(得分:0)

在Loan构造函数中尝试:

// ...
use Doctrine\Common\Collections\ArrayCollection;

class Loan extends Base
{
    // ...

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