Symfony - Doctrine Type数组

时间:2018-06-05 08:20:02

标签: php symfony entity

我的表单有问题,问题出在我的实体上。我收到了一个错误:

  

无法将数据库值“”转换为Doctrine Type数组

问题在于$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发送parms时,我收到了我提到的错误。

1 个答案:

答案 0 :(得分:0)

如果您想在CardUser之间建立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控制台命令:

php bin/console doctrine:generate:entities AppBundle:Card
php bin/console doctrine:generate:entities AppBundle:User

这将为您生成所有需要的方法。