This is my first time to post a question to stackoverflow, i am sorry if there are any misses.
I am using laravel4.2
and xampp 1.8.3
now. I have 2 tables as follows:
Table: tasks id int primary_key auto_incremental, name string.. other attributes....
Table: task_predecessors id int primary_key auto_incremental, task_id int foreign key reference tasks.id task_predecessor_id int foreign key reference tasks.id
table tasks does not have any FK referencing on task_predecessors, but table task_predecessors has 2 FKs, both are referencing on the same primary key of table tasks.
here the task_predecessors looks like:
- id | task_id |predecessor_task_id
- 1 | 1 |11
- 2 | 1 |16
- 3 | 4 |3
Situation: one task may (or may not) serve as a predecessor to other tasks, and a given task may (or may not) has predecessor(s).
Reason of why i design the tables: I know the PK of task_predecessors is meaningless because task_id and predecessor_task_id can be a composite PK. And why i created the predecessor_tasks table, but not only add a column called predecessor_task_id to table tasks is that: i don't want to have different pk value to the same task.
My question is :
- Are there any problems on my design of the tables?
- How to express this recursive relationship in Laravel ORM?
I have tried as follows, i don't know if it is correct.
class Task extends \Eloquent {
public function taskpredecessor(){
return $this->belongsToMany('Task', 'task_predecessors', 'task_id', 'predecessor_task_id');
}
}
when i test the above code: return Task::find(1)->taskpredecessor;
here is the result:
**[*{"id":11,"name":"Task1","start_date":"2014-09-01 00: 00:00","end_date":"2014-09-06 00:00:00","duration":null,"progress":100,"theStatus":"completed","priority":"Norma l","predecessor_id":null,"approved":null,"file_id":null,"wbs_id":null,"created_at":"2015-02-12 05:10:45","updated_at":"2015-02-12 13:12:56","type":"Document","message_id":null,"notes":"Normal analysis","student_id":22,"optimistic":null,"most_likely":null,"pessimistic":null,"expected_time":null,"pivot":{"task_id":1,"predecessor_task_id":11}}*,***{"id":16,"name":"I dont know","start_date":"2014-09-01 00:00:00","end_date":"2014-09-23 00:00:00","duration":null,"progress":0,"theStatus":null,"priority":"Very Low","predecessor_id":null,"approved":null,"file_id":null,"wbs_id":null,"created_at":"2015-04-04 14:37:00","updated_at":"2015-04-05 02:13:40","type":"System","message_id":null,"notes":"","student_id":21,"optimistic":null,"most_likely":null,"pessimistic":null,"expected_time":null,"pivot":{"task_id":1,"predecessor_task_id":16}}***]**
as you can see, there are two records i got, but it only show the precedessor information, how to get back the successor infomation, i.e task_id:1 and its details.
Many thanks for your attentions and reading the long information here.