2013-12-09 44 views
-1

我想生成包含在Drupal 7 由另一模塊文件生成圖像的實體這是我的代碼:創建實體的對Drupal的像場7

global $user; 

$apercu = $_SESSION['image_generate']; 
$file_apercu = new StdClass(); 
$file_apercu->uid = $user->uid; 
$file_apercu->uri = $apercu; 
$file_apercu->filemime = file_get_mimetype($file_apercu->uri); 
$file_apercu->status = 1; 
$file_apercu = file_save($file_apercu); 
$file_apercu = (array) $file_apercu; 
$file_apercu = array($file_apercu); 

$pdf = $_SESSION['pdf']->uri; 
$file_pdf = new StdClass(); 
$file_pdf->uid = $user->uid; 
$file_pdf->uri = $pdf; 
$file_pdf->filemime = file_get_mimetype($file_pdf->uri); 
$file_pdf->status = 1; 
$file_pdf = file_save($file_pdf); 
$file_pdf = (array) $file_pdf; 
$file_pdf = array($file_pdf); 

$form_panier = $_SESSION['form_state_panier']; 
$sql = db_query('SELECT MAX("order_id") 
       FROM commerce_order 
       WHERE uid = '.$user->uid.'')->fetchAssoc(); 

$values = array(
      'type' => 'commandeflyer', 
      'uid' => intval($user->uid), 
      'status' => 1, 
      'comment' => 0, 
      'promote' => 0, 
     ); 

$entity = entity_create('node', $values); 
$ewrapper = entity_metadata_wrapper('node', $entity); 
$ewrapper->title->set('commandeflyer'.$sql['MAX("order_id")']); 
$ewrapper->field_flyer_image_commande->set($file_apercu); 
$ewrapper->field_pdf->set($file_pdf); 
$ewrapper->field_numcommande->set(intval($sql['MAX("order_id")'])); 
$ewrapper->save(true); 
entity_save('node', $entity); 

當我運行這段代碼我得到這個錯誤:

EntityMetadataWrapperException : Invalid data value given. Be sure it matches the required data type and format. dans EntityMetadataWrapper->set() (ligne 122 dans /var/www/commerce_kickstart-7.x-2.9/profiles/commerce_kickstart/modules/contrib/entity/includes/entity.wrapper.inc). 

但我的類型相應:PDF爲PDF和JPG爲圖像。 謝謝你的幫助。

回答

1

發現的解決方案:

//appel de la variable user 
global $user; 

//recuperation de lapercu 
$apercu = $_SESSION['image_generate']; 
//creation de lobject file 
$file_apercu = new StdClass(); 
$file_apercu->uid = $user->uid; 
$file_apercu->uri = $apercu; 
$file_apercu->filename = basename($file_apercu->uri); 
$file_apercu->filemime = file_get_mimetype($file_apercu->uri); 
$file_apercu->status = 1; 
//sauvegarde en base 
$file_apercu = file_save($file_apercu); 

//recuperation du pdf 
$pdf = $_SESSION['pdf']->uri; 
//creation de lobject file 
$file_pdf = new StdClass(); 
$file_pdf->uid = $user->uid; 
$file_pdf->uri = $pdf; 
$file_pdf->filename = basename($file_pdf->uri); 
$file_pdf->filemime = file_get_mimetype($file_pdf->uri); 
$file_pdf->status = 1; 
//sauvegarde en base 
$file_pdf = file_save($file_pdf); 

//recuperation du numero de commande 
$form_panier = $_SESSION['form_state_panier']; 
$sql = db_query('SELECT MAX("order_id") 
       FROM commerce_order 
       WHERE uid = '.$user->uid.'')->fetchAssoc(); 

//creation de lentity 
$values = array(
      'type' => 'commandeflyer', 
      'uid' => intval($user->uid), 
      'status' => 1, 
      'comment' => 0, 
      'promote' => 0, 
     ); 

//creation dentity sans limage et le pdf 
$entity = entity_create('node', $values); 
$ewrapper = entity_metadata_wrapper('node', $entity); 
$ewrapper->title->set('commandeflyer'.$sql['MAX("order_id")']); 
$ewrapper->field_numcommande->set(intval($sql['MAX("order_id")'])); 
$ewrapper->save(true); 
entity_save('node', $entity); 

//chargement de lentity 
$commandeflyer = node_load($nid = $entity->nid, $vid = NULL, $reset = FALSE); 

//ajout image 
$commandeflyer->field_flyer_image_commande[LANGUAGE_NONE][] = array(
    'fid' => $file_apercu->fid, 
    'uid' => $file_apercu->uid, 
    'uri' => $file_apercu->uri, 
    'filename' => $file_apercu->filename, 
    'filemime' => $file_apercu->filemime, 
    'status' => $file_apercu->status 
    ); 

//ajout pdf 
$commandeflyer->field_pdf[LANGUAGE_NONE][] = array(
    'fid' => $file_pdf->fid, 
    'display' => '1', 
    'uid' => $file_pdf->uid, 
    'uri' => $file_pdf->uri, 
    'filename' => $file_pdf->filename, 
    'filemime' => $file_pdf->filemime, 
    'status' => $file_pdf->status 
    ); 

//save entity 
node_save($commandeflyer);