简体   繁体   English

具有EAV返回值的Symfony2.1

[英]Symfony2.1 with EAV Return Values

I have three Entity: 我有三个实体:

1.Feature 1.功能

2.Value 2.价值

3.Product 3.产品

I am getting Value In Product without mentioning It's Feature. 我在获得“产品价值”时没有提及它的功能。 I am trying to Get Value according to feature But But I am unable to do it.I don't Know How it can be done? 我正在尝试根据功能获取价值,但是我却做不到。我不知道该怎么做?

Feature: 特征:

/**
 * @ORM\Table(name="feature")
 * @ORM\Entity
 */
class Feature
{

/**
 * @ORM\Id
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="name", type="string", length=50)
 */
protected $name;

/**
 * @ORM\OneToMany(targetEntity="Feature", mappedBy="Feature")
 **/
protected $value;

 /**
 * Constructor
 */
public function __construct()
{
    $this->value = new \Doctrine\Common\Collections\ArrayCollection();
}

public function __toString()
{
    return $this->getName();
}
}

Value: 值:

/**
 * @ORM\Table(name="value")
 * @ORM\Entity
 */
class Value
{
/**
 * @ORM\Id
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="name", type="string", length=50)
 */
protected $value;

/**
 * @ORM\ManyToOne(targetEntity="Feature", inversedBy="value")
 **/
protected $feature;

 /**
 * @ORM\ManyToMany(targetEntity="Product", mappedBy="value")
 **/
private $product;

public function __construct()
{
    $this->feature = new \Doctrine\Common\Collections\ArrayCollection();
}

 public function __toString()
{
    return $this->getValue();
}
}

Product: 产品:

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

/**
 * @Gedmo\Translatable
 * @ORM\Column(length=64)
 */
private $title;

/**
 * @Gedmo\Slug(fields={"title"})
 * @ORM\Column(length=64, unique=true)
 */
private $slug;

/**
 * @ORM\Column(type="integer")
 */
protected $quantity;

/**
 * @ORM\Column(type="boolean")
 */
protected $active;

/**
 * @ORM\Column(type="datetime")
 */
protected $updated;

/**
 * @ORM\Column(type="datetime")
 */
protected $created;

/**
 * @ORM\ManyToMany(targetEntity="Value", inversedBy="product")
 * @ORM\JoinTable(name="product_value")
 **/
protected $value;


public function __construct()
{
    $this->active = true;
    $this->updated = new \DateTime();
    $this->created = new \DateTime();
    $this->value = new ArrayCollection();

}

public function __toString()
{
    return $this->getTitle();
}
}

I m not explaining more due to weak English . 由于英语薄弱,我不解释更多。

Example What I want: 我想要的示例:

"I want to select the feature of product for example size and color with values like red , green & small, medium etc. as per product ." “我想选择产品的功能,例如尺寸和颜色,并根据产品选择红色,绿色和较小,中等等值。”

In summary you need to build your own query: 总之,您需要构建自己的查询:

Create a repository for Product and have something like this: 为产品创建一个存储库,并具有以下内容:

$qb = $this->getEntitymanager()->createQueryBuilder();
$qb
    ->select('p, f, v')
    ->from('YourBundleName:Product', 'p')
    ->leftJoin('p.value', 'v')
    ->innerJoin('v.feature', 'f');

return $qb->getQuery()->getResult();

Then for each product, do something like this: 然后针对每个产品执行以下操作:

foreach ($products as $product) {
    echo "Title: " . $product->getTitle() . "<br />";
    echo "Features: <br />";
    foreach ($product->getValue() as $value) {
        echo $value->getFeature()->getName() . ": " . $value->getValue() . "<br />"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM