Zend Framework join

2019-09-15 23:21发布

I've been struggle for hours with the follow join issue in Zend Framework.

My table(s)

  • Table (websites)

    • id
    • user_id
    • website
    • url
    • ...
  • Table (users)

    • id
    • username
    • salt
    • password
    • ...
  • Table (reviews)

    • id
    • website_id (id of website)
    • user_id (id from the user/owner of the website)
    • reviewer_id (id from the user who has reviewed the website)
    • review
    • ...

What do I want to get..
To make a array with join of the review and user data is no problem, but I want also add the website compare to the review.

I made the follow join but I won't work like I want, I have just 3 test reviews in my database and I'm getting over 12 results in my array.

My query:

$select = $this->_db->select() ->from('reviews') ->joinLeft('users', 'reviews.reviewer_id = users.id') ->joinLeft('websites', 'reviews.user_id = reviews.user_id') ->where("reviews.user_id = $user_id");

$result = $this->getAdapter()->fetchAll($select);

With kind regards,

Nicky

1条回答
Root(大扎)
2楼-- · 2019-09-15 23:45

Try adding a groupBy to your query (untested)

$select = $this->_db->select()->from('reviews')
                              ->joinLeft('users', 'reviews.reviewer_id = users.id')
                              ->joinLeft('websites', 'reviews.user_id = reviews.user_id')
                              ->where("reviews.user_id = $user_id")
                              ->group('reviews.id');
查看更多
登录 后发表回答