0

Всем привет!

Не могу сделать, а точнее не понимаю как получит данные с двух таблиц. взял пример из официальной доки тут https://symfony.com/doc/3.4/doctrine/associations.html

Однако вижу такую ситуацию:

введите сюда описание изображения

Category entity

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * Category
 */
class Category
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var array
     */
    private $products;

    /**
     * Category constructor.
     */
    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

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

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

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

Product entity:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * Product
 */
class Product
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $categoryId;

    /**
     * @var string
     */
    private $name;

    /**
     * @var float
     */
    private $price;

    /**
     * @var string
     */
    private $description;

    /**
     * @var array
     */
    private $category;

    /**
     * Product constructor.
     */
    public function __construct()
    {
        $this->category = new ArrayCollection();
    }

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

    /**
     * @return string
     */
    public function getCategoryId()
    {
        return $this->categoryId;
    }

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

    /**
     * @return float
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @return array
     */
    public function getCategory()
    {
        return $this->category;
    }

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

    /**
     * @param float $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * @param string $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @param array $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }



}

Маппинги: Категория

AppBundle\Entity\Category:
    type: entity
    table: null
    repositoryClass: AppBundle\Repository\CategoryRepository
    id:
        id:
            type: guid
            id: true
            generator:
                strategy: UUID

    fields:
        name:
            type: string
            nullable: true

    oneToMany:
        products:
            targetEntity: AppBundle\Entity\Product
            mappedBy: category

    lifecycleCallbacks: {  }

И продукт

AppBundle\Entity\Product:
    type: entity
    table: null
    repositoryClass: AppBundle\Repository\ProductRepository
    id:
        id:
            type: guid
            id: true
            generator:
                strategy: UUID


    fields:
        categoryId:
            type: guid
        name:
            type: string
            nullable: true
        price:
            type: decimal
            nullable: true
        description:
            type: text
            nullable: true

    manyToOne:
        category:
            targetEntity: AppBundle\Entity\Category
            inversedBy: products
            joinColumn:
                name: category_id
                referencedColumnName: id
    lifecycleCallbacks: {  }
4
  • Вы не приложили описание связей (либо его вообще нет). Также там может быть lazy load, который подождет с заполнением коллекции до первого обращения.
    – etki
    6 мар 2018 в 18:20
  • @etki поправил. хотя, думаю из доки и так все понятно. Объясните про ленивую загрузку?
    – StereoFlo
    6 мар 2018 в 19:20
  • 1
    Таки да, вы используете ленивую загрузку. fetch="EAGER" (либо кастомный DQL запрос) вам в помощь. 6 мар 2018 в 21:07
  • @DmitriySimushev спасибо, у меня все получилось.
    – StereoFlo
    7 мар 2018 в 5:38

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.