问题描述
我正在使用Laravel 4.我的系统中有很多关系。而我选择使用Wordpress分类表方案。但是,如何使模型与Laravel 4雄辩的ORM关系?这是我的数据库表;
表术语
:
+ ------------ + --------------------- + --- --- + ----- + --------- + ---------------- +
| Field |类型|空|关键|默认|额外|
+ ------------ + --------------------- + ------ + --- - + --------- + ---------------- +
| term_id | bigint(20)unsigned | NO | PRI | NULL | auto_increment |
|名称| varchar(200)| NO | MUL | | |
| s lug | varchar(200)| NO | UNI | | |
+ ------------ + --------------------- + ------ + --- - + --------- + ---------------- +
表 term_taxonomy
:
------------------ + --------------------- + ------ + - --- + --------- + ---------------- +
| Field |类型|空|关键|默认|额外|
+ ------------------ + --------------------- + ---- - + ----- + --------- + ---------------- +
| term_taxonomy_id | bigint(20)unsigned | NO | PRI | NULL | auto_increment |
| term_id | bigint(20)unsigned | NO | MUL | 0 | |
|分类| varchar(32)| NO | MUL | | |
|描述| longtext | NO | | NULL | |
|父母| bigint(20)unsigned | NO | | 0 | |
|数| bigint(20)| NO | | 0 | |
+ ------------------ + --------------------- + ---- - + ----- + --------- + ---------------- +
表 term_relationships
:
code> + ------------------ + --------------------- + --- --- + ----- + --------- + ------- +
| Field |类型|空|关键|默认|额外|
+ ------------------ + --------------------- + ---- - + ----- + --------- + ------- +
| object_id | bigint(20)unsigned | NO | PRI | 0 | |
| term_taxonomy_id | bigint(20)unsigned | NO | PRI | 0 | |
| term_order | int(11)| NO | | 0 | |
+ ------------------ + --------------------- + ---- - + ----- + --------- + ------- +
通常我们可以做 return $ this-> belongsToMany('Term');
但是我们如何做2关系?在找到与taxonomy_id相关的术语关系后,我们需要2个关系首先从term_taxonomy表中查找术语分类。
还有一个我想要使用的例子; p>
$ categories = Post :: find(1) - >类别; // get terms with taxonomy =post_category
$ tags = Post :: find(1) - > tags; //获取术语与taxonomy =post_tag
我不想这样做与基本数据库class DB :: table('table') - > join('...')...
我想使用Eloquent关系方法和模型。 / p>
您可以创建getter方法来处理这些:
在您的发布模型,创建新的方法的东西:
public function getCategories()
{
return $ this-> hasMany() - > where('taxonomy','post_category');
}
public function getTags()
{
return $ this-> hasMany() - > where('taxonomy','post_tag');
}
I'm using Laravel 4. I have many to many relationships in my system. And I choose to use Wordpress taxonomy table scheme.
But how can I make models relationships with Laravel 4 Eloquent ORM? Here is my database tables;
Table terms
:
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | MUL | | |
| slug | varchar(200) | NO | UNI | | |
+------------+---------------------+------+-----+---------+----------------+
Table term_taxonomy
:
+------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+----------------+
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| term_id | bigint(20) unsigned | NO | MUL | 0 | |
| taxonomy | varchar(32) | NO | MUL | | |
| description | longtext | NO | | NULL | |
| parent | bigint(20) unsigned | NO | | 0 | |
| count | bigint(20) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+----------------+
Table term_relationships
:
+------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+-------+
| object_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_taxonomy_id | bigint(20) unsigned | NO | PRI | 0 | |
| term_order | int(11) | NO | | 0 | |
+------------------+---------------------+------+-----+---------+-------+
Normally we can do return $this->belongsToMany('Term');
but how can we do 2 relationships? We need 2 relationships first find term taxonomy from "term_taxonomy" table, after find term relations with "taxonomy_id".
And an example for how I want to use;
$categories = Post::find(1)->categories; // get terms with taxonomy="post_category"
$tags = Post::find(1)->tags; // get terms with taxonomy="post_tag"
I don't want to do this with basic database class "DB::table('table')->join('...')...
" I want to use Eloquent relation methods and models.
You can create getter methods to handle these:
In your Post model, create new methods something along the lines of:
public function getCategories()
{
return $this->hasMany()->where('taxonomy', 'post_category');
}
public function getTags()
{
return $this->hasMany()->where('taxonomy', 'post_tag');
}
这篇关于在雄辩中与分类学有许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!