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

  • Home
  • SEARCH
  • 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 6236357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:50:27+00:00 2026-05-24T10:50:27+00:00

I have two tables: Articles and Categories. Articles can have a single Category assigned

  • 0

I have two tables: Articles and Categories. Articles can have a single Category assigned to them. But they don’t have to have a Category.

Schema:

Article:
  columns:
    title:
      type: string(255)
    content:
      type: string(255)
    category_id:
      type: integer(4)

Category:
  columns:
    name:
      type: string(255)
    article_id:
      type: integer(4)
  relations:
    Article:
      class: Article
      local: article_id
      foreign: id
      foreignAlias: ArticleCategories

I can query for all Articles that have categories assigned to them like this:

$articles= Doctrine_Query::create()
  ->from('Article a')
  ->leftJoin('a.Category c ON c.article_id = a.id')
  ->where('c.id > 0')
  ->execute();

It returns this:

Object->Array
(
  [0] => Array
  (
    [id] => string(1) "1"
    [title] => string(4) "test"
    [content] => string(4) "test"
    [Category] => Array
    (
      [0] => Array
      (
        [id] => string(1) "2"
        [name] => string(7) "testing"
      )
    )
  )
etc...

What I need to do is query for Articles where there is no Category relationship. I can’t just say ->where('c.id = NULL') either because if there is no Category relationship, then there isn’t any [Category] array returned in the object. It only returns the id, title and content. Also I can’t say ->where(a.Category = NULL) because Category isn’t a column of Article.

Any ideas?

UPDATE
I made a mistake on the Schema and updated it. I know it doesn’t really make sense for an Category to only have a relationship with a single Article, but in reality I’m not using Articles/Categories. I was just using those terms as examples.

  • 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-24T10:50:27+00:00Added an answer on May 24, 2026 at 10:50 am

    UPDATE:

    So the easiest way if you want the article as the primary object is to do a leftJoin with a condition for the fk being null. LEFT JOINs always grabs the record on the left side of the join regardless of whether the right side of the join has a corresponding record. So without the where you essentially get a result of all articles. So we can then filter those for only articles that DONT have a category by using the where condition… very similar to before:

    $articles = Doctrine_Query::create()
      ->from('Article a')
      ->leftJoin('a.Category c')
      ->where('c.article_id IS NULL')
      ->execute();
    

    There is no reason to specify an on condition. Doctrine will figure this out base on the realtionship. Additionally you dont need to use a where for this type of filtereing use an innerjoin instead, the inner join will only select itemes where the relationship exists (i.e. there is a a.category_id = c.id) so the query you posted should actually be:

    $articles = Doctrine_Query::create()
      ->from('Article a')
      ->innerJoin('a.Category c')
      ->execute();
    

    To get the articles without any category you can look for a category_id of null on the article:

    $articles= Doctrine_Query::create()
      ->from('Article a')
      ->leftJoin('a.Category c')
      ->where('a.category_id IS NULL')
      ->execute();
    

    Id probably remove the join though because its not really necessary, unless you need the null columns in the result for some reason.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I have a database with two tables: categories and articles . Every
I have two tables -- an article table and a vote table. Users can
I have two tables, like this: #Articles: ID | Title 1 Article title 2
I have two tables. One is article and the other structure. And the articles
I have two tables: articles and articletags articles: id, author, date_time, article_text articletags: id,
I have two tables, news and news_views. Every time an article is viewed, the
Here i need help with joins. I have two tables say articles and users.
I have following two tables: 1.) Articles - [ArticleID] 2.) ArticleComments - [CommentID], [ArticleID]
I have two tables, articles and logs . When new record is inserted into
I have two tables in my database. cat - catid, catname articles - id,

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.