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.