Cau, snazim sa urobit hromadny update, ale neviem aki ako zacat, nevies mi poradit? Mam Tabulku Brand

class Brand extends Object
{
    /**
     * @ORM\OneToMany(targetEntity="ServiceContract", mappedBy="brand", cascade={"persist"}, fetch="EXTRA_LAZY")
     * @var ArrayCollection|ServiceContract[]
     */
    protected $serviceContract;
}

class ServiceContract extends Object
{
    /**
     * @ORM\ManyToOne(targetEntity="Brand", inversedBy="serviceContract", cascade={"persist"})
     * @ORM\JoinColumn(name="brands_id")
     * @var Brand
     */
    protected $brand;
}

Chcem vytiahnut vsetky Brand $markListData nasledne z kazdej Brand vytiahnut ServiceContract zmenit jej nove Brand na $brandEntity a ulozit. Toto sice funguje, ale nie je to pekne kedze pracujem s kazdou Entitou samozstatne a ked ich je nejakych 1000… :-), Ako by sa dalo hromadne UPDATE ServiceContract SET brands_id = 'mn' WHERE brands_id='xy' ? pricom 'mn' == $brandEntity = new Brand();

$markListData = array(1,3,5,8,7,12);
$brandsForMerge  = $this->brandsFacade->getDao()->findBy(array('id IN' => $markListData));

$brandEntity = new Brand();

/** @var Brand $brandForMerge */
foreach ($brandsForMerge as $brandForMerge) {

    $serviceContractList = $brandForMerge->getServiceContract()->getIterator();

    /** @var ServiceContract $serviceContract */
    foreach ($serviceContractList as $serviceContract) {
        $serviceContract->setBrand($brandEntity);
        $this->serviceContractsFacade->save($serviceContract);
    }
    //$this->brandsFacade->delete($brandForMerge);
}

Ahoj, co nějak takhle? :)

$newBrand = new Brand();
$em->persist($newBrand)->flush();

$em->getDao(ServiceContract::class)
    ->createQueryBuilder('sc')
    ->update()->set('sc.brand', $newBrand->getId())
    ->andWhere('sc.brand = :oldBrand', $oldBrandId)
    ->execute();

$em->clear();

Toto uz vyzera pekne, Dakujem ;-)


You must first log in to participate in this discussion