具有EAV返回值的Symfony2.1

时间:2012-10-31 05:56:02

标签: php symfony symfony-forms

我有三个实体:

1.Feature

2.Value

3.产品

我在产品中获得价值而没有提及它的特征。我试图根据功能获得价值但是我无法做到。我不知道如何做到这一点?

特点:

/**
 * @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();
}
}

值:

/**
 * @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();
}
}

的产品:

/**
 * @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();
}
}

由于英语不足,我没有解释更多。

示例我想要的:

“我想根据产品选择产品的特征,例如尺寸和颜色,如红色,绿色和小,中等等。”

1 个答案:

答案 0 :(得分:0)

总之,您需要构建自己的查询:

为Product创建一个存储库,并具有以下内容:

$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();

然后对于每个产品,执行以下操作:

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