Answer the question
In order to leave comments, you need to log in
How to implement a concise record of conditions?
For example, if no error occurred, a message about the successful operation should be displayed. But there are several conditions. This is how I do it... But I feel in my gut that I can write more beautifully.
if ($this->isNewRecord) {
if(!empty($this->parent_id)) {
if (some condition) $success = true;
}
else {
if (some condition) $success = true;
}
}
else {
if(!empty($this->parent_id)) {
if (some condition) $success = true;
}
else {
if (some condition) $success = true;
}
}
if (@$success) {
$this->message = "OK!";
}
if ($this->isNewRecord && !empty($this->parent_id)) {
if (some condition) $success = true;
}
elseif ($this->isNewRecord && empty($this->parent_id)) {
if (some condition) $success = true;
}
elseif (!$this->isNewRecord && !empty($this->parent_id)) {
if (some condition) $success = true;
}
else {
if (some condition) $success = true;
}
if (@$success) {
$this->message = "OK!";
}
Answer the question
In order to leave comments, you need to log in
Why if $this->isNewRecord and else?
after all, there can still be success
with !empty($this->parent_id)
along the way of the appearance of some condition
if (some condition1 || some condition2 || some conditio3 || some condition4) {
$success = true;
}
$condition1 = ($this->isNewRecord && !empty($this->parent_id) && some condition1);
$condition1 = ($this->isNewRecord && empty($this->parent_id) && some condition2);
$condition1 = (!$this->isNewRecord && !empty($this->parent_id) && some condition3);
$condition1 = (!$this->isNewRecord && empty($this->parent_id) && some condition4);
such problems are solved each in its own way. Finding a common solution for everything will not always be optimal.
In this case, you can define a binary function functor: (0,1)x(0,1) -> (f1,f2,f3,f4)
functions and there will be your some conditions.
Like this (sorry for the syntax, because I don’t know php),
int to0123(bool a, bool b)
{
return ((int)(a == true) << 1) + (int)(b == true);
}
conditions = {f1, f2, f3, f4};
, where fx are functions that check your some conditions, idx = to0123($this->isNewRecord, empty($this->parent_id));
if (conditions[idx])
success = true
Here is the real code:
if ($category->isNewRecord && empty($category->pid)) {
if ($category->saveNode()) $categorySaved = true;
}
elseif ($category->isNewRecord && !empty($category->pid)) {
$rootCategory = Categories::model()->findByPk($category->pid);
if ($category->appendTo($rootCategory) && $category->saveNode()) $categorySaved = true;
}
elseif (!$category->isNewRecord && empty($category->pid) && !$category->isRoot()) {
if ($category->moveAsRoot() && $category->saveNode()) $categorySaved = true;
}
elseif (!$category->isNewRecord && !empty($category->pid)) {
$rootCategory = Categories::model()->findByPk($category->pid);
if ($category->moveAsFirst($rootCategory) && $category->saveNode()) $categorySaved = true;
}
exemplary...
$categorySaved = true;
if (!empty($category->pid)) {
$rootCategory = Categories::model()->findByPk($category->pid);
$categorySaved = $category->isNewRecord ?
$category->appendTo($rootCategory) : $category->moveAsFirst($rootCategory);
} else if ($category->isNewRecord && !$category->isRoot()) {
$categorySaved = $category->moveAsRoot();
}
$categorySaved = $categorySaved && $category->saveNode();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question