简体   繁体   中英

Zend Framework join

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

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');

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