FindBy返回相同的对象

时间:2015-09-04 23:53:02

标签: symfony doctrine

我正在编程通知系统。每个通知都有一个id发送者,id接收者,并且可以拥有一个事件的id(外键,可以为null)。我试图获取列us_id_receiver = X的所有通知。

在我的数据库中,id为X的用户有两条记录作为接收者。我应该得到这两个记录,但是学说会给我两个类似的记录......

我的查询是: " $ this-> getDoctrine() - > getRepository(' HeaventUserBundle:Notification') - > findBy(array(' usIdReceiver' => $ id)) ;"

Array
(
[0] => Heavent\UserBundle\Entity\Notification Object
    (
        [usIdSender:Heavent\UserBundle\Entity\Notification:private] => 10
        [usIdReceiver:Heavent\UserBundle\Entity\Notification:private] => 1
        [nfRead:Heavent\UserBundle\Entity\Notification:private] => 
        [nfMessage:Heavent\UserBundle\Entity\Notification:private] => started to follow you
        [nfDate:Heavent\UserBundle\Entity\Notification:private] => DateTime Object
            (
                [date] => 2015-09-04 22:42:50.000000
                [timezone_type] => 3
                [timezone] => UTC
            )

        [evId:Heavent\UserBundle\Entity\Notification:private] => 
    )

[1] => Heavent\UserBundle\Entity\Notification Object
    (
        [usIdSender:Heavent\UserBundle\Entity\Notification:private] => 10
        [usIdReceiver:Heavent\UserBundle\Entity\Notification:private] => 1
        [nfRead:Heavent\UserBundle\Entity\Notification:private] => 
        [nfMessage:Heavent\UserBundle\Entity\Notification:private] => started to follow you
        [nfDate:Heavent\UserBundle\Entity\Notification:private] => DateTime Object
            (
                [date] => 2015-09-04 22:42:50.000000
                [timezone_type] => 3
                [timezone] => UTC
            )

        [evId:Heavent\UserBundle\Entity\Notification:private] => 
    )

)

但是在我的数据库中这两个记录并不相似,唯一真正的区别是一个有事件id而另一个没有。

这是实体。

<?php

namespace Heavent\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Notification
*
* @ORM\Table(name="c_notification")
* @ORM\Entity(repositoryClass="Heavent\UserBundle\Entity\NotificationRepository")
*/
class Notification
{
/**
 * @var integer
 *
 * @ORM\Column(name="us_id_sender", type="integer")
 * @ORM\Id
 */
private $usIdSender;

/**
 * @var integer
 *
 * @ORM\Column(name="us_id_receiver", type="integer")
 * @ORM\Id
 */
private $usIdReceiver;

/**
 * @var string
 *
 * @ORM\Column(name="nf_date", type="datetime")
 */
private $nfDate;

/**
 * @var integer
 *
 * @ORM\OneToOne(targetEntity="Event")
 * @ORM\JoinColumn(name="ev_id", referencedColumnName="ev_id")
 *
 */
private $evId;


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

/**
 * Set usIdSender
 *
 * @return Notification
 */
public function setUsIdSender($id)
{
    $this->usIdSender = $id;

    return $this;
}

/**
 * Set usIdReceiver
 *
 * @param integer $usIdReceiver
 * @return Notification
 */
public function setUsIdReceiver($usIdReceiver)
{
    $this->usIdReceiver = $usIdReceiver;

    return $this;
}

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

/**
 * Set nfDate
 *
 * @param string $nfDate
 * @return Notification
 */
public function setNfDate($nfDate)
{
    $this->nfDate = $nfDate;

    return $this;
}

/**
 * Get nfDate
 *
 * @return string
 */
public function getNfDate()
{
    return $this->nfDate;
}

/**
 * Set evId
 *
 * @param integer $evId
 * @return Notification
 */
public function setEvId($evId)
{
    $this->eEventEvId = $evId;

    return $this;
}

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

namespace Heavent\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Event
*
* @ORM\Table(name="e_event")
* @ORM\Entity(repositoryClass="Heavent\UserBundle\Entity\EventRepository")
*/
class Event
{
/**
 * @var integer
 *
 * @ORM\Column(name="ev_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
* @var string
*
* @ORM\Column(name="ev_name", type="string", length=255)
*/
private $evName;

...
}

我认为我这两个实体之间的关系就是问题所在。但我自己找不到问题。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

当您在属性上定义关联时,例如:$evId,那么该字段不会是相关实体的ID,而是实例,因此它赢了&当您在代码中调用integer时,{39}成为Event setEvId($evId);对象,$evId应该是Event的实例。< / p>