未映射Symfony2 + Doctrine2实体(结果为null)

时间:2015-08-25 21:02:28

标签: php symfony doctrine-orm

我有两个实体TBag和TCustomer,TBag实体包含对TCustomer的引用,但是当我尝试获取我的TBag实体时,关联实体为空。

使用doctrine命令行id;extn_id;bagNumber;customer_id;store_id;delivery_date;order_remarks;currency;t_bag_status_id 1;;1;3;1;2015-08-25;test;1;0

从数据库生成实体

感谢您的时间: - )

TBag数据:

id;name;firstName;email;phoneNumber;address;postalCode;city;country_id;birthDate;ipAddress;localization   
3;Quentin;Test;quentin.test@outlook.com;0655555555;30 rue 123;86360;Poitiers;0;1994-11-06;192.168.1.24;FR

TCustomer数据:

$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('LedcMainBundle:TBag')->find(1);

TBagController代码:

namespace Ledc\Bundle\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * TBag
 *
 * @ORM\Table(name="t_bag", uniqueConstraints={@ORM\UniqueConstraint(name="orderNumber_UNIQUE", columns={"bagNumber"})}, indexes={@ORM\Index(name="fk_t_order_t_customer1_idx", columns={"customer_id"}), @ORM\Index(name="fk_t_order_t_store1_idx", columns={"store_id"}), @ORM\Index(name="fk_t_bag_t_bag_status1_idx", columns={"t_bag_status_id"})})
 * @ORM\Entity(repositoryClass="Ledc\Bundle\MainBundle\Entity\Repository\TBagRepository")
 */
class TBag
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="extn_id", type="string", length=100, nullable=true)
     */
    private $extnId;

    /**
     * @var string
     *
     * @ORM\Column(name="bagNumber", type="string", length=45, nullable=true)
     */
    private $bagnumber;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="delivery_date", type="date", nullable=true)
     */
    private $deliveryDate;

    /**
     * @var string
     *
     * @ORM\Column(name="order_remarks", type="text", length=65535, nullable=true)
     */
    private $orderRemarks;

    /**
     * @var integer
     *
     * @ORM\Column(name="currency", type="integer", nullable=true)
     */
    private $currency;

    /**
     * @var \TBagStatus
     *
     * @ORM\ManyToOne(targetEntity="TBagStatus")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="t_bag_status_id", referencedColumnName="id")
     * })
     */
    private $tBagStatus;

    /**
     * @var \TCustomer
     *
     * @ORM\ManyToOne(targetEntity="TCustomer")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     * })
     */
    private $customer;

    /**
     * @var \TStore
     *
     * @ORM\ManyToOne(targetEntity="TStore")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="store_id", referencedColumnName="id")
     * })
     */
    private $store;



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

    /**
     * Set extnId
     *
     * @param string $extnId
     *
     * @return TBag
     */
    public function setExtnId($extnId)
    {
        $this->extnId = $extnId;

        return $this;
    }

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

    /**
     * Set bagnumber
     *
     * @param string $bagnumber
     *
     * @return TBag
     */
    public function setBagnumber($bagnumber)
    {
        $this->bagnumber = $bagnumber;

        return $this;
    }

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

    /**
     * Set deliveryDate
     *
     * @param \DateTime $deliveryDate
     *
     * @return TBag
     */
    public function setDeliveryDate($deliveryDate)
    {
        $this->deliveryDate = $deliveryDate;

        return $this;
    }

    /**
     * Get deliveryDate
     *
     * @return \DateTime
     */
    public function getDeliveryDate()
    {
        return $this->deliveryDate;
    }

    /**
     * Set orderRemarks
     *
     * @param string $orderRemarks
     *
     * @return TBag
     */
    public function setOrderRemarks($orderRemarks)
    {
        $this->orderRemarks = $orderRemarks;

        return $this;
    }

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

    /**
     * Set currency
     *
     * @param integer $currency
     *
     * @return TBag
     */
    public function setCurrency($currency)
    {
        $this->currency = $currency;

        return $this;
    }

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

    /**
     * Set tBagStatus
     *
     * @param \Ledc\Bundle\MainBundle\Entity\TBagStatus $tBagStatus
     *
     * @return TBag
     */
    public function setTBagStatus(\Ledc\Bundle\MainBundle\Entity\TBagStatus $tBagStatus = null)
    {
        $this->tBagStatus = $tBagStatus;

        return $this;
    }

    /**
     * Get tBagStatus
     *
     * @return \Ledc\Bundle\MainBundle\Entity\TBagStatus
     */
    public function getTBagStatus()
    {
        return $this->tBagStatus;
    }

    /**
     * Set customer
     *
     * @param \Ledc\Bundle\MainBundle\Entity\TCustomer $customer
     *
     * @return TBag
     */
    public function setCustomer(\Ledc\Bundle\MainBundle\Entity\TCustomer $customer = null)
    {
        $this->customer = $customer;

        return $this;
    }

    /**
     * Get customer
     *
     * @return \Ledc\Bundle\MainBundle\Entity\TCustomer
     */
    public function getCustomer()
    {
        return $this->customer;
    }

    /**
     * Set store
     *
     * @param \Ledc\Bundle\MainBundle\Entity\TStore $store
     *
     * @return TBag
     */
    public function setStore(\Ledc\Bundle\MainBundle\Entity\TStore $store = null)
    {
        $this->store = $store;

        return $this;
    }

    /**
     * Get store
     *
     * @return \Ledc\Bundle\MainBundle\Entity\TStore
     */
    public function getStore()
    {
        return $this->store;
    }
}

