Adding some constraints about PK
This commit is contained in:
25
XMLDB.php
25
XMLDB.php
@@ -70,6 +70,13 @@ class XMLDB{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function pkAlreadyExists($pk, $table = '*'){
|
||||
if($this->selectFromPK($table, $pk , 'count') > 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function selectTable($name, $format = 'node'){
|
||||
return $this->select($name, null, null, null, $format);
|
||||
@@ -165,8 +172,8 @@ class XMLDB{
|
||||
if (!$from) {
|
||||
throw new Exception('uhoh, no table selected');
|
||||
}
|
||||
if($id != null && count($id) == 1){
|
||||
$attribute .= '[@' . $this->_primaryKey . ' = "' . $id . '"]';
|
||||
if($id != null){
|
||||
$attribute = '[@' . $this->_primaryKey . ' = "' . $id . '"]';
|
||||
}
|
||||
if($attributes != null){
|
||||
foreach($attributes as $attributeName=>$attributeValue)
|
||||
@@ -233,7 +240,7 @@ class XMLDB{
|
||||
|
||||
// TODO other $where and $position
|
||||
public function insertNode($node, $table = null, $position = null){
|
||||
if(!is_array($node) || !isset($node['name']))
|
||||
if(!is_array($node) || !isset($node['name']) || !isset($node['attributes']))
|
||||
return false;
|
||||
|
||||
// Creating the node from an array
|
||||
@@ -252,17 +259,17 @@ class XMLDB{
|
||||
|
||||
// Inserting the node into the DB
|
||||
// case : creation of a new table
|
||||
if($table == null){
|
||||
if($table == null && !$this->tableAlreadyExists($node['name'])){
|
||||
$this->_doc->firstChild->appendChild($element);
|
||||
}else{
|
||||
}else if($table != null){
|
||||
// case : insertion into the end of table
|
||||
//TODO checking if PK already exists
|
||||
$request = $this->_xpath->query('//table[@name = "'.$table.'"]');
|
||||
if($request->length != 1){
|
||||
if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){
|
||||
return false;
|
||||
}
|
||||
$request = $this->_xpath->query('//table[@name = "'.$table.'"]');
|
||||
$request->item(0)->appendChild($element);
|
||||
}
|
||||
}else
|
||||
return false;
|
||||
return $this->save();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user