How can i do update table doctrine

2019-09-26 01:37发布

问题:

I have this script for inserting the data:

echo "Update ";
$department = new Department();
$department->setNamedepartment($data ['nameDepartment']);
$department->setCodedepartment($data ['codeDepartment']);
$department->setIdfkgeographicalarea($data ['idFkGeographicalArea']);
$entityManager->persist($department);
$entityManager->flush();

how can I update my $department entity with only this call: update($data,$id) ?

回答1:

function update ($data, $id) use ($entityManager) {
    $departament = $entityManager->find('Your\EntityNamespace\Departament', $id);

    $department->setNamedepartment($data['nameDepartment']);
    $department->setCodedepartment($data['codeDepartment']);
    $department->setIdfkgeographicalarea($data['idFkGeographicalArea']);

    $entityManager->flush();
}