如何定义学说中的 nm 关系?
如果有一个表“文章”和一个表“标签”。文章可以有多个标签,标签可以挂在多篇文章上。
BaseArticle 类如下所示:
abstract class BaseArticle extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('article');
$this->hasColumn('article_id', 'integer', 8, array(
'type' => 'integer',
'length' => 8,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('title', 'string', null, array(
'type' => 'string',
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('text', 'string', null, array(
'type' => 'string',
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
$this->hasColumn('url', 'string', 255, array(
'type' => 'string',
'length' => 255,
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
}
public function setUp() {
parent::setUp();
$this->hasMany('Tag as Tags', array( 'local' => 'article_id',
'foreign'=>'tag_id',
'refClass'=>'Articletag')
);
}
}
BaseTag 类如下所示:
abstract class BaseTag extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('tag');
$this->hasColumn('tag_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('name', 'string', null, array(
'type' => 'string',
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
}
public function setUp() {
parent::setUp();
$this->hasMany('Article as Articles', array( 'local' => 'tag_id',
'foreign'=>'article_id',
'refClass'=>'Articletag')
);
}
}
关系类如下所示:
abstract class BaseArticletag extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('articletag');
$this->hasColumn('article_id', 'integer', 8, array(
'type' => 'integer',
'length' => 8,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => false,
));
$this->hasColumn('tag_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => false,
));
}
public function setUp()
{
parent::setUp();
}
}
当我尝试从文章中获取属性时,使用以下方法一切顺利:
$article = Doctrine_Query::create()->from('Article a')
->where('id = ?' , 1)
->fetchOne();
echo $article->title; //gives me the title
但是当我尝试这样做时:
foreach($article->Tags as $tag) {
echo($tag->name)
}
我收到错误:
Unknown record property / related component "Tags" on "Article"
If got a table "Article" and a table "Tags". Articles can have multiple tags and tags can hang to multiple articles.
The class BaseArticle looks like this:
abstract class BaseArticle extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('article');
$this->hasColumn('article_id', 'integer', 8, array(
'type' => 'integer',
'length' => 8,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('title', 'string', null, array(
'type' => 'string',
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('text', 'string', null, array(
'type' => 'string',
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
$this->hasColumn('url', 'string', 255, array(
'type' => 'string',
'length' => 255,
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
}
public function setUp() {
parent::setUp();
$this->hasMany('Tag as Tags', array( 'local' => 'article_id',
'foreign'=>'tag_id',
'refClass'=>'Articletag')
);
}
}
The BaseTag-class like this:
abstract class BaseTag extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('tag');
$this->hasColumn('tag_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('name', 'string', null, array(
'type' => 'string',
'fixed' => false,
'unsigned' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
}
public function setUp() {
parent::setUp();
$this->hasMany('Article as Articles', array( 'local' => 'tag_id',
'foreign'=>'article_id',
'refClass'=>'Articletag')
);
}
}
And the relationship class like this:
abstract class BaseArticletag extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('articletag');
$this->hasColumn('article_id', 'integer', 8, array(
'type' => 'integer',
'length' => 8,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => false,
));
$this->hasColumn('tag_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'fixed' => false,
'unsigned' => false,
'primary' => true,
'autoincrement' => false,
));
}
public function setUp()
{
parent::setUp();
}
}
When I try to get a property from the article all goes well by using:
$article = Doctrine_Query::create()->from('Article a')
->where('id = ?' , 1)
->fetchOne();
echo $article->title; //gives me the title
But when I try this:
foreach($article->Tags as $tag) {
echo($tag->name)
}
I get an error:
Unknown record property / related component "Tags" on "Article"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了设置多对多关系,您必须将关系放在关联表中,而不是连接表中,如下所示:(简化)
然后您将能够执行诸如 $article->Tags 之类的操作。
更多信息请参见 教义文档。
For setting-up many-to-many relations, you have to put the relations in the associated tables, not the join table, like so : (simplified)
Then you will be able to do things such as $article->Tags.
More info in the Doctrine Documentation.
事实证明,教义或我对关系的定义没有任何问题。问题的原因是项目中已经包含了一个名为“Tag”的类。重命名该类后,教义关系工作得很好:-)
It turns out that there was nothing wrong with Doctrine or with my definition of the relation. The cause of the problem was that there was already a class named "Tag" included in the project. After renaming that class, the doctrine-relation worked just fine :-)