method add documentation in bbn\Appui\Option
Creates a new option or a new hierarchy by adding row(s) in the options' table
Example
X::dump($opt->add([
'id_parent' => $opt->fromCode('bbn_ide'),
'text' => 'My new option',
'code' => 'new_opt',
'myProperty' => 'my value'
]));
// (int) 49 New ID
X::dump($opt->add([
'id_parent' => $opt->fromCode('bbn_ide'),
'text' => 'My new option',
'code' => 'new_opt',
'myProperty' => 'my value'
'items' => [
[
'code' => "test",
'text' => "Test",
'myProperty' => "My property's value",
],
[
'code' => "test2",
'text' => "Test 2",
'myProperty' => "My property's value",
'items' => [
[
'code' => "test8",
'text' => "Test 8",
]
]
]
]
], true, true));
// (int) 4 Number of inserted/modified rows
function(array $it, $force = false, $return_num = false, $with_id = false)
{
if ($this->check()) {
$res = $return_num ? 0 : null;
$items = !empty($it['items']) && \is_array($it['items']) ? $it['items'] : false;
$id = null;
try {
$this->_prepare($it);
}
catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
if ($it) {
$c =& $this->fields;
if ($it[$c['code']]) {
$id = $this->db->selectOne(
$this->class_cfg['table'],
$c['id'],
[
$c['id_parent'] => $it[$c['id_parent']],
$c['code'] => $it[$c['code']]
]
);
}
elseif (!empty($it[$c['id']])) {
$id = $this->db->selectOne(
$this->class_cfg['table'],
$c['id'],
[
$c['id'] => $it[$c['id']],
$c['code'] => null
]
);
}
if ($id
&& $force
&& (null !== $it[$c['code']])
) {
try {
$res = (int)$this->db->update(
$this->class_cfg['table'],
[
$c['text'] => $it[$c['text']],
$c['id_alias'] => $it[$c['id_alias']],
$c['value'] => $it[$c['value']],
$c['num'] => $it[$c['num']] ?? null,
$c['cfg'] => $it[$c['cfg']] ?? null
],
[$c['id'] => $id]
);
}
catch (Exception $e) {
$this->log([X::_("Impossible to update the option"), $it]);
throw new Exception(X::_("Impossible to update the option"));
}
}
$values = [
$c['id_parent'] => $it[$c['id_parent']],
$c['text'] => $it[$c['text']],
$c['code'] => empty($it[$c['code']]) ? null : $it[$c['code']],
$c['id_alias'] => $it[$c['id_alias']],
$c['value'] => $it[$c['value']],
$c['num'] => $it[$c['num']] ?? null,
$c['cfg'] => $it[$c['cfg']] ?? null
];
if (isset($it[$c['id']]) && !$this->exists($it[$c['id']])) {
$values[$c['id']] = $it[$c['id']];
}
if (!empty($it[$c['id']]) && $with_id) {
$values[$c['id']] = $it[$c['id']];
}
if (!$id) {
try {
$res = (int)$this->db->insert($this->class_cfg['table'], $values);
}
catch (Exception $e) {
X::log([X::_("Impossible to add the option"), $values], 'OptionAddErrors');
throw new Exception(X::_("Impossible to add the option"));
}
$id = $this->db->lastId();
}
if ($res) {
$this->deleteCache($id);
}
if ($items && bbn\Str::isUid($id)) {
foreach ($items as $item){
$item[$c['id_parent']] = $id;
$res += (int)$this->add($item, $force, $return_num, $with_id);
}
}
}
else {
X::log($it, 'OptionAddErrors');
}
return $return_num ? $res : $id;
}
return null;
}
Creates a new option or a new hierarchy by adding row(s) in the options' table BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2024
BBN Solutions