0

I can get information regarding orders using 3rd party API but when I want to save it into 3 tables, for sales_flat_order and sales_flat_order_adress the data has been saved and for sales_flat_order_grid I've gotten this error I think that I want to save the Parent_id in the entity_id as it should be same. But I don't know how.

<br />
<b>Fatal error</b>:  Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`sales_flat_order_grid`, CONSTRAINT `FK_SALES_FLAT_ORDER_GRID_ENTITY_ID_SALES_FLAT_ORDER_ENTITY_ID` FOREIGN KEY (`entity_id`) REFERENCES `sales_flat_order` (`entity_id`) ON DELETE CA)' in D:\wamp\www\magento1.9\lib\Zend\Db\Statement\Pdo.php:228

this array that I want to save for 3 tables

Foreach($array as $value){
                    $array = array('status' => $value['status'],
                 'base_grand_total' => $value['price'],
                 'grand_total' => $value['price'],
                 'increment_id' => $value['number'],
                 'base_currency_code' => 'MXN',
                 'customer_email' => $value['email'],
                 'customer_firstname' => $value['name'],
                 'customer_lastname' => $value['apellido'],
                 'global_currency_code' => 'MXN',
                 'order_currency_code' => 'MXN',
                 'store_name' => 'linio marketplace',
                 'created_at' => $value['created'],
                 'updated_at' => $value['created'],
                 'total_item_count' => $value['item']);
                  $modele = Mage::getModel('thorleif/order')->setData($array);
                  $modele->save();
                 $id = $modele->getId();
                 $array = array(
                 'parent_id' => $id,
                 'region' => $value['region'],
                 'postcode' => $value['zip'],
                 'street' => $value['address'].'.'.$value['country'],
                 'city' => $value['city'],
                 'email' => $value['email'],
                 'telephone' => $value['tel'],
                 'firstname' => $value['name'],
                 'lastname' => $value['apellido'],
                 'address_type' => 'shipping');
                  $modele = Mage::getModel('thorleif/address')->setData($array);
                  $modele->save();
                  $array = array(
                 'parent_id' => $id,
                 'status' => $value['status'],
                 'base_grand_total' => $value['price'],
                 'grand_total' => $value['price'],
                 'increment_id' => $value['number'],
                 'base_currency_code' => 'MXN',
                 'shipping_name' => $value['name'],
                 'global_currency_code' => 'MXN',
                 'order_currency_code' => 'MXN',
                 'store_name' => 'linio marketplace',
                 'created_at' => $value['created'],
                 'updated_at' => $value['created']);
                  $modele = Mage::getModel('thorleif/grid')->setData($array);
                  $modele->save();
                 }
  • 1
    Is it for us to guess line 46 ? – PЯINCƏ Jul 9 '17 at 19:34
  • I have this in line 46 if ($order->getPayment()->getMethodInstance()->getCode() != 'free') { return $this; } – javaTodev Jul 9 '17 at 19:36
  • Please put your full code and the name of each one – PЯINCƏ Jul 9 '17 at 19:40
  • this the principle code. I get the external data using input and I put each value into variable. so I use the model for order to save those data as new order using setdata or adddata method. – javaTodev Jul 9 '17 at 19:45
  • 1
    Take a look at these two methods on this link stackoverflow.com/questions/6877767/… – Ledian Hymetllari Jul 10 '17 at 0:15
0

Please try this to check if you get well your paiement method code:

$paymentMethods = Mage::getModel('payment/config')->getAllMethods();

foreach($paymentMethods as $paymentMethod) {
    echo $paymentMethod->getCode();
}

Edit:

Check also this one and look if you get the payment method code :

$order->getPayment->getMethodInstance()->getData()

If you get your payment method :

$order->getPayment()->getMethod();

You can follow this inchoo tuto to create an order programmatically

| improve this answer | |
  • my payment value is like this CashOnDelivery_Payment so I don't know if this is the issue? – javaTodev Jul 9 '17 at 19:47
  • Please look my update. – PЯINCƏ Jul 9 '17 at 19:49
  • I've use it this line to test it and it works updating information $modele = Mage::getModel('sales/order')->load(194); but what I need to do is to create a new row, but using addData doesn't work – javaTodev Jul 9 '17 at 21:43
  • I don't know what you mean by create new row? if you want to archieve some information use setData() and getData(), if you want to use an array use addData() check this: magento.stackexchange.com/a/62539/48355 – PЯINCƏ Jul 9 '17 at 22:04
  • What I need to do is to create new order in table "sales_flat_order" using the code before putting each value in the attribute and save all the values I got. – javaTodev Jul 9 '17 at 23:37
0

It's work to me using the insertion like this for sales_flat_order_grid

$entity_id =1; 
$entity_type_id =4; 
$attribute_id = 92;
$value = 'red'; 
$store_id  =1;
$resource     = Mage::getSingleton('core/resource');
$writeAdapter   = $resource->getConnection('core_write');
$table        = $resource->getTableName('some_table_name');
$query        = "INSERT INTO {$table} (`entity_id`,`entity_type_id`,`attribute_id`,`value`,`store_id`) VALUES ($entity_id, $entity_type_id, $attribute_id, $value, $store_id);";
$writeAdapter->query($query);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.