Jump to content

All language objects get filled instead of just the ones I want


Recommended Posts

Hello,

 

I have my own object: AllNewsObject, that has 2 multilang fields (title & content)

 

<?php
class AllNewsObject extends ObjectModel
{
/** @var string Name */

    public $main_title;
public $title;
public $publish;
    public $content;
public $date_publish;
public $manual;

    public static $definition = array(
	    'table' => 'all_news',
	    'primary' => 'id_all_news',
	    'multilang' => true,
	    'fields' => array(
		    'main_title' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
		    'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
		    'content' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'required' => true),
		    'publish' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 1),
		    'date_publish' => array('type' => self::TYPE_DATE),
		    'manual' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 1),
	    ),
);
}
?>

 

this table exists, the ps_all_news_lang table exists as well.

 

Then I have the following code to create an object and fill it:

$oNews = new AllNewsObject();  
	    $aLangs = Language::getLanguages(false);
	    foreach($aLangs as $iKey => $aVal){
		    if(Tools::getValue('ALL_MSG_TITLE_'.$aVal['id_lang']))
			    $oNews->title[$aVal['id_lang']] = Tools::getValue('ALL_MSG_TITLE_'.$aVal['id_lang']);
		    if(Tools::getValue('ALLARE_MSG_CONTENT_'.$aVal['id_lang']))
			    $oNews->content[$aVal['id_lang']] = Tools::getValue('ALLARE_MSG_CONTENT_'.$aVal['id_lang']);
	    }


	    // new one is added
	    $oNews->main_title = 'test';
	    $oNews->manual = 'N';
	    $oNews->save();

 

this code words; it fills my object, fills both tables with the correct data. but there's one thing I don't understand - I have 4 languages active in my prestashp installation: Dutch, German, French and English.

 

if I only fill the Dutch fields of my object, in the database I see that also for German, French and English the Dutch values are used

 

if I fill the Dutch and German fields, then I see in database that the German fields are indeed corresponding with what I used there, all other fields are in Dutch

 

so basically; it seems that I should always fill ALL language fields - is there any way to work around this?

 

For example; one time I want a news item that is only in Dutch and German - I don't want a record for French and English in the database

 

next time i want a news item that is only Dutch and English - I don't want a record for Germand and French in DB.

 

etc.

 

is this possible?

Link to comment
Share on other sites

×
×
  • Create New...