2011-11-23 83 views
1

我試圖構建一個擴展,它將創建自動重定向短URL的頁面,並使其縮短需要根據其entry_id獲取頁面的路徑。ExpressionEngine:如何獲得給定其entry_id頁面的路徑(結構插件)

說,我有一個頁面的路徑:http://server.tld/index.php/path/to/my/page 但是,在代碼中,我只知道這個頁面的entry_id。

如果我查看exp_channel_titles表,我可以得到url_title字段。但它只會包含「頁面」。我想要「/ path/to/my/page」。而且這似乎沒有任何API。

你知道我該怎麼辦?

非常感謝。

回答

0

我沒發現什麼比下面的代碼好:

// 
    // 
    // Don't look the following code just yet . . . 
    // 
    // 
    // You'll be pulling your hair out of your head. Just read me first. 

    // Following is a huge SQL query, that assume that pages are not nested 
    // than 9 times. This is actually a hard limitation of EE, and I'm using 
    // that to get the information I need in only one query instead of nine. 
    // 
    // The following code is just to get the path of the current entry we are 
    // editing, so the redirect page will know where to redirect. And I did 
    // not find any function or API to get this information. Too bad. 
    // 
    // If you find any solution to that, please answer 
    // http://stackoverflow.com/questions/8245405/expressionengine-how-to-get-the-path-of-a-page-given-its-entry-id-with-the-str 
    // it might save other people all the trouble. 

    // S 
    // 
    // P 
    // 
    // O 
    // 
    // I 
    // 
    // L 
    // 
    // E 
    // 
    // R 

    // First, we get all the entry_id of all the elements that are parent to 
    // the current element in the structure table (created by the Structure 
    // plugin). 

    $q = $this->EE->db->query(
    "SELECT 
     S1.entry_id AS entry9, 
     S2.entry_id AS entry8, 
     S3.entry_id AS entry7, 
     S4.entry_id AS entry6, 
     S5.entry_id AS entry5, 
     S3.entry_id AS entry4, 
     S7.entry_id AS entry3, 
     S8.entry_id AS entry2, 
     S9.entry_id AS entry1 
    FROM 
     exp_structure AS S1, 
     exp_structure AS S2, 
     exp_structure AS S3, 
     exp_structure AS S4, 
     exp_structure AS S5, 
     exp_structure AS S6, 
     exp_structure AS S7, 
     exp_structure AS S8, 
     exp_structure AS S9 
    WHERE 
     S1.entry_id = $entry_id AND 
     S1.parent_id = S2.entry_id AND 
     S2.parent_id = S3.entry_id AND 
     S3.parent_id = S4.entry_id AND 
     S4.parent_id = S5.entry_id AND 
     S5.parent_id = S6.entry_id AND 
     S6.parent_id = S7.entry_id AND 
     S7.parent_id = S8.entry_id AND 
     S8.parent_id = S9.entry_id"); 

    // Then, we construct a similar query to get all the url_title attributes 
    // for these pages. 

    $path = array(); 
    $sql = array("SELECT" => "SELECT", "FROM" => " FROM", "WHERE" => " WHERE"); 
    $j = 1; 
    for($i = 1; $i <= 9; ++$i){ 
    $id = $q->row("entry$i"); 
    if($id > 0){ 
     $sql['SELECT'] .= " CT$j.url_title AS title$j,"; 
     $sql['FROM'] .= " exp_channel_titles as CT$j,"; 
     $sql['WHERE'] .= " CT$j.entry_id = $id AND"; 
     $j++; 
    } 
    } 
    $sql['SELECT'] = rtrim($sql['SELECT'], ","); 
    $sql['FROM'] = rtrim($sql['FROM'], ","); 
    $sql['WHERE'] = preg_replace("/ AND$/", "", $sql['WHERE']); 
    $sql = $sql['SELECT'] . $sql['FROM'] . $sql['WHERE']; 
    $q = $this->EE->db->query($sql); 

    // Finally, we can construct the path for the current page 

    $path = "/"; 
    for($i = 1; $i < $j; ++$i){ 
    $path .= $q->row("title$i") . '/'; 
    } 

    // 
    // Blood and bloody ashes. 
    // 
0

我可以建議他們通過詢問support forum結構的開發者?

4

我不記得確切的文檔在哪裏,但我認爲你的問題來自頁面uris不直接從數據庫中檢索的事實。

它們位於Expressionengine全局配置變量中。我已經能夠做到用entry_id使用下面的代碼的網址查詢:

注:這裏假設你正在使用的結構,頁面模塊等

<?php 

    $this->EE =& get_instance(); // Get global configuration variable 

    $site_id = $this->EE->config->item('site_id'); // Get site id (MSM safety) 
    $site_pages = $this->EE->config->item('site_pages'); // Get pages array 

    /** The array is indexed as follows: 
    * $site_pages[{site_id}]['url'] = '{site_url} 
    * $site_pages[{site_id}]['uris']['entry_id'] = {page_uri} 
    **/ 

    $page_url = $site_pages[$site_id]['uris'][$entry_id]; 

?> 

編輯: 我最初說, uris不在數據庫中,嚴格來說不是真的......頁面實際上被存儲爲由網站ID索引的exp_sites.site_pages中的散列字符串。

相關問題