Multiple user addresses

Hello,

what is the best way to have unlimited multiple addresses for login members?

Addresses like street, city etc.

thank you in advance.

bye
Chris

I don’t know if any existing extra offers this functionality.

Where and how do you want to manage these addresses? In the manager? On the frontend?

The best way is probably to create a custom database table with the required fields and a column that stores the user-id.

You can use ClassExtender extra - it does what you need. It goes with snippet for using with registration form to manage additional fields (addresses, etc.) and other for later updating user profiles.

thank you very much for solution idea. Is it possible with ClassExtender to have 1:n data for addresses? A user should have unlimited address data.

ClassExtender is for a 1:1 relationship between the MODX modUser and the new userData class.
You could possibly change the schema to a 1:n relationship, but I suspect you’d have to change all the other code to access/write the data as well. So there is not much advantage in using ClassExtender in my opinion.

thank you for your answer. I think its not so right solution.
then, the only way is custom solution?
I will manage the addresses in frontend.

I suppose so.

I would use ExtraBuilder (or maybe MIGX) to create/parse the schema.

And maybe QuickApi for your AJAX endpoint to manage the addresses.

oh, many thanks.
With the use of ExtraBuilder I created a custom table in relation zu _user. But how could I use snippet to get data?

Can you post your schema? Do you use MODX 2.x?

yes, I use MODX 2.8.3

<?xml version="1.0" encoding="UTF-8"?>
<model package="extendeduser.model.extendeduser" baseClass="xPDOObject" platform="mysql" defaultEngine="InnoDB" phpdoc-package="" version="1.1">
  <object class="extUser" table="" extends="modUser">
    <composite alias="Data" class="Userdata" local="id" foreign="userdata_id" cardinality="one" owner="local"/>
  </object>
  <object class="Userdata" table="user_addresses" extends="xPDOSimpleObject">
    <field key="userdata_id" dbtype="int" precision="11" phptype="integer" null="false" default=""/>
    <field key="name1" dbtype="varchar" precision="100" phptype="string" null="true" default=""/>
    <field key="name2" dbtype="varchar" precision="100" phptype="string" null="true" default=""/>
    <field key="contact" dbtype="varchar" precision="100" phptype="string" null="true" default=""/>
    <field key="phone" dbtype="varchar" precision="25" phptype="string" null="true" default=""/>
    <field key="address" dbtype="varchar" precision="50" phptype="string" null="true" default=""/>
    <field key="address_number" dbtype="varchar" precision="5" phptype="string" null="true" default=""/>
    <field key="zip" dbtype="varchar" precision="5" phptype="string" null="true" default=""/>
    <field key="city" dbtype="varchar" precision="50" phptype="string" null="true" default=""/>
    <field key="country" dbtype="varchar" precision="50" phptype="string" null="true" default=""/>
    <index alias="userdata_id" name="userdata_id" primary="false" unique="false" type="BTREE">
      <column key="userdata_id" length="" collation="A" null="false"/>
    </index>
    <aggregate alias="extUser" class="modUser" local="userdata_id" foreign="id" cardinality="one" owner="foreign"/>
  </object>
</model>

You should be able to read the data in a snippet with code like this:

<?php
$base_path = $modx->getOption('core_path').'components/extendeduser.model.extendeduser/';
$modx->addPackage('extendeduser.model.extendeduser', $base_path.'model/');

$q = $modx->newQuery('Userdata');
//only get addresses from current user
$q->where(array(
    'userdata_id' => $modx->user->get("id"),
));
$addresses = $modx->getCollection('Userdata', $q); //query multiple addresses

$output = array(); 
foreach($addresses as $address){
    $output[] = $address->toArray();
    
    $name1 = $address->get("name1"); //read a field
}
return $modx->toJson($output);

Use $modx->newObject('Userdata') to create a new address.
$modx->getObject('Userdata', $id) to get a single address.

Read the xPDO documentation for more information.

perfect. thank you so much.

and how did I use QuickApi?

To use QuickApi you have to create some snippets.

Create a snippet MyApiAuthorized (or maybe change the snippet ApiAuthorized) to define who can access your API.

Then for every endpoint create a snippet like ApiAddress.
This creates the endpoint https://yoursite.com/api/address.

In the snippet you can run some code based on the request method

if ($method === 'GET') { ... }

the $_GET parameters ($params["my_parameter"]) and the json data in the request body ($body["my_field"]).

thank you so much. I try this. many thanks :pray: