Doctrine:一种更有效的方式一次性获取所有相关实体?

时间:2016-05-16 15:32:09

标签: mysql symfony doctrine-orm

所以在我的系统中,我有产品,它们有option_names,有option_values。获取那些我可以开始工作的唯一方法就是这个:

public function productAction($id)
{
    $orm = $this
        ->getDoctrine();

    $product = $orm
        ->getRepository('AppBundle:Catalogue\Product')
        ->find($id);

    $options = $orm
        ->getRepository('AppBundle:Catalogue\OptionName')
        ->findByProduct($product);   

    $values = [];

    foreach($options as $option)
    {
        $values[$option->getId()] = $orm
            ->getRepository('AppBundle:Catalogue\OptionValue')
            ->findByOptionName($option);
    }

    return $this->render(
        'catalogue/product.html.twig',
        array(
            'product' => $product,
            'options' => $options,
            'values' => $values
        )
    );
}

这对我来说效率不高。使用普通的SQL,我只会做一个表连接,但我不能在这里做。

我尝试创建自己的存储库并使用DQL的join()功能,但是莫名其妙地给了我一个关于'id'索引不存在的错误(我的ManyToOne,OneToMany& Join注释很好,我检查过,双重检查和三重检查)。我用Google搜索的解决方案都不是我的解决方案,所以我基本上都不同意这一点。

好的,我认为这些是一些根本问题

我通过这样的注释定义了一个关联:

在产品中:     

namespace AppBundle\Entity\Catalogue;

use Doctrine\ORM\Mapping as ORM;
use Repository\Product;

/**
 * Represents a Product (chair, bed, etc) as fetched from the database 'product' table. 
 *
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Catalogue\ProductRepository")
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="OptionName", mappedBy="product")
     */
    private $id;

在OptionName中:     

namespace AppBundle\Entity\Catalogue;

use Doctrine\ORM\Mapping as ORM;

/**
 * Represents a Product's OptionName (finish, handle, etc). Refer to product documentation on the database design.
 *
 * @ORM\Entity
 * @ORM\Table(name="option_name")
 */
class OptionName
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="OptionValue", mappedBy="optionName")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="id")
     * @ORM\JoinColumn(name="product", referencedColumnName="id")
     */
    private $product;

在OptionValue中:

<?php

namespace AppBundle\Entity\Catalogue;

use Doctrine\ORM\Mapping as ORM;

/**
*  Represents an OptionValue (e.g. blue metal handle). Refer to product documentation on the database design.
*  
*  @ORM\Entity
*  @ORM\Table(name="option_value")
*/

class OptionValue
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * The ID of the OptionName
     *
     * @ORM\ManyToOne(targetEntity="OptionName", inversedBy="id")
     * @ORM\JoinColumn(name="option_name", referencedColumnName="id")
     */
    private $optionName;

为什么我会收到这些错误:

The association AppBundle\Entity\Catalogue\OptionName#product refers to the inverse side field AppBundle\Entity\Catalogue\Product#id which is not defined as association.
The association AppBundle\Entity\Catalogue\OptionName#product refers to the inverse side field AppBundle\Entity\Catalogue\Product#id which does not exist.

The association AppBundle\Entity\Catalogue\OptionValue#optionName refers to the inverse side field AppBundle\Entity\Catalogue\OptionName#id which is not defined as association.
The association AppBundle\Entity\Catalogue\OptionValue#optionName refers to the inverse side field AppBundle\Entity\Catalogue\OptionName#id which does not exist.

声称这些字段不存在,显然,它们确实存在?

1 个答案:

答案 0 :(得分:0)

如果我正确理解你的阶级关系:

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Product
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Option")
     */
    private $options;

    public function __construct()
    {
        $this->options = new ArrayCollection();
    }

    public function getOptions()
    {
        return $this->options;
    }
}

选项:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Option
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;
}

生成的sql:

CREATE TABLE option (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT 
NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE product_option (product_id INT NOT NULL, option_id INT NOT NULL, INDEX IDX_6F9B0F6A4584665A (product_id), INDEX IDX_6F9B0F6AA7C41D6F (option_id), PRIMARY KEY(product_id, option_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

用法:

public function productAction($id)
{
    $orm = $this
        ->getDoctrine();

    $product = $orm
        ->getRepository('AppBundle:Catalogue\Product')
        ->find($id);

    return $this->render(
        'catalogue/product.html.twig',
        array(
            'product' => $product
        )
    );
}

模板:

<ul>
    {% for option in product.options %}
        <li>{{ option.name }}</li>
    {% endfor %}
</ul>