Reputation: 483
I want to search for records which match on two fields with two supplied objects, for example
$cnt = $this->usedCouponRepository->findByUser($validvip)->toArray() ;
$cnt2 = $this->usedCouponRepository->findByCoupon($validcoupon)->toArray() ;
get interesection, (find how many times validvip pairs up with validcoupon in the usedcoupon table) a call like this would be it -
$cnt3 = $this->usedCouponRepository->findByUserAndCoupon($validcoupon,$validuser);
is there a magic function to do this or some other efficient way? I'd hate to just loop over the first looking for a match in code. Thanks
Upvotes: 0
Views: 226
Reputation: 7016
Nope, there is no "extbase magic" doing this job for you. You have to implement it yourself. But lucky you, its not that hard. Just add a method to your repository like this one:
public function findByUserAndCoupon($validcoupon, $validuser) {
$query = $this->createQuery();
$query->matching(
$query->logicalAnd(
$query->equals('user', $validuser),
$query->equals('coupon', validcoupon)
)
);
$result = $query->execute();
return $result;
}
Now you can call this like you tried before. The strings 'user'
and 'coupon'
in the logicalAnd Statement have to be the fieldnames of the database.
Upvotes: 2