구성 요소의 #__content에 기사를 어떻게 추가합니까?


11

내 구성 요소는 비 전통적인 방식으로 프런트 엔드에서 기사를 추가해야합니다. MySQL로 삽입 할 수 있다는 것을 알고 있지만 가능한 경우 핵심 기능을 사용하고 싶습니다.

components \ com_content의 코드를 연구 한 결과, 발생해야하는 모든 것에 약간 압도 당하고 너무 복잡해지기를 바랐습니다.

Joomla에 이에 대한 예가 있습니까, 아니면 그것을 달성하기 위해 따라야 할 단계가 있습니까?

답변:


6

모델 파일을 열고 다음 클래스를 모델 클래스 안에 추가하십시오.

public function getContentTable($type = 'Content', $prefix = 'JTable', $config = array())
{
    return JTable::getInstance($type, $prefix, $config);
}

이제 모델 클래스 내에 메소드를 정의하여 기사를 추가 할 수 있습니다. 이 같은:

public function addArticle()
{
    $table = $this->getContentTable();
    $table->title = "Foo";
    $table->alias = "foo";
    // or
    // $table->alias = JApplication::stringURLSafe($table->title);
    $table->catid = 2;
    $table->state = 1;
    // and so on!
    // then save it
    $table->save();
}

1

또한 비 전통적인 방식으로 기사를로드해야했습니다. 이를 위해 많은 Joomla 코드를 활용할 수있었습니다. 필요에 따라이를 조정해야합니다.

이 함수는 id (숫자) 또는 별칭이 지정된 artlice를 반환합니다.

    function loadArticle($id){

            $app = JFactory::getApplication();
            $db = JFactory::getDBO();
            $query = $db->getQuery(true);
            $selects = array('a.introtext','a.publish_up','a.publish_down');
            $query->select($selects);
            $query->from('#__content as a');

            // select the alias or id
            $where = 'a.title = ' . $db->q(NNText::html_entity_decoder($id));
            $where .= ' OR a.alias = ' . $db->q(NNText::html_entity_decoder($id));
            if (is_numeric($id)) {
                    $where .= ' OR a.id = ' . $id;
            }

            $query->where('(' . $where . ')');

            // check the publish and unpublish dates
            $now = JFactory::getDate('now','UTC');
            $nullDate = $db->getNullDate();

            $query->where('a.state = 1');

            $query->where('( a.publish_up = ' . $db->q($nullDate) . ' OR a.publish_up <= ' . $db->q($now) . ' )');
            $query->where('( a.publish_down = ' . $db->q($nullDate) . ' OR a.publish_down >= ' . $db->q($now) . ' )');

            $db->setQuery($query);
            $article = $db->loadObject();
            return $article;
    }

이것은 기사를 얻는 좋은 출발점처럼 보이지만 기사를 추가해야합니다 ...
— Al Knight

죄송합니다, 오해
— ContextSwitch
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.