应为字符串类型的参数,给定的对象为-Symfony2



我有朋友和类别-每个朋友都有一个类别。在主页上,可以看到所有朋友的列表,还有一个包含所有类别的下拉菜单和一个提交按钮——当你选择类别并单击它时,你只会看到该类别中的朋友。

这是Category.php:

<?php
namespace EMMyFriendsBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineCommonCollectionsArrayCollection;
/**
* EMMyFriendsBundleEntityCategory
* @ORMEntity
* @ORMTable(name="categories")
* @ORMEntity(repositoryClass="EMMyFriendsBundleEntitySameRepository")
*/
class Category 
{
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", length=100)
* @AssertNotBlank()
* @AssertMinLength(
*         limit=3,
*         message="The name is too short." )
*/
protected $name;
/**
* @ORMColumn(type="string", length=225, nullable=true)
*/
protected $description;
/**
* @ORMManyToOne(targetEntity="User", inversedBy="categories")
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORMOneToMany(targetEntity="Friend", mappedBy="category")
* @ORMOrderBy({"name" = "ASC"})
*/
protected $friends;
public function __construct()
{
$this->friends = new DoctrineCommonCollectionsArrayCollection();
} 
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getDescription()
{
return $this->description;
}
public function setUser(EMMyFriendsBundleEntityUser $user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
public function addFriend(EMMyFriendsBundleEntityFriend $friend)
{
$this->friends[] = $friend;
}
public function getFriends()
{
return $this->friends;
}
} 

选择CatType.php

<?php
namespace EMMyFriendsBundleEntity;
use SymfonyComponentFormFormBuilder;
use SymfonyComponentFormAbstractType;
class ChooseCatType extends AbstractType
{
protected $user;
function __construct($user) 
{
$this->user = $user;
}
public function buildForm(FormBuilder $builder, array $options)
{
$user = $this->user;
$builder->add('name', 'entity', array(
'class' => 'EMMyFriendsBundle:Category',
'property' => 'name',
'empty_value' => 'All friends',
'required' => false,
'query_builder' => function ($repository) use ($user)
{ return $repository->createQueryBuilder('cat')
->select('cat')
->where('cat.user = :user')
->orderBy('cat.name', 'ASC')
->setParameter('user', $user);
}, ));
}
public function getName()
{
return 'choose_category';
}
}

这是控制器的动作:

public function filterAction(Request $request)
{
$this->init();
$cat = new Category();
$dd_form = $this->createForm(new ChooseCatType($this->user), $cat);
if($request->getMethod() == 'POST') { 
$dd_form->bindRequest($request);
if($cat->getName() == null) {      
return $this->redirect($this->generateUrl('home_display'));
}
$filter = $cat->getName()->getId();
if ($dd_form->isValid()) {   
$friends = $this->em->getRepository('EMMyFriendsBundle:Friend')
->filterFriends($filter);
if(!$friends) {
$this->setFlash('There are no frieds in this category.', 'error');
return $this->redirect($this->generateUrl('home_display'));
}
}
return $this->render('EMMyFriendsBundle:Home:filtered_home.html.twig', array(
'friends' => $friends, 'category' => $cat->getName()->getName(), 
'dd_form' => $dd_form->createView()));   
}
return $this->redirect($this->generateUrl('home_display'));
}
}

其中init()设置$this->em = $this->getDoctrine()->getEntityManager();$this->user = $this->get('security.context')->getToken()->getUser();

我收到一个错误:Expected argument of type string, object given,但不知道它来自哪里以及如何修复。有什么帮助吗?我们将不胜感激。我认为它发生在$dd_form->bindRequest($request);这一行,因为当我把exit;放在它之前时,程序就会退出,但当我把它放在它之后时,它会显示错误。

另一件事是,当我删除时

@AssertMinLength(
*         limit=3,
*         message="The name is too short." )

它位于Category.php中的name属性之上,一切都很完美。

任何想法都会有所帮助!谢谢你抽出时间!

还有一件事——为什么要获得我应该使用$cat->getName()->getId();的类别的id?我原以为应该只有$cat->getId(),但事实并非如此。

当您使用'entity'字段类型时,您从中得到的是一个实体,因此将此字段称为"name"是没有意义的。所以我会把你的建设者改为:

$builder->add('category', 'entity', array(
...

然后,您不需要将Category对象传递给这个表单生成器。这通常是在表单将每个字段与对象属性连接时完成的,但事实并非如此。您没有使用此表单来获取category对象的"category"属性。这只是一个将向我们返回Category对象的表单,您可以从表单数据中获得该对象。因此,将您的控制器更改为:

public function filterAction(Request $request)
{
$this->init();
$dd_form = $this->createForm(new ChooseCatType($this->user));
if($request->getMethod() == 'POST') { 
$dd_form->bindRequest($request);
if ($dd_form->isValid()) {   
$data = $form->getData();
$cat = $data['category'];
$filter = $cat->getId();
$friends = $this->em->getRepository('EMMyFriendsBundle:Friend')
->filterFriends($filter);
if(!$friends) {
$this->setFlash('There are no frieds in this category.', 'error');
return $this->redirect($this->generateUrl('home_display'));
}
return $this->render('EMMyFriendsBundle:Home:filtered_home.html.twig', array(
'friends' => $friends, 'category' => $cat->getName(), 
'dd_form' => $dd_form->createView()));   
}
}
return $this->redirect($this->generateUrl('home_display'));
}
}

相关内容

  • 没有找到相关文章

最新更新