2013-06-06 2 views
1

Я пытаюсь отказаться от некоторых служебных программ Wordpress и запросить базы данных напрямую, используя отношения модели лития.Проблема, реплицирующая запрос таблицы таксономии 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

Неверная ошибка «Model relationship TermTaxonomies not found», которую вы видите, является опечаткой.

Вы назвали свои модели TermsTaxonomies и TermsRelationships пока Термины :: находкой ищет "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" 
    ) 
)); 
+0

Связанный вопрос: http://stackoverflow.com/questions/9836392/accessing-more-than-one-model-deep-relationships-in-lithium – christopheradams