activerecord - why did php yii2 active record query giving duplicates?
Get the solution ↓↓↓I'm developinga a classifieds portal where user can post their ads. We have a option to chat with user for ads.
I store chats in a table called "tb_inbox" it looks like this Screenshot of Inbox table
I have a area of my chats there i want to show the ads user chatting to. user will be sender or receiver. but based on ad_id i want to list the ads in my chat.
But now it shows many duplicates with my query. it shows same ads repeatedly for each message
my code
$messages=Inbox::find()->where(['sender_id'=>$user_id])
->orWhere(['recipient_id'=>$user_id])
->orderBy(['id'=>SORT_DESC])
->all();
How I can show ads what I'm receiving and sending based on ad_id? The user will be sender or receiver
thanks
Answer
Solution:
try with one() or distinct()
$messages=Inbox::find()->where(['sender_id'=>$user_id])
->orWhere(['recipient_id'=>$user_id])
->orderBy(['id'=>SORT_DESC])
->one();
$messages=Inbox::find()->where(['sender_id'=>$user_id])
->orWhere(['recipient_id'=>$user_id])
->orderBy(['id'=>SORT_DESC])
->distinct()->all();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: use the option --with-all-dependencies (-w) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.