Symfony2学说加入OneToMany不起作用

时间:2014-11-04 10:18:18

标签: php symfony doctrine-orm doctrine

我在OneToMany连接中有两个连接的实体,但是当我尝试查询它们时,我得到以下错误

  

注意:未定义的索引:/var/www/Pp/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php第887行中的Mybox

我的实体如下: Mybox(一)

<?php
namespace Pp\CoreBundle\Entity;

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

/**
 * @ORM\Entity(repositoryClass="Pp\CoreBundle\Repository\MyboxRepository")
 * @ORM\Table(name="Mybox")
 */
class Mybox
{
    public static $collectingTypeAny = 0;
    public static $collectingTypeFixed = 1;
    public static $collectingTypeMinimum = 2;

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


    /**
     * @ORM\ManyToOne(targetEntity="Pp\UserBundle\Entity\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\Column(type="integer",nullable=TRUE)
     */
    protected $user_id;

    /**
     * @ORM\OneToMany(targetEntity="Pp\CoreBundle\Entity\Mybox", mappedBy="Mybox")
     */
    protected  $PaypalPayment;


    public function __construct(){
        // set default value for paypal with an array.
        $this->PaypalPayment = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set user
     *
     * @param \Pp\UserBundle\Entity\User $user
     * @return Mybox
     */
    public function setUser(\Pp\UserBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Pp\UserBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Add PaypalPayment
     *
     * @param \Pp\CreditBundle\Entity\Mybox $paypalPayment
     * @return Mybox
     */
    public function addPaypalPayment(\Pp\CreditBundle\Entity\PaypalPayment $paypalPayment)
    {
        $this->PaypalPayment[] = $paypalPayment;

        return $this;
    }

    /**
     * Remove PaypalPayment
     *
     * @param \Pp\CreditBundle\Entity\Mybox $paypalPayment
     */
    public function removePaypalPayment(\Pp\CreditBundle\Entity\PaypalPayment $paypalPayment)
    {
        $this->PaypalPayment->removeElement($paypalPayment);
    }

    /**
     * Get PaypalPayment
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPaypalPayment()
    {
        return $this->PaypalPayment;
    }

    /**
     * get all the payments type in a array
     *
     * @return array
     */
    public function getPayments() {
        $allPayments = Array();
        foreach ( $this->getPaypalPayment() as $paypalPayment ) {
            $allPayments[] = $paypalPayment;
        }
        return $allPayments;
    }


    /**
     * Set user_id
     *
     * @param integer $userId
     * @return Mybox
     */
    public function setUserId($userId)
    {
        $this->user_id = $userId;

        return $this;
    }

    /**
     * Get user_id
     *
     * @return integer
     */
    public function getUserId()
    {
        return $this->user_id;
    }
}

PaypalPayment :(很多)

<?php
namespace Pp\CreditBundle\Entity;

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

/**
 * @ORM\Entity(repositoryClass="Pp\CreditBundle\Repository\PaypalPaymentRepository")
 * @ORM\Table(name="paypal_payment")
 */
class PaypalPayment
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @ORM\ManyToOne(targetEntity="Pp\UserBundle\Entity\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="Pp\CoreBundle\Entity\Mybox")
     * @ORM\JoinColumn(name="mybox_id", referencedColumnName="id")
     */
    protected $mybox;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set user
     *
     * @param \Pp\UserBundle\Entity\User $user
     * @return PaypalPayment
     */
    public function setUser(\Pp\UserBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Pp\UserBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set mybox
     *
     * @param \Pp\CoreBundle\Entity\Mybox $mybox
     * @return PaypalPayment
     */
    public function setMybox(\Pp\CoreBundle\Entity\Mybox $mybox = null)
    {
        $this->mybox = $mybox;

        return $this;
    }

    /**
     * Get mybox
     *
     * @return \Pp\CoreBundle\Entity\Mybox
     */
    public function getMybox()
    {
        return $this->mybox;
    }

}

1 个答案:

答案 0 :(得分:1)

您的错误如下:

  1. 你的mappedBy中有一个拼写错误,你写了Mybox而不是mybox
  2. 您的OneToMany注释引用了自己
  3. 我将反转的部分添加到ManyToOne中,尽管这不是必需的!

    /**
     * @ORM\ManyToOne(targetEntity="Pp\CoreBundle\Entity\Mybox", inversedBy="PaypalPayment")
     * @ORM\JoinColumn(name="mybox_id", referencedColumnName="id")
     */
    protected $mybox;
    
    /**
     * @ORM\OneToMany(targetEntity="Pp\CoreBundle\Entity\PaypalPayment", mappedBy="mybox")
     */
    protected  $PaypalPayment;