Author Topic: Missing newly inserted address ID  (Read 5086 times)

Offline renato.aloi

  • Newbie
  • *
  • Posts: 11
  • Karma: +4/-0
    • View Profile
Missing newly inserted address ID
« on: March 14, 2017, 06:03:28 PM »
Hello again!

I am stucked again at another point developing my extension.

Now I need to hook up some new fields to Address Book form.

I've managed to hook up my code in registration form when user is registering the login for 1st time. Like this:

public function onControllerPagesAccountCreate_UpdateData()
{
  $that = $this->baseObject;
  if ($that->request->is_POST())
  {
    ...
    $address_id = $result->rows[0]['address_id'];
    $that->db->query(
      sprintf("INSERT INTO `%s` (`address_id`, `address_complement`, `address_number`, `address_district`) VALUES (%d, '%s', '%s', '%s') ",
        'pse_addresses_attributes',
        $address_id,
        $request_data['complement'],
        $request_data['number'],
        $request_data['district'])
      );
  }
 ...
}

My problem now is how can I determine the $address_id when adding new address from Address Book?

At Address Book edit page there is a button called "New Address" and it triggers the insert method at ControllerPagesAccountAddress class.

For some strange reason I do not understand the method addAddress from ModelAccountAddress class (called by insert method) does not set any address_id global variable.

So how can I get the address id latter in my hook function?

I have tried this with out success:

public function onControllerPagesAccountAddress_ProcessData()
{
  $that = $this->baseObject;
  $method = $this->baseObject_method;
  $request_data = $that->request->post;

  if ($method == 'insert') $address_id = $that->db->getLastId(); // This do not work!!
  ...
}

----
P.S.: Sorry about the code off the tags, but the forum is blocking me from posting code inside apropriate tags.

The forum is gaving me this error when I try to post code inside tags:

Code: newbielink:javascript:void(0); [nonactive]

and this

Sorry, you are not allowed to post external links.
----

Thank you all in advance!

Best Regards
Renato
« Last Edit: March 14, 2017, 06:05:47 PM by renato.aloi »

Offline abantecart

  • Administrator
  • Hero Member
  • *****
  • Posts: 4358
  • Karma: +298/-10
    • View Profile
    • Ideal Open Source Ecommerce Solution
Re: Missing newly inserted address ID
« Reply #1 on: March 15, 2017, 08:38:29 AM »
Hello,

I see your struggle.

The thing is, model class ModelAccountAddress and function method addAddress does return address_id back
Code: [Select]
public function addAddress($data = array ()){
...
$address_id = $this->db->getLastId();
...
return $address_id;

But controller ControllerPagesAccountAddress does not use that returned ID.

Code: [Select]
$this->model_account_address->addAddress($this->request->post);


We can have 2 solutions here.
1. We can add returned address_id to $this->data and you can access it in your hook.
This will be only in v 1.2.10. You will need to propose this in github with the pull request. 

2. You can create your own SQL query to get latest address_id for this customer_id.
Something like this:
Code: [Select]
$query = $this->db->query(
"SELECT addresses_id
FROM " . $this->db->table("addresses") . "
WHERE customer_id = '" . (int)$this->customer->getId() . "' ORDER BY addresses_id DESC LIMIT 0, 1 ");

I suggest #2.



 

Please  rate your experience or leave your review
We need your help to build better free open source ecommerce platform for everyone. See how you can help

Offline renato.aloi

  • Newbie
  • *
  • Posts: 11
  • Karma: +4/-0
    • View Profile
Re: Missing newly inserted address ID
« Reply #2 on: March 15, 2017, 09:45:32 AM »
Thank you a lot!

Solution #2 worked like a charm!

Best Regards
Renato

 

Powered by SMFPacks Social Login Mod