Update to use new Managed_dataobject

This commit is contained in:
Zach Copley 2011-03-09 18:00:24 -08:00
parent 65f9b5d954
commit 0429a52c6e
4 changed files with 80 additions and 136 deletions

View File

@ -95,8 +95,6 @@ class ExtendedProfilePlugin extends Plugin
$schema = Schema::get(); $schema = Schema::get();
$schema->ensureTable('profile_detail', Profile_detail::schemaDef()); $schema->ensureTable('profile_detail', Profile_detail::schemaDef());
// @hack until key definition support is merged
Profile_detail::fixIndexes($schema);
return true; return true;
} }

View File

@ -23,7 +23,7 @@ if (!defined('STATUSNET')) {
/** /**
* DataObject class to store extended profile fields. Allows for storing * DataObject class to store extended profile fields. Allows for storing
* multiple values per a "field" (field property is not unique). * multiple values per a "field_name" (field_name property is not unique).
* *
* Example: * Example:
* *
@ -36,166 +36,105 @@ if (!defined('STATUSNET')) {
* Profile_detail objects, each named 'phone_number' like this: * Profile_detail objects, each named 'phone_number' like this:
* *
* $phone1 = new Profile_detail(); * $phone1 = new Profile_detail();
* $phone1->field = 'phone_number'; * $phone1->field_name = 'phone_number';
* $phone1->rel = 'home'; * $phone1->rel = 'home';
* $phone1->field_index = 1;
* $phone1->value = '510-384-1992'; * $phone1->value = '510-384-1992';
* $phone1->value_index = 1;
* *
* $phone1 = new Profile_detail(); * $phone1 = new Profile_detail();
* $phone1->field = 'phone_number'; * $phone1->field_name = 'phone_number';
* $phone1->rel = 'mobile'; * $phone1->rel = 'mobile';
* $phone1->field_index = 2;
* $phone1->value = '510-719-1139'; * $phone1->value = '510-719-1139';
* $phone1->value_index = 2;
* *
* $phone1 = new Profile_detail(); * $phone1 = new Profile_detail();
* $phone1->field = 'phone_number'; * $phone1->field_name = 'phone_number';
* $phone1->rel = 'work'; * $phone1->rel = 'work';
* $phone1->field_index = 3; * $phone1->field_value = '415-231-1121';
* $phone1->value = '415-231-1121'; * $phone1->value_index = 3;
* *
*/ */
class Profile_detail extends Memcached_DataObject class Profile_detail extends Managed_DataObject
{ {
public $__table = 'profile_detail'; public $__table = 'profile_detail';
public $id; public $id;
public $profile_id; // profile this is for
public $profile_id;
public $rel; // detail for some field types; eg "home", "mobile", "work" for phones or "aim", "irc", "xmpp" for IM public $rel; // detail for some field types; eg "home", "mobile", "work" for phones or "aim", "irc", "xmpp" for IM
public $field; public $field_name; // name
public $value; // primary text value public $value; // primary text value
public $field_index; // relative ordering of multiple values in the same field public $value_index; // relative ordering of multiple values in the same field
public $ref_profile; // for people types, allows pointing to a known profile in the system public $ref_profile; // for people types, allows pointing to a known profile in the system
public $created; public $created;
public $modified; public $modified;
public /*static*/ function staticGet($k, $v=null) /**
* Get an instance by key
*
* This is a utility method to get a single instance with a given key value.
*
* @param string $k Key to use to lookup
* @param mixed $v Value to lookup
*
* @return User_greeting_count object found, or null for no hits
*
*/
function staticGet($k, $v=null)
{ {
return parent::staticGet(__CLASS__, $k, $v); return Memcached_DataObject::staticGet('Profile_detail', $k, $v);
} }
/** /**
* return table definition for DB_DataObject * Get an instance by compound key
* *
* DB_DataObject needs to know something about the table to manipulate * This is a utility method to get a single instance with a given set of
* instances. This method provides all the DB_DataObject needs to know. * key-value pairs. Usually used for the primary key for a compound key; thus
* the name.
*
* @param array $kv array of key-value mappings
*
* @return Bookmark object found, or null for no hits
* *
* @return array array of column definitions
*/ */
function table() function pkeyGet($kv)
{ {
return array('id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL, return Memcached_DataObject::pkeyGet('Profile_detail', $kv);
'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
'field' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
'field_index' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
'value' => DB_DATAOBJECT_STR,
'rel' => DB_DATAOBJECT_STR,
'ref_profile' => DB_DATAOBJECT_INT,
'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
} }
/**
* Database schema setup
*
* @see Schema
* @see ColumnDef
*
* @return boolean hook value; true means continue processing, false means stop.
*/
static function schemaDef() static function schemaDef()
{ {
return array(new ColumnDef('id', 'integer', return array(
null, false, 'PRI'), 'description'
=> 'Additional profile details for the ExtendedProfile plugin',
// @fixme need a unique index on these three 'fields' => array(
new ColumnDef('profile_id', 'integer', 'id' => array('type' => 'serial', 'not null' => true),
null, false), 'profile_id' => array('type' => 'int', 'not null' => true),
new ColumnDef('field', 'varchar', 'field_name' => array(
16, false), 'type' => 'varchar',
new ColumnDef('field_index', 'integer', 'length' => 16,
null, false), 'not null' => true
),
new ColumnDef('value', 'text', 'value_index' => array('type' => 'int'),
null, true), 'field_value' => array('type' => 'text'),
new ColumnDef('rel', 'varchar', 'rel' => array('type' => 'varchar', 'length' => 16),
16, true), 'rel_profile' => array('type' => 'int'),
new ColumnDef('ref_profile', 'integer', 'created' => array(
null, true), 'type' => 'datetime',
'not null' => true
new ColumnDef('created', 'datetime', ),
null, false), 'modified' => array(
new ColumnDef('modified', 'datetime', 'type' => 'timestamp',
null, false)); 'not null' => true
} ),
),
/** 'primary key' => array('id'),
* Temporary hack to set up the compound index, since we can't do 'unique keys' => array(
* it yet through regular Schema interface. (Coming for 1.0...) 'profile_detial_profile_id_field_name_value_index'
* => array('profile_id', 'field_name', 'value_index'),
* @param Schema $schema )
* @return void );
*/
static function fixIndexes($schema)
{
try {
// @fixme this won't be a unique index... SIGH
$schema->createIndex('profile_detail', array('profile_id', 'field', 'field_index'));
} catch (Exception $e) {
common_log(LOG_ERR, __METHOD__ . ': ' . $e->getMessage());
}
}
/**
* Return key definitions for DB_DataObject
*
* DB_DataObject needs to know about keys that the table has; this function
* defines them.
*
* @return array key definitions
*/
function keys()
{
return array_keys($this->keyTypes());
}
/**
* Return key definitions for Memcached_DataObject
*
* Our caching system uses the same key definitions, but uses a different
* method to get them.
*
* @return array key definitions
*/
function keyTypes()
{
// @fixme keys
// need a sane key for reverse lookup too
return array('id' => 'K');
}
/**
* Get the sequence key
*
* Returns the first serial column defined in the table, if any.
*
* @access private
* @return array (column,use_native,sequence_name)
*/
function sequenceKey()
{
return array('id', true);
} }
} }

View File

@ -53,7 +53,7 @@ class ExtendedProfile
$fields = array(); $fields = array();
while ($detail->fetch()) { while ($detail->fetch()) {
$fields[$detail->field][] = clone($detail); $fields[$detail->field_name][] = clone($detail);
} }
return $fields; return $fields;
@ -86,7 +86,7 @@ class ExtendedProfile
if (in_array(strtolower($name), $profileFields)) { if (in_array(strtolower($name), $profileFields)) {
return $this->profile->$name; return $this->profile->$name;
} else if (in_array($name, $this->fields)) { } else if (in_array($name, $this->fields)) {
return $this->fields[$name]->value; return $this->fields[$name]->field_value;
} else { } else {
return null; return null;
} }

View File

@ -111,26 +111,33 @@ class ProfileDetailSettingsAction extends SettingsAction
$detail = new Profile_detail(); $detail = new Profile_detail();
$detail->profile_id = $profile->id; $detail->profile_id = $profile->id;
$detail->field = $name; $detail->field_name = $name;
$detail->field_index = 1;
$result = $detail->find(true); $result = $detail->find(true);
if (empty($result)) { if (empty($result)) {
$detail->value = $value;
$detail->field_value = $value;
$detail->created = common_sql_now(); $detail->created = common_sql_now();
common_debug("XXXXXXXXXXXXXXX not found"); common_debug("XXXXXXXXXXXXXXX not found");
//common_debug('bbbbbbbbbbbb ' . var_export($detail, true));
$result = $detail->insert(); $result = $detail->insert();
if (empty($result)) {
common_log_db_error($detail, 'INSERT', __FILE__);
$this->serverError(_m('Could not save profile details.'));
}
} else { } else {
common_debug('zzzzz FOUND'); common_debug('zzzzz FOUND');
$orig = clone($detail); $orig = clone($detail);
$detail->value = $value; $detail->field_value = $value;
$result = $detail->update($orig); $result = $detail->update($orig);
} }
$detail->free(); $detail->free();