【发布时间】:2011-08-11 17:47:15
【问题描述】:
我有两个表:文章和类别。文章可以分配给它们一个类别。但他们不必有一个类别。
架构:
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
我可以像这样查询分配了类别的所有文章:
$articles= Doctrine_Query::create()
->from('Article a')
->leftJoin('a.Category c ON c.article_id = a.id')
->where('c.id > 0')
->execute();
它返回这个:
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...
我需要做的是查询没有类别关系的文章。我也不能只说->where('c.id = NULL'),因为如果没有Category 关系,那么对象中就不会返回任何[Category] 数组。它只返回id, title and content。我也不能说->where(a.Category = NULL),因为Category 不是Article 的一列。
有什么想法吗?
更新 我在架构上犯了一个错误并更新了它。我知道类别仅与单个文章有关系并没有真正意义,但实际上我没有使用文章/类别。我只是以这些术语为例。
【问题讨论】:
标签: php mysql symfony1 doctrine