我试图放弃一些 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_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"
)
));
不用说,我相当肯定我没有正确掌握锂模型关系。