我收到此错误:
在第13行的form_div_layout.html.twig中,在呈现模板期间抛出了异常(“Catchable致命错误:类Mihail \ SearchBundle \ Entity \ FileSearch的对象无法转换为字符串”)。
使用symfony 2.8LTS。 以下是我用来基本上在我自己的包中创建搜索表单的文件,它根据表单中提供的搜索条件在app目录中搜索。 搜索控制器:
<?php
namespace Mihail\SearchBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Mihail\SearchBundle\Entity\FileSearch;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Mihail\SearchBundle\Form\Type\SearchType;
class SearchController extends Controller
{
/**
* @Route("/search")
*/
public function searchAction(Request $request)
{
$search = new FileSearch();
$dirsList = $this->generateDirsList();
$form = $this->createForm(SearchType::class, $search, array('dirs_list' => $dirsList));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$searchContent = $form->get('searchContent')->getData();
$fileType = $form->get('fileType')->getData();
$searchDir = $form->get('searchDir')->getData();
$finder = new Finder();
if(isset($fileType))
{
$finder->files()->name("*.$fileType");
}
if(isset($searchDir))
{
$finder->files()->in($searchDir);
}else{
$finder->files()->in(__DIR__);
}
if(isset($searchContent))
{
$finder->contains($searchContent);
}
if(isset($finder) && count($finder)>0)
{
echo "Found results in $searchDir: <br/>";
foreach ($finder as $key => $value) {
echo "<h4> $value contains '$searchContent' </h4>";
}
}else{
echo "No content '$searchContent' in '$searchDir'";
}
}
return $this->render('MihailSearchBundle:randomsearch:search_form.html.twig', array(
'form' => $form->createView(),
));
}
public function generateDirsList()
{
$path = $this->get('kernel')->getRootDir();
$finder = new Finder();
$iterator = $finder
->ignoreUnreadableDirs()
->directories()
->depth('< 2')
->in($path);
$dirsList = array();
foreach ($iterator as $key => $value) {
$dirsList["$value"] = "$value";
}
return $dirsList;
}
}
获取并设置文件搜索字段
<?php
namespace Mihail\SearchBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class FileSearch
{
protected $searchContent;
protected $fileType;
protected $searchDir;
public function getSearchContent()
{
return $this->searchContent;
}
public function setSearchContent($searchContent)
{
$this->searchContent = $searchContent;
}
public function getFileType()
{
return $this->fileType;
}
public function setFileType($fileType)
{
$this->fileType = $fileType;
}
public function getSearchDir()
{
return $this->searchDir;
}
public function setSearchDir($searchDir)
{
$this->searchDir = $searchDir;
}
}
用于创建表单的搜索类型
<?php
namespace Mihail\SearchBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('searchContent', TextType::class, array('label'=> 'Search Content:* '))
->add('fileType', TextType::class , array('label'=> 'File Type:'))
->add('searchDir', ChoiceType::class, array(
'label' => 'Search Path:',
'choices' => $options['dirs_list'],
'choices_as_values' => true))
->add('save', SubmitType::class, array('label' => 'Search for File'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array(
'dirs_list',
));
$resolver->setDefaults(array(
'data_class' => 'Mihail\SearchBundle\Entity\FileSearch',
'dirs_list' => null,
));
}
}
用于呈现表单字段的简单树枝模板
{# Mihail/SearchBudnle/Resources/views/randomsearch/search_form.html.twig #}
{{ form(form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_start(form) }}
{{ form_row(form.fileType) }}
{{ form_row(form.searchContent) }}
{{ form_row(form.searchDir) }}
{{ form_end(form) }}
任何想法我做错了什么?