我有朋友和類別 - 每個朋友都有一個類別。在主頁上可以看到所有朋友的列表,並且有一個包含所有類別和提交按鈕的下拉菜單 - 當您選擇類別並單擊它時,您只會看到來自該類別的朋友。期望的參數類型字符串,給定的對象 - Symfony2
這是Category.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;
}
}
ChooseCatType.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';
}
}
,這是控制器的動作:
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.")
這是上面Category.php name屬性,一切都運行完美。
任何想法都會有所幫助!謝謝你的時間!
還有一件事 - 爲什麼要得到我應該使用的類別的id $cat->getName()->getId();
?我認爲它應該只有$cat->getId()
,但事實並非如此。
真的很奇怪你需要使用$ cat-> getName() - > getId();.這不應該。如果你使用$ cat-> getId(),你會得到什麼? –
啊,我知道什麼是錯的。看到回答 –
@Carlos格拉納多斯,我得到'getId()'沒有在對象或類似的東西上調用。 – Faery