2012-09-29 3 views
4

joomla 2.5에 새 기사를 만들고 해당 기사를 메뉴 항목에 추가하는 구성 요소를 만들었습니다.프로그래밍 방식으로 줌라에 메뉴 항목 만들기

문서를 만드는 데는 문제가 없지만 메뉴 항목을 만드는 데는 문제가 있습니다.

   //add the article to a menu item 
       $menuTable = JTable::getInstance('Menu', 'JTable', array()); 

        $menuData = array(
        'menutype' => 'client-pages', 
        'title' => $data[name], 
        'type' => 'component', 
        'component_id' => 22,     
        'link' => 'index.php?option=com_content&view=article&id='.$resultID, 
        'language' => '*', 
        'published' => 1, 
        'parent_id' => '1', 
        'level' => 1, 
       ); 

       // Bind data 
       if (!$menuTable->bind($menuData)) 
       { 
        $this->setError($menuTable->getError()); 
        return false; 
       } 

       // Check the data. 
       if (!$menuTable->check()) 
       { 
        $this->setError($menuTable->getError()); 
        return false; 
       } 

       // Store the data. 
       if (!$menuTable->store()) 
       { 
        $this->setError($menuTable->getError()); 
        return false; 
       } 

오류가 PARENT_ID 및 레벨을 설정하여 것 같다 :

나는 다음과 같은 코드가 있습니다.

경고 : str_repeat() [function.str 반복] : 두 번째 인수를 0으로 PARENT_ID 및 레벨을 설정 tablenested.php 라이브러리/줌라/데이터베이스/디버깅이 내 관리자 페이지에 다음과 같은 오류가 발생 라인 /Applications/MAMP/htdocs/joomla_2_5/administrator/components/com_menus/views/items/tmpl/default.php 0보다 크거나 같아야하는 것은 129

+0

그것은 오류 메시지가 당신이 뭘 잘못했는지 꽤 구체적인 것을 것 같습니다. 그게 뭐가 문제 야? – hakre

+0

오류 메시지가 joomla 코어 파일을 가리키고 있기 때문에 거기에서 해킹하고 싶지 않습니다. 나는 왜 내가 joomla 코어가 내 parent_id와 레벨을 0으로 재설정 유지하는지에 대한 문제를 해결할 필요가 있다고 생각한다. –

답변

4

사용하여 시도 JTableNested::setLocation($referenceId, $position = 'after'):

$table->setLocation($parent_id, 'last-child'); 

나는 또한 당신이 n

// Rebuild the tree path. 
if (!$table->rebuildPath($table->id)) { 
    $this->setError($table->getError()); 
    return false; 
} 

여전히 작동하지 않는 경우, MenusModelItem::save 당신이하지 않는 무엇을 알아보십시오 EED 경로를 재 구축합니다.

+0

멋지다! 그게 다행 이네. 다시 감사합니다 Fnatte. –

0

이 코드는 나를 위해

JTable의 :: addIncludePath (JPATH_ADMINISTRATOR를 '/ 구성 요소/com_menus/테이블 /'.) 일; $ menuTable = & JTable :: getInstance ('menu', 'menusTable');

  $menuData = array(
        'menutype' => 'client-pages', 
        'title' => 'mytrialmenu', 
        'type' => 'component', 
        'component_id' => 22, 
        'link' => 'index.php?option=index.php?     option='com_content&view=article&id='.$resultID, 
        'language' => '*', 
        'published' => 1, 
        'parent_id' => 'choose some parent', 
        'level' => 1, 
      ); 
      // Bind data 
      if (!$row->bind($menuData)) 
      { 
       $this->setError($menuTable->getError()); 
       return false; 
      } 

      // Check the data. 
      if (!$row->check()) 
      { 
       $this->setError($menuTable->getError()); 
       return false; 
      } 

      // Store the data. 
      if (!$row->store()) 
      { 
       $this->setError($menuTable->getError()); 
       return false; 
      } 

나는 이유는 menusTable가 수동으로 사람들을 업데이트해야하므로 데이터베이스 테이블에 parent_idlevel를 업데이트하지 않습니다

0

어떻게 든 $menutable 메뉴 표에 LFT와 RGT 필드를 조작 할 필요 JnestedTable을 확장 생각 joomla 쿼리로 두 필드.

$menuTable = JTable::getInstance('Menu', 'JTable', array()); 

     $menuData = array(
      'menutype' => 'client-pages', 
      'title' => $data[name], 
      'type' => 'component', 
      'component_id' => 22,     
      'link' => 'index.php?option=com_content&view=article&id='.$resultID, 
      'language' => '*', 
      'published' => 1, 
      'parent_id' => '1', 
      'level' => 1, 
     ); 

     // Bind data 
     if (!$menuTable->bind($menuData)) 
     { 
      $this->setError($menuTable->getError()); 
      return false; 
     } 

     // Check the data. 
     if (!$menuTable->check()) 
     { 
      $this->setError($menuTable->getError()); 
      return false; 
     } 

     // Store the data. 
     if (!$menuTable->store()) 
     { 
      $this->setError($menuTable->getError()); 
      return false; 
     } 

     $db = $this->getDbo(); 
     $qry = "UPDATE `#__menu` SET `parent_id` = 1 , `level` = 1 WHERE `id` = ".$menuTable->id; 
     $db->setQuery($qry); 
     $db->query(); 
+0

당신은 항상 JTableNested API를 사용하여 업데이트를 수행해야합니다. 이렇게하면 중첩 된 설정 값이 올바르게 설정되지 않습니다. 또한 조회를 실행하지 않고 조회에 API를 사용해야합니다. – Elin

0
$table->setLocation($parent_id, 'last-child'); 

는 모두가 그 왼쪽/오른쪽 값은 새 메뉴 항목에 대해 올바르게 작성되었는지 확인하기 위해 필요하다. JTableMenu의 store 메소드에 의해 처리되기 때문에 경로를 재 작성할 필요가 없습니다.

또한, 편리한 메소드는, 바인딩 확인하고 메뉴 항목을 저장하는 데 사용할 수 있습니다 "저장":

$menuItem = array(
      'menutype' => 'client-pages', 
      'title' => $data[name], 
      'type' => 'component', 
      'component_id' => 22,     
      'link' => 'index.php?option=com_content&view=article&id='.$resultID, 
      'language' => '*', 
      'published' => 1, 
      'parent_id' => $parent_id, 
      'level' => 1, 
     ); 

$menuTable = JTable::getInstance('Menu', 'JTable', array()); 

$menuTable->setLocation($parent_id, 'last-child'); 

if (!$menuTable->save($menuItem)) { 
    throw new Exception($menuTable->getError()); 
    return false; 
} 
관련 문제