public function add($values, $targetPk = null, $mode = 'first', $scope = null)
{
$clazz = $this->getPhpName();
$obj = new $clazz();
$this->mapValues($obj, $values);
if ($this->definition->isNested()) {
$root = false;
$query = $this->getQueryClass();
if ($targetPk) {
$branch = $query->findPk($this->getPropelPk($targetPk));
} else {
$branch = $query->findRoot($scope);
$root = true;
if (!$branch) {
//no root, create one
$branch = new $clazz();
$labelField = lcfirst($this->getDefinition()->getLabelField());
$rootValues = $values;
$rootValues[$labelField] = 'Root';
$this->mapValues($branch, $rootValues);
if (method_exists($branch, 'setScopeValue')) {
$branch->setScopeValue($scope);
}
$branch->makeRoot();
$branch->save();
}
}
if (!$branch) {
return false;
}
if ($branch->getLeftValue() == 1) {
$root = true;
}
if (!$scope && method_exists($branch, 'getScopeValue')) {
$scope = $branch->getScopeValue();
}
switch (strtolower($mode)) {
case 'last':
$obj->insertAsLastChildOf($branch);
break;
case 'prev':
if (!$root) {
$obj->insertAsPrevSiblingOf($branch);
}
break;
case 'next':
if (!$root) {
$obj->insertAsNextSiblingOf($branch);
}
break;
case 'first':
default:
$obj->insertAsFirstChildOf($branch);
break;
}
if ($scope && method_exists($obj, 'setScopeValue')) {
$obj->setScopeValue($scope);
}
}
if (!$obj->save()) {
return false;
}
//return new pk
$newPk = array();
foreach ($this->primaryKeys as $pk) {
$newPk[$pk] = $obj->{'get' . ucfirst($pk)}();
}
return $newPk;
}