TBag实体:

<?php

namespace Ledc\Bundle\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * TCustomer
 *
 * @ORM\Table(name="t_customer", uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})}, indexes={@ORM\Index(name="fk_t_customer_country1_idx", columns={"country_id"})})
 * @ORM\Entity(repositoryClass="Ledc\Bundle\MainBundle\Entity\Repository\TCustomerRepository")
 */
class TCustomer
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="firstName", type="string", length=100, nullable=true)
     */
    private $firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=100, nullable=true)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="phoneNumber", type="string", length=45, nullable=true)
     */
    private $phonenumber;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="text", length=65535, nullable=true)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="postalCode", type="string", length=45, nullable=true)
     */
    private $postalcode;

    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=100, nullable=true)
     */
    private $city;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="birthDate", type="date", nullable=true)
     */
    private $birthdate;

    /**
     * @var string
     *
     * @ORM\Column(name="ipAddress", type="string", length=45, nullable=false)
     */
    private $ipaddress;

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

    /**
     * @var \TCountry
     *
     * @ORM\ManyToOne(targetEntity="TCountry")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="country_id", referencedColumnName="id")
     * })
     */
    private $country;



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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return TCustomer
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set firstname
     *
     * @param string $firstname
     *
     * @return TCustomer
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     *
     * @return TCustomer
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set phonenumber
     *
     * @param string $phonenumber
     *
     * @return TCustomer
     */
    public function setPhonenumber($phonenumber)
    {
        $this->phonenumber = $phonenumber;

        return $this;
    }

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

    /**
     * Set address
     *
     * @param string $address
     *
     * @return TCustomer
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

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

    /**
     * Set postalcode
     *
     * @param string $postalcode
     *
     * @return TCustomer
     */
    public function setPostalcode($postalcode)
    {
        $this->postalcode = $postalcode;

        return $this;
    }

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

    /**
     * Set city
     *
     * @param string $city
     *
     * @return TCustomer
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

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

    /**
     * Set birthdate
     *
     * @param \DateTime $birthdate
     *
     * @return TCustomer
     */
    public function setBirthdate($birthdate)
    {
        $this->birthdate = $birthdate;

        return $this;
    }

    /**
     * Get birthdate
     *
     * @return \DateTime
     */
    public function getBirthdate()
    {
        return $this->birthdate;
    }

    /**
     * Set ipaddress
     *
     * @param string $ipaddress
     *
     * @return TCustomer
     */
    public function setIpaddress($ipaddress)
    {
        $this->ipaddress = $ipaddress;

        return $this;
    }

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

    /**
     * Set localization
     *
     * @param string $localization
     *
     * @return TCustomer
     */
    public function setLocalization($localization)
    {
        $this->localization = $localization;

        return $this;
    }

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

    /**
     * Set country
     *
     * @param \Ledc\Bundle\MainBundle\Entity\TCountry $country
     *
     * @return TCustomer
     */
    public function setCountry(\Ledc\Bundle\MainBundle\Entity\TCountry $country = null)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return \Ledc\Bundle\MainBundle\Entity\TCountry
     */
    public function getCountry()
    {
        return $this->country;
    }
}

