我正试图放弃一些wordpress开销,并使用锂模型关系直接查询数据库。
这是我正在复制的查询:
SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (<list of id's>) ORDER BY t.name ASC
如果我理解正确,这就是关系:
wp_term_relationships 属于 wp_term_taxonomy,属于 wp_terms。
以下是我设置模型的方法:
class Terms extends \lithium\data\Model{
public $_meta = array(
'source' => 'wp_terms',
'key' => 'term_id'
);
protected $_schema = array(
'term_id' => array('type' => 'id'),
'name' => array('type' => 'string'),
'slug' => array('type' => 'string'),
'term_group' => array('type' => 'int')
);
public $hasOne = array(
"TermsTaxonomies" => array(
"to" => 'app\models\TermsTaxonomies',
"key" => "term_id",
)
);
}
class TermsTaxonomies extends \lithium\data\Model{
public $_meta = array(
'source' => 'wp_term_taxonomy'
);
protected $_schema = array(
'term_taxonomy_id' => array('type' => 'id'),
'term_id' => array('type' => 'int'),
'taxonomy' => array('type' => 'string'),
'description' => array('type' => 'string'),
'parent' => array('type' => 'int'),
'count' => array('type' => 'int')
);
public $belongsTo = array(
"Terms" => array(
"to" => 'app\models\Terms',
"key" => "term_id",
)
);
}
class TermsRelationships extends \lithium\data\Model{
public $_meta = array(
'source' => 'wp_term_relationships'
);
protected $_schema = array(
'object_id' => array('type' => 'id'),
'term_taxonomy_id' => array('type' => 'int'),
'term_order' => array('type' => 'int')
);
public $belongsTo = array(
"TermsTaxonomies" => array(
"to" => 'app\models\TermsTaxonomies',
"key" => "term_taxonomy_id",
)
);
}
我得到了“未找到模型关系TermTaxonomies”。运行此查询时出错:
$terms = Terms::find('all', array(
"conditions" => array(
"TermTaxonomies.taxonomy" => "category",
"TermRelationships.object_id" => array(8489)
),
"with" => array(
"TermTaxonomies", "TermRelationships"
)
));
毋庸置疑,我相当肯定我对锂模型关系没有正确的把握。
答案 0 :(得分:0)
您所看到的即时“模型关系TermTaxonomies not found”错误是由于输入错误。
您已将模型命名为TermsTaxonomies
和TermsRelationships
,而Terms :: find正在寻找“TermTaxonomies”和“TermRelationships”。更新“with”语句以使用正确的类名。
查阅正在讨论的表格图表可能是有益的:http://codex.wordpress.org/images/9/9e/WP3.0-ERD.png
在每个模型中,您可以明确键,然后定义每个模型之间的关系。我已经省略了模式,因为它与你的问题无关。
对于Terms
模型:
<?php
namespace app\models;
class Terms extends \lithium\data\Model {
public $_meta = array(
'source' => 'wp_terms',
'key' => 'term_id'
);
public $hasMany = array(
"TermsTaxonomies" => array(
"to" => 'app\models\TermsTaxonomies',
"key" => "term_id",
),
"TermsRelationships" => array(
"from" => "app\models\TermsTaxonomies",
"to" => "app\models\TermsRelationships",
"key" => array(
"term_id" => "term_taxonomy_id"
),
),
);
}
?>
对于TermsTaxonomies
模型:
<?php
namespace app\models;
class TermsTaxonomies extends \lithium\data\Model {
public $_meta = array(
'source' => 'wp_term_taxonomy',
'key' => 'term_taxonomy_id'
);
public $belongsTo = array(
"Terms" => array(
"to" => 'app\models\Terms',
"key" => array(
"term_id" => "term_id",
),
),
);
public $hasMany = array(
"TermsRelationships" => array(
"to" => "app\models\TermsRelationships",
"key" => array(
"term_taxonomy_id" => "term_taxonomy_id"
),
),
);
}
?>
对于TermsRelationships
模型:
<?php
namespace app\models;
class TermsRelationships extends \lithium\data\Model {
public $_meta = array(
'source' => 'wp_term_relationships',
'key' => array('object_id', 'term_taxonomy_id'),
);
public $belongsTo = array(
"TermsTaxonomies" => array(
"to" => 'app\models\TermsTaxonomies',
"key" => array(
"term_taxonomy_id" => "term_taxonomy_id"
),
),
"Terms" => array(
"from" => "app\models\TermsTaxonomies",
"to" => "app\models\Terms",
"key" => array(
"term_taxonomy_id" => "term_id"
),
),
);
}
?>
最后在您的控制器中:
$terms = Terms::find('all', array(
"conditions" => array(
"TermsTaxonomies.taxonomy" => "category",
"TermsRelationships.object_id" => array(8489)
),
"with" => array(
"TermsTaxonomies",
"TermsRelationships"
)
));