对象父对象是$Object上的对象本身->;save()$这->;父级->;父级->;..===$这个


Object parent is the object itself on $object->save(); $this->parent->parent->... === $this

首先,如果标题有点模糊,我很抱歉,但这是我能想到的最好的标题。

我的问题:我有几个任务,每个任务都可以有子任务。创建任务时,它会在方法recursiveParentUpdater((中设置DB中的finished_at字段。出于某种原因,我保存后的新任务是他自己的父任务(但不在数据库中,仍然为NULL(。

例如。

$task = new Task;
// set my properties
$task->save(); // let's say ID = 5
$task->parent->parent->parent->...->id === 5 // I have no idea why this happens

然后$task->recursiveParentUpdater((使用$this->parent来获取自身并将其全部搞砸。只有在创建新任务时才会发生这种情况,删除时没有更多的$task作为父任务。

过去一切都很好(本地和共享主机(,2天后,在没有接触它的情况下,我遇到了这个问题(仅在共享主机上,本地仍然没有问题(。我的Debian机器仍在运行PHP5.6,共享主机支持5.6和7,但没有任何变化。

我的数据库架构更新(添加了"显示创建表任务"(

Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('description')->nullable();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->integer('user_id')->unsigned()->nullable();
        $table->date('deadline_date')->nullable()->default(null);
        $table->time('deadline_time')->nullable()->default(null);
        $table->datetime('finished_at')->nullable()->default(null);
        $table->timestamps();
        $table->foreign('parent_id')->references('id')->on('tasks')->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
    });
or
 tasks| CREATE TABLE `tasks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`parent_id` int(10) unsigned DEFAULT NULL,
`user_id` int(10) unsigned DEFAULT NULL,
`deadline_date` date DEFAULT NULL,
`deadline_time` time DEFAULT NULL,
`finished_at` datetime DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `tasks_parent_id_foreign` (`parent_id`),
KEY `tasks_user_id_foreign` (`user_id`),
CONSTRAINT `tasks_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `tasks_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=337 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

我的型号(相关零件(

public function user() {
    return $this->belongsTo('App'User');
}
public function children() {
    return $this->hasMany('App'Task', 'parent_id', 'id');
}
public function parent() {
    return $this->belongsTo('App'Task');
}
public function hasParent() {
    return (!is_null($this->parent)) ? true : false;
}
public function hasChildren() {
    return (count($this->children) > 0) ? true : false;
}
public function updateParentFinishedStatus() {
    function recursiveParentUpdater($task) {
        if ($task->hasParent()) {
            $task->parent->setFinishedAt(date('d-m-Y H:i:s'));
            foreach ($task->parent->children as $child) {
                if (!$child->isFinished()) {
                    $task->parent->setFinishedAt(null);
                    break;
                }
            }
            $task->parent->save();
            if ($task->parent->hasParent()) recursiveParentUpdater($task->parent);
        }
    }
    recursiveParentUpdater($this);
}

我的控制器

public function postCreate(Request $request) {
    $this->validate($request, [
        'title' => 'string|required',
        'description' => 'string',
        'ptid' => 'integer|exists:tasks,id',
        'deadline_date' => 'date|after:yesterday|required_with:deadline_time',
        'deadline_time' => 'dateformat:H:i',
    ]);
    $task = new Task;
    $task->setTitle($request->get('title'));
    $task->setDescription($request->get('description'));
    $task->setDeadline($request->get('deadline_date'), $request->get('deadline_time'));
    if (!empty($request->get('ptid'))) {
        $task->parent()->associate($request->get('ptid'));
    }
    $task->user()->associate(Auth::user());
    $task->save();
    $task->updateParentFinishedStatus();
    return redirect()->back();
}
public function getDelete(Request $request) {
    $this->validate($request, [
        'tid' => 'integer|required|exists:tasks,id',
    ]);
    $task = Task::where('id', $request->get('tid'))->where('user_id', Auth::user()->id)->first();
    $task->delete();
    $task->updateParentFinishedStatus();
    return redirect()->back();
}

我仍然不确定为什么它在主机上而不是在我的本地机器上出现问题,但这就是解决问题的原因。
我更改了这行代码

public function hasParent() {
    return (!is_null($this->parent)) ? true : false;
}

public function hasParent() {
    return (!is_null($this->parent_id)) ? true : false;
}