dtz33344 2015-08-25 21:02
浏览 37
已采纳

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

I have two entity TBag and TCustomer, the TBag entity contain a reference to TCustomer but when I try to get my TBag entity the associated entities is null.

The entities have been generated from the database with the doctrine command line doctrine:generate:entities

Thanks for your time :-)

TBag data :

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

TCustomer data :

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

The TBagController code :

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

TBag entity :

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;
    }
}

TCustomer entity :

<?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;
    }
}

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

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
  }
}
  • 写回答

1条回答 默认 最新

  • doubeizhong5178 2015-08-25 21:08
    关注

    It looks like you create one directional relation you need create bidirectional

    From manual:

    A one-to-many association has to be bidirectional, unless you are using an additional join-table. This is necessary, because of the foreign key in a one-to-many association being defined on the “many” side. Doctrine needs a many-to-one association that defines the mapping of this foreign key.

    This bidirectional mapping requires the mappedBy attribute on the OneToMany association and the inversedBy attribute on the ManyToOne association.

    On link below you will find example of how to do it:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图