如何在 Symfony 2 和 Doctrine 中过滤实体对象内的数据
How filter data inside entity object in Symfony 2 and Doctrine
我有两个实体:Product
和 Feature
。 Product
有许多其他 Features
(一对多关系)。每个 Feature
都有一个名称和一个重要状态(如果功能重要则为 true,否则为 false)。我想在 TWIG 中获得我产品的所有重要功能。
下面的解决方案非常难看:
Product: {{ product.name }}
Important features:
{% for feature in product.features %}
{% if feature.important == true %}
- {{ feature.name }}
{% endif %}
{% endfor %}
所以我想得到:
Product: {{ product.name }}
Important features:
{% for feature in product.importantFeatures %}
- {{ feature.name }}
{% endfor %}
我必须过滤实体对象中的数据,但是如何?
// MyBundle/Entity/Vehicle.php
class Product {
protected $features; // (oneToMany)
// ...
protected getFeatures() { // default method
return $this->features;
}
protected getImportantFeatures() { // my custom method
// ? what next ?
}
}
// MyBundle/Entity/Feature.php
class Feature {
protected $name; // (string)
protected $important; // (boolean)
// ...
}
您可以从存储库中完成
$featureEntityRepository->findBy(array(
'impoertant' => true,
'product' => $product->getId()
));
可以使用Criteriaclass筛选出相关特征的Arraycollection
class Product {
protected $features; // (oneToMany)
// ...
protected getFeatures() { // default method
return $this->features;
}
protected getImportantFeatures() { // my custom method
$criteria = \Doctrine\Common\Collections\Criteria::create()
->where(\Doctrine\Common\Collections\Criteria::expr()->eq("important", true));
return $this->features->matching($criteria);
}
}
在树枝上
Product: {{ product.name }}
Important features:
{% for feature in product.getImportantFeatures() %}
- {{ feature.name }}
{% endfor %}
我有两个实体:Product
和 Feature
。 Product
有许多其他 Features
(一对多关系)。每个 Feature
都有一个名称和一个重要状态(如果功能重要则为 true,否则为 false)。我想在 TWIG 中获得我产品的所有重要功能。
下面的解决方案非常难看:
Product: {{ product.name }}
Important features:
{% for feature in product.features %}
{% if feature.important == true %}
- {{ feature.name }}
{% endif %}
{% endfor %}
所以我想得到:
Product: {{ product.name }}
Important features:
{% for feature in product.importantFeatures %}
- {{ feature.name }}
{% endfor %}
我必须过滤实体对象中的数据,但是如何?
// MyBundle/Entity/Vehicle.php
class Product {
protected $features; // (oneToMany)
// ...
protected getFeatures() { // default method
return $this->features;
}
protected getImportantFeatures() { // my custom method
// ? what next ?
}
}
// MyBundle/Entity/Feature.php
class Feature {
protected $name; // (string)
protected $important; // (boolean)
// ...
}
您可以从存储库中完成
$featureEntityRepository->findBy(array(
'impoertant' => true,
'product' => $product->getId()
));
可以使用Criteriaclass筛选出相关特征的Arraycollection
class Product {
protected $features; // (oneToMany)
// ...
protected getFeatures() { // default method
return $this->features;
}
protected getImportantFeatures() { // my custom method
$criteria = \Doctrine\Common\Collections\Criteria::create()
->where(\Doctrine\Common\Collections\Criteria::expr()->eq("important", true));
return $this->features->matching($criteria);
}
}
在树枝上
Product: {{ product.name }}
Important features:
{% for feature in product.getImportantFeatures() %}
- {{ feature.name }}
{% endfor %}