chi*_*ser 4 php doctrine dql symfony twig
我对使用Symfony2中的实体的嵌套记录做一个组合框有一个严重的疑问.我已经在http://gediminasm.org/article/tree-nestedset-behavior-extension-for-doctrine-2中阅读了关于Doctrine 2的嵌套树扩展,它看起来很有趣,但它没有提到如何实现这个嵌套树形成一个实体字段.
另外,我已经阅读了更多关于PHP中递归函数的内容,我发现了一个有趣的博客,在这里分析它,这里是链接http://www.sitepoint.com/hierarchical-data-database/,它具体解释了这个递归函数:
function display_children($parent, $level) {
// Retrieve all children of $parent
$result = mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'"');
// Display each child
while ($row = mysql_fetch_array($result)) {
// Indent and display the title of this child
echo str_repeat(' ',$level).$row['title']."\n";
// Call this function again to display this child's children
display_children($row['title'], $level+1);
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道如何将此代码转换为Symfony2以及它将被存储的位置(Controller,Entity等).如果有人对使用Twig Extensions处理嵌套记录有其他想法,那也会很感激.
非常感谢你的帮助.
这就是我们为类别(缩进下拉列表)实现嵌套树以便在产品编辑表单中使用的方法:
定义您的Category实体类,如文档中所示
向Category实体类添加一个方法,该类显示由嵌套级别缩进的名称
/**
* @ORM\Table()
* @ORM\Entity(repositoryClass="CP\YourBundle\Entity\CategoryRepository")
* @Gedmo\Tree(type="nested")
*/
class Category
{
public function getOptionLabel()
{
return str_repeat(
html_entity_decode(' ', ENT_QUOTES, 'UTF-8'),
($this->getLevel() + 1) * 3
) . $this->getName();
}
Run Code Online (Sandbox Code Playgroud)使用Doctrine2注释定义与Category实体的产品实体关系(在我们的例子中,我们为一个产品提供多个类别支持)
class Product
{
/**
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity="Category", cascade={"persist", "remove"})
*/
private $categories;
...
Run Code Online (Sandbox Code Playgroud)现在,您只需将以下内容添加到ProductType表单类中
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('categories', null, array('property' => 'optionLabel'));
}
Run Code Online (Sandbox Code Playgroud)现在,表单应显示带有正确缩进的类别列表的下拉列表
| 归档时间: |
|
| 查看次数: |
9005 次 |
| 最近记录: |