gpt4 book ai didi

php - 字符串类型的预期参数,给定对象 - Symfony2

转载 作者:行者123 更新时间:2023-12-04 05:31:41 24 4
gpt4 key购买 nike

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

这是分类.php :

<?php

namespace EM\MyFriendsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
* EM\MyFriendsBundle\Entity\Category
* @ORM\Entity
* @ORM\Table(name="categories")
* @ORM\Entity(repositoryClass="EM\MyFriendsBundle\Entity\SameRepository")
*/
class Category
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string", length=100)
* @Assert\NotBlank()
* @Assert\MinLength(
* limit=3,
* message="The name is too short." )
*/
protected $name;

/**
* @ORM\Column(type="string", length=225, nullable=true)
*/
protected $description;

/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="categories")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;

/**
* @ORM\OneToMany(targetEntity="Friend", mappedBy="category")
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $friends;

public function __construct()
{
$this->friends = new \Doctrine\Common\Collections\ArrayCollection();
}

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(\EM\MyFriendsBundle\Entity\User $user)
{
$this->user = $user;
}

public function getUser()
{
return $this->user;
}

public function addFriend(\EM\MyFriendsBundle\Entity\Friend $friend)
{
$this->friends[] = $friend;
}

public function getFriends()
{
return $this->friends;
}
}

选择CatType.php :
<?php

namespace EM\MyFriendsBundle\Entity;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\AbstractType;

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';
}
}

这是 Controller 的 Action :
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;在它之前程序退出,但是当我把它放在它之后时,它显示了错误。

另一件事是,当我删除
@Assert\MinLength(
* limit=3,
* message="The name is too short." )

它位于 中的 name 属性上方分类.php ,一切正常。

任何想法都会有所帮助!感谢您的时间!

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

最佳答案

当您使用“实体”字段类型时,您从中得到的是一个实体,因此将该字段称为“名称”是没有意义的。因此,我会将您的构建器更改为:

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

然后,您不需要将 Category 对象传递给此表单构建器。这通常在表单将每个字段与对象属性连接时完成,但事实并非如此。您没有使用此表单来获取 Category 对象的“category”属性。这只是一个表单,它将返回一个 Category 对象,您可以从表单数据中获取该对象。因此,将您的 Controller 更改为:
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'));
}
}

关于php - 字符串类型的预期参数,给定对象 - Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com