因此,在我的系统中,我有一些产品,其中有 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 注释都很好,我检查了,双重-检查并三次检查)。我在谷歌上搜索到的解决方案都不适合我,所以我基本上放弃了整个想法。
好吧,我认为这些是一些根本问题
我通过注释定义了一个关联,如下所示:
在产品中:
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;
在选项名称中:
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.
声称这些字段不存在,但显然它们确实存在?
最佳答案
如果我正确理解了你的类(Class)关系:
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>
关于mysql - 教义 : a more efficient way to fetch all related entities in one swoop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37257648/