douqiao5543 2014-10-27 02:32
浏览 24
已采纳

如何从关系中获得赞美

I've FabricanteDistribuidor.php entity with this code:

class FabricanteDistribuidor
{
    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Pais", mappedBy="fabricanteDistribuidor", cascade={"persist"})
     */
    private $paises;


    /**
     * Set paises
     *
     * @param  \AppBundle\Entity\Pais $pais
     * @return FabricanteDistribuidor
     */
    public function setPaises(\Sencamer\AppBundle\Entity\Pais $pais)
    {
        $this->paises[] = $pais;
        return $this;
    }

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

}

And the related Pais.php entity code:

class Pais
{
    use IdentifierAutogeneratedEntityTrait;
    use NamedEntityTrait;
    use ActiveEntityTrait;

    /**
     * @ORM\ManyToMany(targetEntity="Sencamer\AppBundle\Entity\FabricanteDistribuidor", inversedBy="paises", cascade={"persist"})
     * @ORM\JoinTable(name="negocio.fabricante_distribuidor_pais", schema="negocio",
     *      joinColumns={@ORM\JoinColumn(name="fabricante_distribuidor", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="pais_id", referencedColumnName="id")}
     * )
     */
    protected $fabricanteDistribuidor;

    /**
     * Add fabricanteDistribuidor
     *
     * @param AppBundle\Entity\FabricanteDistribuidor $fabricanteDistribuidor
     */
    public function addfabricanteDistribuidor(\AppBundle\Entity\FabricanteDistribuidor $fabricanteDistribuidor)
    {
        $this->fabricanteDistribuidor[] = $fabricanteDistribuidor;
    }

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

}

I'm trying to get now the related paises from FabricanteDistribuidoras follow and I'm doing something wrong since I can't get their names, so what is wrong in my code?

public function obtenerDetallesFabricanteAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('AppBundle:FabricanteDistribuidor')->find($request->query->get('id'));

    if ($request->isXmlHttpRequest()) {
        $response['entities'] = array();

        $dataResponse = array();
        // rest of columns  .... 

        if ($entity->getPaises() instanceof Pais) {
            $paises = array();
            foreach ($entity->getPaises() as $pais) {
                $paises[] = $pais->getNombre();
            }

            $dataResponse['paises'] = $paises;
        }

        $response['entities'][] = $dataResponse;
        return new JsonResponse($response);
    }
}

Maybe the right way is go through a DQL in a Doctrine Repository but then is the performance problem and here I'm not an expert so need a clue or a workaround on this.

Now with this same classes I've also a concern related the Pais class because as you notice I added the inversed side $fabricanteDistribuidor so, do I have to insert this any time I want to insert a new Pais or is just to tell Doctrine how to deal with proxies inside it? I've not clear yet how owning/inversed side works yet maybe due to this I did thing as my code shown. Any advice around this too?

  • 写回答

1条回答 默认 最新

  • douduoting8408 2014-10-27 03:17
    关注

    May be you're missing to initialize $paises (and also $fabricanteDistribuidor) collection? Shouldn't it be look like this?:

    class FabricanteDistribuidor
    {
        /**
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Pais", mappedBy="fabricanteDistribuidor", cascade={"persist"})
         */
        private $paises;
    
        public function __construct()
        {
            $this->paises = new ArrayCollection(); // don't forget to import it
        }
    
        ....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号