简体   繁体   中英

Symfony 2 one to many relationship not mapping

I have 2 Entities: OwnerProperty,OwnerPropertyIntl. I declared a relationship between OwnerProperty and OwnerPropertyIntl. I use this command php app/console doctrine:schema:validate , and result: [Mapping] OK - The mapping files are correct. But when i get data by

At file OwnerProperty

    /**
     * One-To-Many, Bidirectional
     *
     * @var ArrayCollection $ownerPropertyIntl
     *
     * @ORM\OneToMany(targetEntity="OwnerPropertyIntl", mappedBy="ownerProperty", cascade={"all"})
     */
    private $ownerPropertyIntl;

   /**
     * Add owner_property_intl
     *
     * @param OwnerPropertyIntl $ownerPropertyIntl
     * @return OwnerProperty
     */
    public function addOwnerPropertyIntl(OwnerPropertyIntl $ownerPropertyIntl)
    {
        $this->ownerPropertyIntl[] = $ownerPropertyIntl;

        return $this;
    }

    /**
     * Remove owner_property_intl
     *
     * @param OwnerPropertyIntl $ownerPropertyIntl
     */
    public function removePropertyIntl(OwnerPropertyIntl $ownerPropertyIntl)
    {
        $this->ownerPropertyIntl->removeElement($ownerPropertyIntl);
    }

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

$ownerProperty = $this->getDoctrine()->getManager()->getRepository('ACMWebBundle:OwnerProperty')->find(123);

and dump this data: 在此处输入图片说明

No data for ownerPropertyIntl, although database have 4 records of ownerPropertyIntl with owner_property_id = 123.

Please help me.

You have to either call getOwnerPropertyIntl() as stated by Fracsi, which will trigger a new SQL query, or you can set FETCH=EAGER on your relationship, which will always pull out the second entity as well. An example is provided here

Unless you really want lazy loading, you should always use eager strategy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM