News:

AbanteCart v1.4.2 is released.

Main Menu

Missing newly inserted address ID

Started by renato.aloi, March 14, 2017, 06:03:28 PM

Previous topic - Next topic

renato.aloi

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

abantecart

Hello,

I see your struggle.

The thing is, model class ModelAccountAddress and function method addAddress does return address_id back

public function addAddress($data = array ()){
...
$address_id = $this->db->getLastId();
...
return $address_id;


But controller ControllerPagesAccountAddress does not use that returned ID.


$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:

$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

renato.aloi

Thank you a lot!

Solution #2 worked like a charm!

Best Regards
Renato

Forum Rules Code of conduct
AbanteCart.com 2010 -