TCustomer实体:

object(stdClass)#531 (10) {
  ["__CLASS__"]=>
  string(34) "Ledc\Bundle\MainBundle\Entity\TBag"
  ["id"]=>
  int(1)
  ["extnId"]=>
  string(0) ""
  ["bagnumber"]=>
  string(1) "1"
  ["deliveryDate"]=>
  object(stdClass)#557 (3) {
    ["__CLASS__"]=>
    string(8) "DateTime"
    ["date"]=>
    string(25) "2015-08-25T00:00:00+02:00"
    ["timezone"]=>
    string(13) "Europe/Berlin"
  }
  ["orderRemarks"]=>
  string(4) "test"
  ["currency"]=>
  int(1)
  ["tBagStatus"]=>
  object(stdClass)#558 (6) {
    ["__CLASS__"]=>
    string(40) "Ledc\Bundle\MainBundle\Entity\TBagStatus"
    ["__IS_PROXY__"]=>
    bool(true)
    ["__PROXY_INITIALIZED__"]=>
    bool(false)
    ["id"]=>
    int(0)
    ["lib"]=>
    NULL
    ["description"]=>
    NULL
  }
  ["customer"]=>
  object(stdClass)#562 (15) {
    ["__CLASS__"]=>
    string(39) "Ledc\Bundle\MainBundle\Entity\TCustomer"
    ["__IS_PROXY__"]=>
    bool(true)
    ["__PROXY_INITIALIZED__"]=>
    bool(false)
    ["id"]=>
    int(3)
    ["name"]=>
    NULL
    ["firstname"]=>
    NULL
    ["email"]=>
    NULL
    ["phonenumber"]=>
    NULL
    ["address"]=>
    NULL
    ["postalcode"]=>
    NULL
    ["city"]=>
    NULL
    ["birthdate"]=>
    NULL
    ["ipaddress"]=>
    NULL
    ["localization"]=>
    NULL
    ["country"]=>
    NULL
  }
  ["store"]=>
  object(stdClass)#591 (12) {
    ["__CLASS__"]=>
    string(36) "Ledc\Bundle\MainBundle\Entity\TStore"
    ["__IS_PROXY__"]=>
    bool(true)
    ["__PROXY_INITIALIZED__"]=>
    bool(false)
    ["id"]=>
    int(1)
    ["name"]=>
    NULL
    ["contact"]=>
    NULL
    ["email"]=>
    NULL
    ["phonenumber"]=>
    NULL
    ["address"]=>
    NULL
    ["postalcode"]=>
    NULL
    ["city"]=>
    NULL
    ["localization"]=>
    NULL
  }
}

\ Doctrine \ Common \ Util \ Debug :: dump($ entity):

SELECT SUM(product_stock_quantity*IF(product_stock_status = '-',-1,1)) AS total From product_stock

1 个答案:

答案 0 :(得分:0)

看起来你创建了一个你需要创建双向的方向关系

来自手册:

  

除非您使用其他连接表,否则一对多关联必须是双向的。这是必要的,因为在“许多”方面定义了一对多关联中的外键。 Doctrine需要一个多对一的关联来定义这个外键的映射。

     

此双向映射需要OneToMany关联上的mappedBy属性和ManyToOne关联上的inversedBy属性。

在下面的链接中,您将找到如何操作的示例:

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional