Reputation: 183
I want to update the field of another entity , when an update is made to an entity.
I keep on " last_buy " the last date of purchase.
I want from my query to the repository and the data obtained , the method setLastBuy information is saved with the last purchase Offers entity.
It is that my ' Buy ' entity store modification date of the date of the last purchase of the entity offers.
But when I update my entity offers , I get the error:
Error: Call to a member function setLastBuy() on a non-object
namespace MyAppBundle\AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use MyAppBundle\AppBundle\Entity\Offers;
class UpdateModified
{
public function postUpdate(LifecycleEventArgs $args){
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
if ($entity instanceof Offers) {
$offers = $entityManager->getRepository('MyAppBundle:Buy')->findOffersByBuy($entity);
$last_buy = $entity->getUpdatedAt();
foreach($offers as $updateDate){
$updateDate->setLastBuy($last_buy);
$entityManager->persist($updateDate);
}
$entityManager->flush();
}
}
Upvotes: 0
Views: 85
Reputation: 3697
It seems that the variable $offers is an array but not an array of Buy Objects. So i bet that you did something wrong in your BuyRepository class. Maybe you forgot to call the getResult() function or something like that.
Upvotes: 0
Reputation: 1025
First thing I would recommend is to write your own EventSubscriber for your Offers Entity and then dispatch your Buy entity on Update, delete, etc.
Secondly:
if ($entity instanceof Offers) { //$entity is of type Offers
$offers = $entityManager->getRepository('MyAppBundle:Buy')->findOffersByBuy($entity);
//Offers above is empty array!
//findOfferByBuy() where passed entity is not a Buy object will return empty resultset
$last_buy = $entity->getUpdatedAt();
foreach($offers as $updateDate){
//$pdateDate is null
$updateDate->setLastBuy($last_buy); //throw error
$entityManager->persist($updateDate);
}
$entityManager->flush();
}
Upvotes: 1