I got introduced to the REST API a few days ago and despite some initial resistance from my part Iāve grown to like it. Itās working well, but as new records are being created I miss that I cannot provide some extra custom values to user attributes (such as a Name etc.)
So I looked into /restapi/includes/subscribers.php and added a function:
public static function subscriberUpdateCustomField()
{
$sql = 'UPDATE phplist_user_user_attribute SET value=:value WHERE userid=:userid AND attributeid=:attributeid;';
$userid = sprintf('%d',$_REQUEST['userid']);
$attributeid = sprintf('%d',$_REQUEST['attributeid']);
if (empty($userid) || empty($attributeid)) {
Response::outputErrorMessage('invalid call');
}
try {
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam('userid', $userid, PDO::PARAM_INT);
$stmt->bindParam('attributeid', $attributeid, PDO::PARAM_INT);
$stmt->bindParam('value', $_REQUEST['value'], PDO::PARAM_STR);
$stmt->execute();
$db = null;
self::SubscriberGet($userid);
} catch (\Exception $e) {
Response::outputError($e);
}
}
This works well for records that already have one or more user attributes (e.g. imported via the browser interface).
If that is the case I can change any user attribute with ease.
However, for newly added records, e.g. via the API, that hence have no user attributes yet, I cannot change the user attributes. Even though the function always returns āsuccessā in this case, inspecting the record via the browser shows the fields are still empty.
I donāt know enough about mySQL and this db to know why, I assume the user attributes are a sub record of some sort (?) that needs to be created first as well ? Again, not a database expert at all.
If somebody could put me back on the rails for this last hurdle, that would be great !
Thanks !
Thank you @duncanc
However, there is an added level of complexity that Iām unsure about on how to handle.
I mean, I could INSERT a userid in the table, and UPDATE it in case itās a DUPLICATE, but the userid contains several attributeidās with value as well, so how do I update only one attributeid this way ?
I could try something like:
$sql = āINSERT INTO phplist_user_user_attribute (userid) VALUES (:userid) ON DUPLICATE KEY UPDATE attributeid=:attributeid;ā;
but that doesnāt feel right nor does it update the value
Can I combine WHERE in this statement ?
I canāt seem to figure this one out (yet)
And hereās how you use latter function:
PS. you can find the attribute idās when you logon to your phplist and navigate to Config / Configure attributes
In below example, user attribute 1 contains the userās name: