2

I have a custom module to store customer relationship, with database structure like this:

|entity_id|first_customer|second_customer|

entity_id => the relationship id
first_customer => customer id
second_customer => customer id

if the customer has a relationship with another customer, each of their customer id will fill first_customer and second_customer, i want to check if the relationship already exist for given 2 customer id, for example

+---------+--------------+---------------+
|entity_id|first_customer|second_customer|
+---------+--------------+---------------+
|    1    |      1       |     35        |
|    2    |      3       |      1        | 
|    2    |      4       |      2        | 
|    2    |      2       |      6        | 
+---------+--------------+---------------+

public function validate($customerIdA,$customerIdB){
  $friend = $this->friendFactory->create()->getCollection()
             ->addFieldToFilter(????)->getFirstItem();
  if(!empty($friend->getId())) return true;
  else return false;
}

//the order of the parameter is not gonna be the order of the filter
$this->validate(1,35); //true
$this->validate(35,1); //true
$this->validate(1,2); //false
$this->validate(1,3); //true
$this->validate(4,2); //true
0

The previous answer is not entirely correct. Our suggestion is to use this:

$friend = $this->friendFactory
                ->create()
                ->getCollection()
                ->addFieldToFilter([
                    ['attribute' => 'first_customer', 'eq' => $customerIdA],
                    ['attribute' => 'second_customer', 'eq' => $customerIdB]
                ])
        ->getFirstItem();
-1

you can try this

$friend = $this->friendFactory->create()->getCollection()
->addFieldToFilter(['first_customer', 'second_customer'],
[
   ['eq' => $customerIdA],
   ['eq' => $customerIdB]
]
)->getFirstItem();;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.