简体   繁体   中英

how to: findByProperty on two fields in typo3 v4.5.30?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM