Hi, I am trying to batch save some articles using a foreach loop.
I am facing the two following issues :
- If the category does not exist, in the first run in the loop, a category is created with name and alias of the catid.
But in the second run, the exact same thing is happening an error "Another category with the same parent category has the same alias." is thrown.
- The articles ID is always the same, so in every save the next article, overwrites the previous one. I discovered that, adding a breakpoint and checking the content in every run.
Any Ideas ?
Here is the code :
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\OutputFilter;
$app = Factory::getApplication();
$mvcFactory = $app->bootComponent('com_content')->getMVCFactory();
$articleModel = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]);
$articles = [
[
'catid' => 12,
'alias' => OutputFilter::stringURLSafe('My Article 1'),
'title' => 'My Article 1',
'introtext' => 'My Article Intro Text',
'fulltext' => 'My Article Full Text',
'state' => 1,
'language' => '*',
],
[
'catid' => 12,
'alias' => OutputFilter::stringURLSafe('My Article 2'),
'title' => 'My Article 2',
'introtext' => 'My Article Intro Text',
'fulltext' => 'My Article Full Text',
'state' => 1,
'language' => '*',
],
];
foreach($articles as $article) {
if (!$articleModel->save($article)) {
throw new Exception($articleModel->getError());
}
Thank you