Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:17:32+00:00 2026-05-28T08:17:32+00:00

I have a relational record similar to the following: There are users (table users)

  • 0

I have a relational record similar to the following:

  • There are users (table users) with ids
  • There are categories (table categories) with id, name
  • There are articles (table articles) with id, body, category_id
  • Then there is a table read_articles with article_id, user_id

So a user can see a list of all articles in a category. They can click on an article and it will add an entry (user_id, article_id) to the read_articles table.

I want to be able to use Yii’s ActiveRecord to get all articles from a given category that have not been read by the current user and display those. I’ve been thinking about it in terms of SQL a little bit but I’m not sure how to fit it into my ActiveRecord setup. My first idea was a parameterized scope on the Article active record:

public function unread( $userId )
{
    $this->getDbCriteria()->mergeWith( array(
        'alias' => 'articles',
        'condition' => 'not exists (SELECT * '
            . 'FROM user_read_articles '
            . 'WHERE articles.id=user_read_articles.article_id '
            . 'AND read_articles.user_id=' . (int)$userId
            . ')',
    ) );
}

It seems to be working, but it feels really dirty to me.

Is there a cleaner way to do what I’m talking about in ActiveRecord? Or should I maybe consider moving toward more plain SQL and handling things like this myself (but lose a lot of nice feature of AR)?

Edit Here are the relations for the above-mentioned models: (Disclaimer, these are truncated to only relevant parts)

UserActiveRecord

public function relations()
{
    return array(
        'categories' => array(
            self::HAS_MANY,
            'UserCategoryActiveRecord',
            'user_id' ),
        // I added this early using gii, never really used it...
        'readArticles' => array(
            self::HAS_MANY,
            'UserReadArticleActiveRecord',
            'user_id' ),
    );
}

UserCategoryActiveRecord

public function relations()
{
    return array(
        'user' => array(
            self::BELONGS_TO,
            'UserActiveRecord',
            'user_id' ),
        'articles' => array(
            self::HAS_MANY,
            'ArticleActiveRecord',
            'category_id',
            'order' => 'articles.pub_date DESC' ),
    );
}

ArticleActiveRecord

public function relations()
{
    return array(
        'category' => array(
            self::BELONGS_TO,
            'CategoryActiveRecord',
            'category_id' ),
    );
}

I have also made a “UserReadArticleActiveRecord” early as I was building this, but I never used that model and it is pretty much just empty.

In all honesty, I’ve been thinking of moving to Symfony2 and Doctrine anyway and just ditching active record altogether. I know it doesn’t solve this problem, but I might just drop this and keep my current solution for the time being.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:17:33+00:00Added an answer on May 28, 2026 at 8:17 am

    To get the read articles, you can define a MANY_MANY relationship on your User model as described here:

    http://www.yiiframework.com/doc/guide/1.1/en/database.arr

    I don’t think you’ll need the proxy model UserReadArticles, you just need to define the relationship properly.

    To get unread articles, I’m also having trouble thinking about the Yii way to do it, but a more efficient way than using a sub-select query is to do a LEFT JOIN to user_read_articles and check if a column from user_read_articles IS NULL (there was no matching row) something like this:

    $this->getDbCriteria()->mergeWith( array(
        'alias' => 'articles',
        'join' => 'LEFT JOIN user_read_articles ON articles.id=user_read_articles.article_id',
        'condition' => 'user_read_articles.article_id IS NULL',
    ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relational table containing a foreign key in which there is no
I have the following requirements I have relational content stored in a SQL Server
I have a SQL DB that contains multiple relational tables. There are some fields
I have an audit table in SQL server. It is to record an entry
I want to convert a table storing in Name-Value pair data to relational form
Can't seem to find a solution to this one. I have a relational table,
I have: @method Users setMail() Sets the current record's mail value @method Users setUsersGroups()
I have a table called posts with the following struture: post_id | post_body |
Lets say you have a relational DB table like INVENTORY_ITEM. It's generic in the
I have a relational database with about 100 tables. Each table has unique, numerical,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.