如何在同一个Doctrine2对象上正确插入或更新

Rey*_*rPM -1 php symfony doctrine-orm symfony-2.6

我有这段代码:

$entity = $em->getRepository('AppBundle:Representative')->find($soqlObj1['records'][0]['Id']);

if ($entity === null) {
    $entity = new Representative();
    $em->persist($entity);
}

// we set the values from veeva
$entity->setVeevaRepId($soqlObj1['records'][0]['Id']);
$entity->setEmail($soqlObj1['records'][0]['Email']);
...

$em->flush();
Run Code Online (Sandbox Code Playgroud)

这是实体的一部分Representative

class Representative
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="string", length=45, nullable=false, unique=true)
     * @ORM\GeneratedValue()
     * @Expose()
     */
    protected $veeva_rep_id;

    /**
     * @var string
     * @ORM\Column(type="string", length=45)
     * @Expose()
     */
    protected $display_name;

    /**
     * @var string
     * @ORM\Column(type="string", length=255)
     * @Expose()
     */
    protected $avatar_url = 'https://pdone.s3.amazonaws.com/avatar/default_avatar.png';

    /**
     * @var string
     * @ORM\Column(type="string", length=45)
     * @Expose()
     */
    protected $rep_type = "VEEVA";

    /**
     * @var string
     * @ORM\Column(type="string", length=45)
     * @Expose()
     */
    protected $username;

    /**
     * @var string
     * @ORM\Column(type="string", length=45)
     * @Expose()
     */
    protected $first;

    /**
     * @var string
     * @ORM\Column(type="string", length=45)
     * @Expose()
     */
    protected $last;

    /**
     * @var string
     * @ORM\Column(type="string", length=45, nullable=true)
     * @Expose()
     */
    protected $title;

    /**
     * @var string
     * @ORM\Column(type="text", nullable=true)
     */
    protected $bio;

    /**
     * @var string
     * @ORM\Column(type="string", length=45, nullable=true)
     * @Expose()
     */
    protected $phone;

    /**
     * @var string
     * @ORM\Column(type="string", length=45)
     * @Expose()
     */
    protected $email;

    /**
     * @var bool
     * @ORM\Column(type="boolean")
     * @Expose()
     */
    protected $inactive = false;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime", nullable=true)
     * @Expose()
     */
    protected $lastLoginAt;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime")
     * @Expose()
     */
    protected $lastSyncAt;

    /**
     * @var Territory
     * @ORM\ManyToOne(targetEntity="Territory")
     * @ORM\JoinColumn(name="territories_id", referencedColumnName="veeva_territory_id")
     * @Expose()
     */
    protected $territory;

    /**
     * @var string
     * @ORM\Column(type="string", nullable=true, length=150)
     */
    protected $repTokenId;

    ...
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:在数据库级别还需要其他一些列(在实体级别,nullable = false):如果该对象在数据库中不存在,它将根据我编写的代码创建或更新,或者我需要移动每个必填字段变成有条件的?哪个是实现此目标的正确方法?我正在尝试INSERT|UPDATE仅基于一个查询结果

Don*_*sto 5

如果您从db中检索到对象,则说教义本身会知道下一个操作是更新还是插入:您不必担心任何事情

您的答案是有效的,但我将对其进行如下修改

$entity = $em->getRepository('AppBundle:Representative')->find($soqlObj1['records'][0]['Id']);

if ($entity === null) {
    $entity = new Representative();
}

// we set the values from veeva
$entity->setVeevaRepId($soqlObj1['records'][0]['Id']);
$entity->setEmail($soqlObj1['records'][0]['Email']);

$em->persist($entity);
$em->flush();
Run Code Online (Sandbox Code Playgroud)

正如我告诉您的那样,您不必担心插入或更新,教义将为您完成。但是,如果仅当对象是新对象或从数据库中获取对象时才需要设置某些值,只需在$entity === null控制下添加适当的代码即可