【发布时间】:2018-07-27 09:38:17
【问题描述】:
我正在创建一个 API REST,我想为 $userInterest 设置一个名为 Category 的实体(多对一)我为 User 实体做了同样的事情(并且我设置了“Profile”实体),但是 Profile 实体作为参数传递,并且它是在此函数中创建的,因此它很简单并且可以正常工作。
我是这样做的:
/**
*
* @Rest\Post(
* path = "/users/register",
* name = "api_users_add"
* )
* @Rest\View(StatusCode=201, serializerGroups={"user_detail"})
* @ParamConverter(
* "user",
* converter="fos_rest.request_body",
* options={"deserializationContent"={"groups"={"Deserialize"}}},
* )
* @ParamConverter(
* "profile",
* converter="fos_rest.request_body",
* options={"deserializationContent"={"groups"={"Deserialize"}}},
* )
* @ParamConverter(
* "userInterest",
* converter="fos_rest.request_body",
* options={"deserializationContent"={"groups"={"Deserialize"}}},
* )
*/
public function postUserAction(Request $request, User $user, Profile
$profile, UserInterest $userInterest) {
$profile->setLastConnexion(new \DateTime('now'));
$profile->setCreatedAccount(new \DateTime('now'));
$user->setIdProfile($profile);
$em = $this->getDoctrine()->getManager();
$em->persist($profile);
$em->flush();
$this->encodePassword($user);
$user->setRoles([User::ROLE_USER]);
$this->persistUser($user);
}
-
所以前面的例子工作得很好,但是当我尝试为 $userInterest 设置 Category 实体时不起作用(也已经创建了类别,它就像一个“静态”表 (
$em_category = $this->getDoctrine()->getManager();
$em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category'))));
$userInterest->setCategory($em_category);
$em = $this->getDoctrine()->getManager();
$em->persist($userInterest);
$em->flush();
但这会导致错误:
“关联>字段“AppBundle\Entity\UserInterest#$category”的“AppBundle\Entity\Category”类型的预期值,改为>“Doctrine\ORM\EntityManager”。”
如何设置实体类别而不是实体管理器...?
发送的 JSON(如果有帮助的话):
{
"username": "Usertest",
"password": "strenghtpassword",
"email": "thxforall@symfony.com",
"birth": "1999-04-26T18:25:43-05:00",
"content": "I like Netflix",
"id_category": "1"
}
在类别表中 ID:1 作为内容“你喜欢什么”例如。
【问题讨论】:
-
Unknown column 't0.content_c' in 'field list'. -
我不明白错误我没有归档列表...
-
“SQL”部分并没有告诉你它与数据库有关,而你试图获取一个不存在的字段?
-
我刷新了 sql 数据库,错误消失了,但现在我有了:关联字段“AppBundle\Entity\UserInterest#$category”的类型为“AppBundle\Entity\Category”的预期值,得到了“Doctrine\ ORM\EntityManager" 代替。
-
如何将实体设置为 userinterest 而不是 entitymanager?
标签: php json api symfony symfony-3.4