1

I have a custom component, from which I am taking my data & creating a new article with extra field values(from code). Below, is my code & attempt.

JLoader::import('components.com_content.models.article', JPATH_ADMINISTRATOR);

    $app = JFactory::getApplication();
    // Get the model
    $model = $this->getModel("lists");
    $newsLists = $model->getNewsList();
    foreach ($newsLists as $news)
                {
                    $articleModel = "";
                    $articleModel = JModelLegacy::getInstance('Article', 'ContentModel');

                    $data = array();
                    $data['title'] = trim($news->title);
                    $data['state'] = trim($news->state);
                    $data['language'] = '*';
                    $data['catid'] = '221';
                    $data['com_fields']['title'] = trim($news->title);
                    $data['com_fields']['source'] = trim($news->publication);
                    $data['com_fields']['page-no'] = trim($news->pageno);
                    $data['com_fields']['link'] = trim($news->externalurl);
                    $data['com_fields']['attributes'] = trim($news->attributes);
                    $data['com_fields']['downloads'] = trim($news->newsfile);
                    $data['com_fields']['date-free-text'] = trim($news->published_date);
                    $data['com_fields']['state'] = trim($news->state);

                    $articleModel->save($data);

                }

Actually, it creates the article correctly in the database(in Joomla 'content' table) but it only saves the single extra field value of 'title' ($data['com_fields']['title']) in Joomla fields_value table.

Code when I print $data;

Array
(
    [title] => unveils
    [state] => 2
    [language] => *
    [catid] => 221
    [com_fields] => Array
        (
            [title] => unveils
            [source] => 
            [page-no] => 
            [link] => images/news/unveils.pdf
            [attributes] => 
            [downloads] => 
            [date-free-text] => 
            [state] => 2
        )

)
Array
(
    [title] => Workshop
    [state] => 1
    [language] => *
    [catid] => 221
    [com_fields] => Array
        (
            [title] => Workshop
            [source] => 
            [page-no] => 
            [link] => /images/news/workshops.pdf
            [attributes] => 
            [downloads] => 
            [date-free-text] => 20.04.2015
            [state] => 1
        )

)

What's the wrong in foreach loop? Note : all the extra fields are already created from "Content >> Fields"

  • This looks like a well crafted question with a coding attempt, clear issue, sample data, and an expected result -- you are the kind of poster that we want to see more of. While you wait for support, please register your account take the tour to earn your Informed badge, and perhaps add some details in your profile. – mickmackusa Oct 22 '18 at 11:37
  • Cross-network post: stackoverflow.com/q/52927516/2943403 – mickmackusa Oct 22 '18 at 11:47

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.