我正在Symfony2中创建一个表单.表单只包含一个book字段,允许用户在Books实体列表中进行选择.我需要检查所选择的Book属于Author我控制器中的.
public class MyFormType extends AbstractType
{
protected $author;
public function __construct(Author $author) {
$this->author = $author;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('book', 'entity', array('class' => 'AcmeDemoBundle:Book', 'field' => 'title');
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
我想在提交表单后检查所选内容Book是否由$author我的控制器编写:
public class MyController
{
public function doStuffAction() {
$author = ...;
$form = $this->createForm(new MyFormType($author));
$form->bind($this->getRequest());
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我找不到任何办法.我尝试按照Cookbook中的说明创建自定义验证器约束,但是虽然我可以通过EntityManager将验证器定义为服务来传递as参数,但我无法将$author控制器传递给验证器约束.
class HasValidAuthorConstraintValidator extends …Run Code Online (Sandbox Code Playgroud) 我正在使用symfony 1.4和Doctrine.
我正试图找到一种方法来启用调试模式,只有当前sfUser有一个特殊的debugger凭证.
我已经创建了一个过滤器,如果sfUser没有此凭据(在我的文件中web_debug设置为),则停用symfony调试栏:truesettings.yml
class checkWebDebugFilter extends sfFilter
{
public function execute($filterChain)
{
if(!$this->getContext()->getUser()->hasCredential('debugger'))
{
sfConfig::set('sf_web_debug', false);
}
$filterChain->execute();
}
}
Run Code Online (Sandbox Code Playgroud)
我的index.php文件的代码是:
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false));
sfContext::createInstance($configuration)->dispatch();
Run Code Online (Sandbox Code Playgroud)
问题是,由于debug模式false在我的硬编码中index.php,它也被调试器禁用; 因此,Web调试栏不显示Doctrine语句或时序指示.
有没有办法只在当前sfUser具有精确凭证的情况下启用调试模式?
我试图添加sfConfig::set('sf_debug', true);到我的checkWebDebugFilter::execute()方法,但由于过滤器在 Doctrine语句之后执行,因此不会记录它们.
我还尝试添加session_start();我的index.php文件,然后浏览$_SESSION变量以检查当前用户是否具有debugger凭证,但它不起作用(并且它也不符合symfony的精神).
提前感谢您的回答.