Archive for พฤศจิกายน 27th, 2007

add drupal user with user_save()

จากครั้งก่อนที่ผมหาทาง เพิ่ม user ให้กับ drupal ผ่านทาง curl ซึ่งมันก็ทำงานได้ แต่พอเอาโค้ดไปใส่ไว้เป็น node ใน drupal มันกลับทำงานไม่ได้ ผมก็เลยต้องหาวิธีอื่น ตอนแรกก็ลอง insert ข้อมูลเข้าตาราง users ตรงๆ ก็ติดเรื่องที่ว่ามีตารางอื่นที่ user ต้องเกี่ยวพันด้วย เลยทำให้ยุ่งยาก และการมายุ่งกับตารางของระบบตรงๆเองแบบนี้อาจจะทำให้มันเจ๊งเอาได้ง่ายๆ ก็เลยไปหาebookเกี่ยวกับ drupal มาลองค้นดูมีคำอธิบายเกี่ยวกับโมดูลต่างๆอยู่ มีอยู่ module หนึ่งคือ user ผมก็เลยลองไปค้นในโค้ดของ user.module เพื่อหาฟังก์ชันที่ทำหน้าที่ในการadd user และแล้วก็มาเจอ คอมเม้นของฟังก์ชันหนึ่งแบบนี้

/**
* Save changes to a user account or add a new user.
*
* @param $account
*   The $user object for the user to modify or add. If $user->uid is
*   omitted, a new user will be added.
*
* @param $array
*   An array of fields and values to save. For example array('name' => 'My name');
*   Setting a field to NULL deletes it from the data column.
*
* @param $category
*   (optional) The category for storing profile information in.
*/

จากคอมเม้นบอกว่ามันสามารถแก้ไขข้อมูลของ user ที่มีอยู่แล้ว หรือถ้า user นั้นไม่มี ก็จะเป็นการ add user นั้นเข้าไป
ซึ่งวิธีการ add user เข้าไปก็คือเวลาส่งค่าให้ฟังก์ชัน ไม่ต้องส่ง uid ของ user เข้าไป ตัวอย่างการสร้าง add user ใหม่ เช่น

  user_save(array ('name' => 'username'),array ('name' => 'username','pass' => 'abcdefg','status' => 1));

เพียงเท่านี้เราก็จะได้ user ใหม่แล้ว password และ สถานะ Active เรียบร้อย

1 comment พฤศจิกายน 27, 2007


 

พฤศจิกายน 2007
พฤ อา
« ต.ค.   ธ.ค. »
 1234
567891011
12131415161718
19202122232425
2627282930  

ความเห็นล่าสุด

etcpe9 on Validate Form ด้วย LiveVa…
BraiN on ประสบการณ์ จับใบดำใบแดง
pphetra on Tree In Haskell
phondanai on Tree In Haskell
ืneogravity on About

Meta

Blogroll

cpe9

Linux

programming