AND a_related != CONCAT_WS(\',\', a_related, a_related);
';

$result = MMSynchronizer::instance()->db->arrayQuery($query);

$cli->output(sprintf('%s article to check', count($result)));

while(list(,$row) = each($result))
{
    $article = false;

    try
    {
        $db->begin();

        $article = MMSynchArticle::fetch($row['a_id']);

        if ($article)
        {
            $remoteId   = $article->remoteId();
            $object     = eZContentObject::fetchByRemoteID($remoteId, true);

            if ($object)
            {
                $logPrefix  = sprintf('Synchronize object[%s] %s with article %s', $row['a_language'], $object->ID, $row['a_id'] );
                $dataMap    = $object->fetchDataMap(false, $row['a_language']);

                if (!$dataMap)
                    throw new Exception(sprintf('DataMap not fetchable for object[%s] %s with article %s', $row['a_language'], $object->ID, $row['a_id']));

                $str = implode('-', MMSynchArticle::fetchIdsToObjectIds('MMSynchMediaImage', explode(',', $article->source['a_promo_image'])));
    public function process( $row )
    {
        $db = eZDB::instance();

        // Récupéraiton de l'article
        $article        = MMSynchArticle::fetch($row['a_id']);
        $contentObject  = eZContentObject::fetchByRemoteID( $article->remoteId() );

        if (!$contentObject)
        {
            $this->cli->notice(sprintf(' Synchronization problem for Article %s ', $row['a_id']));
            return false;
        }

        // Récupération des publisher_internal_id
        $referenceArticleIds = array();
        if (trim($article->source['a_referencearticleid']) != '')
        {
            $referenceArticleIds = array_unique(explode('|', trim($article->source['a_referencearticleid'])));
        }

        $db->begin();

        try
        {
            if (count($referenceArticleIds) > 0)
            {
                $query = sprintf("
                    SELECT  a.id as a_id
                    FROM    article a,
                            content c
                    WHERE   a.content_id = c.id
                    AND     a.language = '%s'
                    AND     a.publisher_internal_id IN (%s)
                    AND     c.publisher_folder_id = %s
                    ",
                    $row['a_language'],
                    implode(', ', $referenceArticleIds),
                    $article->source['c_publisher_folder_id']
                );
                $referenceArticles = MMSynchronizer::instance()->db->arrayQuery($query);

                if (count($referenceArticles) > 0)
                {
                    $referenceArticlesObjectIds = array();

                    // Fetch des object correspondant au publisher_internal_id
                    while(list(,$referenceArticleRow) = each($referenceArticles))
                    {
                        $referenceArticle       = MMSynchArticle::fetch($referenceArticleRow['a_id']);
                        $referenceContentObject = eZContentObject::fetchByRemoteID( $referenceArticle->remoteId() );

                        if ($referenceContentObject)
                        {
                            $referenceArticlesObjectIds[] = $referenceContentObject->attribute('id');
                        }
                        else
                        {
                            $this->cli->notice(sprintf(' Synchronization problem for Article %s', $referenceArticleRow['a_id']));
                        }
                    }

                    if (count($referenceArticlesObjectIds))
                    {
                        $dataMap    = $contentObject->fetchDataMap(false, $row['a_language']);
                        $attribute  = $dataMap['reference_articles'];

                        $attribute->fromString(implode('-', array_unique($referenceArticlesObjectIds)));
                        $attribute->store();

                        if (count($referenceArticlesObjectIds) == count($referenceArticleIds))
                        {
                            $this->updateStatus($row);
                        }
                        else
                        {
                            $this->cli->notice(sprintf(' Relationships among the missing publisher_internal_id %s', implode(', ', $referenceArticleIds)));
                        }
                    }
                }
                else
                {
                    $this->cli->notice(sprintf(' No items synchronized among publisher_internal_id %s', implode(', ', $referenceArticleIds)));
                }
            }
            else
            {
                $dataMap    = $contentObject->fetchDataMap(false, $row['a_language']);
                $attribute  = $dataMap['reference_articles'];

                if ($attribute && $attribute->hasContent())
                {
                    $attribute->fromString('');
                    $attribute->store();
                }

                $this->updateStatus($row);
            }

            $db->commit();
        }
        catch (Exception $e)
        {
            $db->rollback();
        }

        eZContentObject::clearCache();
    }