我的表单有问题,而问题出在我的实体。我得到一个错误:
无法将数据库值"“转换为规则类型数组
问题出在$card
字段上。
public function addNewUser ($id, $firstName, $lastName)
{
$termUsers = $this->getUserRepository()
->createQueryBuilder('nu')
->select('nu')
->where('nu.id = :id')
->setParameter('id', $id)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
if (empty($termUsers)) {
$termUsers = new User();
$termUsers->setId($id);
$termUsers->setFirstName($firstName);
$termUsers->setLastName($lastName);
$this->em->persist($termUsers);
$this->em->flush();
}
return $termUsers;
}
public function getUserAddAction()
{
$this->requirePostParams(['id', 'firstName', 'lastName']);
$this->get('lottoland.user')->addNewUser(
$this->data['id'],
$this->data['firstName'],
$this->data['lastName']
);
return $this->success();
}
当我通过Postman发送参数时,我得到了我提到的错误。
发布于 2018-06-05 16:29:59
如果您想在Card
和User
之间建立oneToMany关系,那么:
# Entity/Card.php
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="card")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
# Entity/User.php
/**
* @ORM\ManyToOne(targetEntity="Card", inversedBy="users")
* @ORM\JoinColumn(name="card_id", referencedColumnName="id", nullable=false)
*/
private $card;
对于初学者来说,创建这种关系的一种更方便的方法是,像我上面做的那样声明关系,然后,要生成getter和setter,请使用方便的symfony console命令:
php bin/console doctrine:generate:entities AppBundle:Card
php bin/console doctrine:generate:entities AppBundle:User
这将为您生成所有需要的方法。
https://stackoverflow.com/questions/50695406
复制相似问题