Symfony 1 - 如何从foreach建立链接

时间:2016-03-07 09:12:55

标签: php symfony1 symfony-1.4

我对Symfony来说真的很陌生。我知道它已经老了,而且这个版本的支持已经结束了。但我在实习公司的一个项目仍在symfony 1.4上运行。

我有一个foreach循环;

<?php foreach ($configuration->getFormFields($form, 'show') as $fieldset => $fields): ?>
        <?php //include_partial('service/form_fieldset', array('service' => $service, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?>

        <table id="sf_fieldset_<?php echo preg_replace('/[^a-z0-9_]/', '_', strtolower($fieldset)) ?>" cellspacing="0" cellpadding="0" class="sf_admin_st">
            <tr>
                <th>Property</th>
                <th>Value</th>
            </tr>
            <?php
            $i = 0;
            foreach ($fields as $name => $field):

                ?>
                <?php $i++;
                $class = ($i % 2 == 0) ? 'even' : 'odd';

                ?>

                <?php $attributes = $field->getConfig('attributes', array()); ?>
                <?php if ($field->isPartial()): ?>
                    <?php include_partial('service/' . $name, array('service' => $service, 'form' => $form, 'attributes' => $attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes)) ?>
                <?php elseif ($field->isComponent()): ?>
                    <?php include_component('service', $name, array('service' => $service, 'form' => $form, 'attributes' => $attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes)) ?>
                    <?php else: ?>
                    <tr class="<?php echo $class ?>">
                        <td><?php echo $field->getConfig('label') ? $field->getConfig('label') : $field->getName() ?>:</td>
                        <td><?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?></td>
                    <?php endif; ?>
                    </tr>
                <?php endif; ?>

        <?php endforeach; ?>
        </table>
    <?php endforeach; ?>

上面提到的代码输出表;

Output Table

我的问题是,如何在表格中创建一个指向该特定描述的链接。我希望表中的Parent Service值成为该特定值的链接。

我想到了if,就像这样;

<?php if ($field->getConfig('label') == "parent_service_description"): ?>
                            <td><a href="#"><?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?></a></td>
                        <?php else: ?>
                            <td><?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?></td>
                        <?php endif; ?>

但是我的知识仍然缺乏很多想法。我会非常感谢你的帮助吗?

2 个答案:

答案 0 :(得分:0)

如果你有一个到详细信息页面的路线,我会尝试使用url_for和给定的$ parentId:

<?php if ($field->getConfig('label') == "parent_service_description"): ?>
<td>
   <a href="<?php echo url_for('@show_details?id=' . $parentId) ?>">
      <?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?>
   </a>
</td>

答案 1 :(得分:0)

你应该使用。

echo link_to ($name, "action/module?id=". $sf_params->get("id") . "&params2=2") ) 

背后的原因是它的可读性和简单性。