所以在我的系统中,我有一些产品,它们有option_names,也有option_values。唯一能让我工作的方法是:
public function productAction($id)
{
$orm = $this
->getDoctrine();
$product = $orm
->getRepository('AppBundle:CatalogueProduct')
->find($id);
$options = $orm
->getRepository('AppBundle:CatalogueOptionName')
->findByProduct($product);
$values = [];
foreach($options as $option)
{
$values[$option->getId()] = $orm
->getRepository('AppBundle:CatalogueOptionValue')
->findByOptionName($option);
}
return $this->render(
'catalogue/product.html.twig',
array(
'product' => $product,
'options' => $options,
'values' => $values
)
);
}
在我看来,这不是很有效。使用普通的SQL,我只会做一个表联接,但在这里我做不到。
我试着创建自己的存储库并使用DQL的join()功能,但它莫名其妙地给了我一个关于"id"索引不存在的错误(我的ManyToOne、OneToMany&join注释很好,我检查了、双重检查和三次检查)。我在谷歌上搜索的解决方案都不是适合我的,所以我基本上偏离了整个想法。
好的,我认为这是一些根本问题
我已经通过这样的注释定义了一个关联:
在产品中:
namespace AppBundleEntityCatalogue;
use DoctrineORMMapping as ORM;
use RepositoryProduct;
/**
* Represents a Product (chair, bed, etc) as fetched from the database 'product' table.
*
* @ORMEntity(repositoryClass="AppBundleEntityCatalogueProductRepository")
* @ORMTable(name="product")
*/
class Product
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMOneToMany(targetEntity="OptionName", mappedBy="product")
*/
private $id;
在OptionName中:
namespace AppBundleEntityCatalogue;
use DoctrineORMMapping as ORM;
/**
* Represents a Product's OptionName (finish, handle, etc). Refer to product documentation on the database design.
*
* @ORMEntity
* @ORMTable(name="option_name")
*/
class OptionName
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMOneToMany(targetEntity="OptionValue", mappedBy="optionName")
*/
private $id;
/**
* @ORMManyToOne(targetEntity="Product", inversedBy="id")
* @ORMJoinColumn(name="product", referencedColumnName="id")
*/
private $product;
在OptionValue中:
<?php
namespace AppBundleEntityCatalogue;
use DoctrineORMMapping as ORM;
/**
* Represents an OptionValue (e.g. blue metal handle). Refer to product documentation on the database design.
*
* @ORMEntity
* @ORMTable(name="option_value")
*/
class OptionValue
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* The ID of the OptionName
*
* @ORMManyToOne(targetEntity="OptionName", inversedBy="id")
* @ORMJoinColumn(name="option_name", referencedColumnName="id")
*/
private $optionName;
为什么我会出现这些错误:
The association AppBundleEntityCatalogueOptionName#product refers to the inverse side field AppBundleEntityCatalogueProduct#id which is not defined as association.
The association AppBundleEntityCatalogueOptionName#product refers to the inverse side field AppBundleEntityCatalogueProduct#id which does not exist.
The association AppBundleEntityCatalogueOptionValue#optionName refers to the inverse side field AppBundleEntityCatalogueOptionName#id which is not defined as association.
The association AppBundleEntityCatalogueOptionValue#optionName refers to the inverse side field AppBundleEntityCatalogueOptionName#id which does not exist.
声称这些领域不存在,但很明显,它们存在?
如果我正确理解您的类关系:
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
*/
class Product
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMManyToMany(targetEntity="Option")
*/
private $options;
public function __construct()
{
$this->options = new ArrayCollection();
}
public function getOptions()
{
return $this->options;
}
}
选项:
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
*/
class Option
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(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:CatalogueProduct')
->find($id);
return $this->render(
'catalogue/product.html.twig',
array(
'product' => $product
)
);
}
模板:
<ul>
{% for option in product.options %}
<li>{{ option.name }}</li>
{% endfor %}
</ul>