I have create a entity type "apply" through ECK.After that i want add an "Apply" programmatically, This is my code:

  global $language;
  $langcode = $language->language;	
$apply = new stdClass();
	$apply->uid = $uid;
	$apply->created = $created;
	$apply->entity_type = 'apply';
	$apply->type = 'apply';
	$apply->bundle = 'apply';
	$apply->field_resume_id[$langcode][]['target_id'] = $resume_id;
	$apply->field_resume_id[$langcode][]['target_type'] = 'node';
	$apply->field_job_id[$langcode][]['target_id'] = $job_id;
	$apply->field_job_id[$langcode][]['target_type'] = 'node';
	$apply->field_company_id[$langcode][]['target_id'] = $company_id;
	$apply->field_company_id[$langcode][]['target_type'] = 'node';
          entity_save('apply',$apply);

Entity reference field was not saved when the $langcode is 'zh-hans', after i change the $langcode to "und", the value was saved.

  global $language;
  $langcode = $language->language;	
$langcode = 'und';// works
$apply = new stdClass();
	$apply->uid = $uid;
	$apply->created = $created;
	$apply->entity_type = 'apply';
	$apply->type = 'apply';
	$apply->bundle = 'apply';
	$apply->field_resume_id[$langcode][]['target_id'] = $resume_id;
	$apply->field_resume_id[$langcode][]['target_type'] = 'node';
	$apply->field_job_id[$langcode][]['target_id'] = $job_id;
	$apply->field_job_id[$langcode][]['target_type'] = 'node';
	$apply->field_company_id[$langcode][]['target_id'] = $company_id;
	$apply->field_company_id[$langcode][]['target_type'] = 'node';
          entity_save('apply',$apply);

I am not sure this is a bug of ECK or Entity reference.

Comments

acrazyanimal’s picture

Status: Active » Needs review

I'm not 100% sure here, but it looks to me that your code is not quite right. I don't think you can have empty '[]' for both the 'target_id' and 'target_type'. You need to link them together in a single array identifier. Your code would create:

field_resume_id[$langcode] = array (
              0 => array( 'target_id' => $resume_id ),
              1 => array( 'target_type' => 'node'), // this should be indexed with a zero too!
)

Try the following:

  global $language;
  $langcode = $language->language;	
  $apply = new stdClass();
	$apply->uid = $uid;
	$apply->created = $created;
	$apply->entity_type = 'apply';
	$apply->type = 'apply';
	$apply->bundle = 'apply';
	$apply->field_resume_id[$langcode][0]['target_id'] = $resume_id;
	$apply->field_resume_id[$langcode][0]['target_type'] = 'node';
	$apply->field_job_id[$langcode][0]['target_id'] = $job_id;
	$apply->field_job_id[$langcode][0]['target_type'] = 'node';
	$apply->field_company_id[$langcode][0]['target_id'] = $company_id;
	$apply->field_company_id[$langcode][0]['target_type'] = 'node';
  entity_save('apply',$apply);

Regardless, I don't believe this is ECK related.

fmizzell’s picture

Status: Needs review » Active
fmizzell’s picture

@acrazyanimal You've worked with multilingual sites right? Do multilingual fields work with eck?

fmizzell’s picture

Status: Active » Postponed (maintainer needs more info)
legolasbo’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)

Closing old issues. Please reopen the issue with a clear description and/or steps to reproduce if this issue is still relevant.

legolasbo’s picture

Status: Closed (duplicate) » Closed (cannot reproduce)