Insert, update and delete records and related intrinsic values, attributes and labels.

From CollectiveAccess Documentation
Jump to: navigation, search

This article intends to explain by simple examples how this operations works.
It is implied that you know how to obtain data. API:Accessing_Data


In this example we'll insert a new record.

$obj = new ca_objects(); //Instantiate an empty object
$obj->setMode(ACCESS_WRITE); //Set access mode to WRITE
$pn_locale_id='1'; //Set the locale
$obj->set(array('access' => 2, 'status' => 3, 'idno' => $rand,'type_id' => 179,'locale_id'=>$pn_locale_id));//Define some intrinsic data.
$obj->insert();//Insert the object


In this example we'll update an existing record.

$ent = new ca_entities(32); //Instantiate an entity with id 32
$ent->setMode(ACCESS_WRITE);//Set access mode to WRITE
$ent->set(array('access' => 2, 'status' => 3));// Update some intrinsic data. 
$ent->update(); //Update  the entity


In this example we'll delete an existing record.

$ent = new ca_entities(32);//Instantiate an entity with id 32
$ent->setMode(ACCESS_WRITE); //Set access mode to WRITE
$ent->delete(); //Delete the entity

delete() method without options performs a soft delete. Record will remain in the database but delete field will be set to 1.

for a hard delete the sequence would be

$ent = new ca_entities(32);//Instantiate an entity with id 32
$ent->setMode(ACCESS_WRITE); //Set access mode to WRITE
$t_ent->delete(true, array('hard' => true)); 

Add attributes

$address = 'fake address 123';
$obj->addAttribute(array('address1'=>$address ),'address ');//We insert value $address into address1  field inside address container.
$obj->setMode(ACCESS_WRITE); //Set access mode to WRITE
$obj->update(); //Update the record	

Delete attribute

$obj->removeAttribute($key); //We remove attribute with id $key;		
$obj->setMode(ACCESS_WRITE); //Set access mode to WRITE					
$obj->update();	//Update the record

Add labels

$name = 'cool label'; //Label value
$pn_locale_id = 1; //Label locale
$obj->addLabel(array('name'=>$name),$pn_locale_id,null,true);//Add the label. Only the array containing the name and the locale are mandatory. 
If 'true' is set label will be preferred.

Adding an Entity to an existing object

$o_search = new ObjectSearch(); //instantiate a new search object

	$qr_hits = $o_search->search($ID_NUMBER);  //search for a specific record
		$ID_Number = print_r($qr_hits->get('ca_objects.idno'),true);  //confirm the id number
		print "Inspecting {$ID_Number}\n";
		$ObjectID = print_r($qr_hits->get('ca_objects.object_id'),true);  //get the record number
		$t_object = new ca_objects($ObjectID);  //and instantiate an new object with that record's information
		// $vn_first_name has the author first name (or blank if an organization)
		// $vn_middle_name has the authors middle name (or blank if an organization or no middle name)
		// $vn_sur_name has the authors surname or the organization name
		// this example is for an entity_type of individual where $vn_individual= $t_list->getItemIDFromList('entity_types', 'individual');
		// and for source is authors where $vn_related_author_type_id = $t_rel_types->getRelationshipTypeID('ca_objects_x_entities', 'author');
		if ($vn_author_id = getEntityID('Individual', $vn_first_name, $vn_middle_name, $vn_sur_name, $vn_display_name, $vn_individual, $vn_related_author_type_id)) 
			$t_object->addRelationship('ca_entities',$vn_author_id, $vn_related_source_type_id);
			if ($t_object->numErrors()) 
				print "ERROR ADDING INDIVIDUAL ENTITY (Author){$vn_display_name} FOR OBJECT {$ID_NUMBER}: ".join('; ', $t_object->getErrors())."\n";
// ----------------------------------------------------------------------
function getEntityID($type, $ps_forename, $ps_middlename, $ps_surname, $ps_displayname, $pn_type_id, $pn_source_id) {
	global $pn_locale_id;
	$t_entity = new ca_entities();
	if (sizeof($va_entity_ids = $t_entity->getEntityIDsByName($ps_forename, $ps_surname)) == 0) {
		print "\t\t CREATING {$type} ENTITY  {$ps_forename}/{$ps_surname} type {$pn_type_id}, source {$pn_source_id}\n";
		// insert person
		$t_entity->set('locale_id', $pn_locale_id);
		$t_entity->set('type_id', $pn_type_id);
		$t_entity->set('source_id', $pn_source_id);
		$t_entity->set('access', 1);
		$t_entity->set('status', 2);
		if ($t_entity->numErrors()) {
			print "ERROR INSERTING entity ($ps_forename $ps_middlename $ps_surname): ".join('; ', $t_entity->getErrors())."\n";
			return null;
			'forename' => $ps_forename, 'middlename' => $ps_middlename, 'surname' => $ps_surname, 'displayname' => $ps_displayname
		), $pn_locale_id, null, true);
		$vn_entity_id = $t_entity->getPrimaryKey();
	} else {
		print "\t\t Found Entity {$ps_forename}/{$ps_surname}\n";
		$vn_entity_id = array_shift($va_entity_ids);
	return $vn_entity_id;

This results in a new entity (if it didn't exist already) or a reference to an existing entity being associated as an Author to the existing record

Deleting all the authors associated to an existing object

$vn_organization= $t_list->getItemIDFromList('entity_types', 'organization');
$vn_individual= $t_list->getItemIDFromList('entity_types', 'individual');

$t_rel_types = new ca_relationship_types();
$vn_related_author_type_id = $t_rel_types->getRelationshipTypeID('ca_objects_x_entities', 'author');

$o_search = new ObjectSearch();
$qr_hits = $o_search->search('some accession id');  //find the record

	$ACCESSIONNO = print_r($qr_hits->get('ca_objects.idno'),true); 
	print "inspecting accession id {$ACCESSIONNO}\n";
	$t_object = new ca_objects();
	$t_object->load(array('idno' => $ACCESSIONNO));  //get a new instance of object with the records information

	$va_entities = $t_object->getRelatedItems('ca_entities', array('restrict_to_relationship_types' => array('author')));  //get the related entities limited to authors
		foreach($va_entities as $va_info){
			$vn_first_name = $va_info["forename"];
			$vn_last_name = $va_info["surname"];
			$vn_display_name = $va_info["displayname"];
			deleteEntityID($vn_first_name,$vn_last_name, $vn_individual, $vn_related_author_type_id);  //individuals are identified by first name and last name
			deleteEntityID('',$vn_display_name, $vn_organization, $vn_related_author_type_id);  //organizations only have a display name

This finds a record, gets all the entities related to it and deletes only the authors. Useful in case you have made a horrible mistake and need to recreate them.



Personal tools