1

我试图放弃一些 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"
    )
));

不用说,我相当肯定我没有正确掌握锂模型关系。

4

1 回答 1

0

您看到的直接“未找到模型关系 TermTaxonomies”错误是由于拼写错误。

您已命名您的模型TermsTaxonomiesTermsRelationships而 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"
    )
));
于 2013-06-07T12:43:45.630 回答