0

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 :

  1. Are there any problems on my design of the tables?
  2. 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.

rmuc8
  • 2,869
  • 7
  • 27
  • 36
  • " return Task::with('taskpredecessor')->get(); " works very good after many testings, but how to use group_concat() to combine the predecessors in one cell though Laravel's method? – EastCompass Apr 05 '15 at 16:14
  • finally @@ i can apply the GROUP_CONCAT() to eager loading in laravel. return Task::with(array('taskpredecessor' => function($query){ $query->selectRaw('GROUP_CONCAT(name) as predecessor'); }))->get(array('id', 'name')); – EastCompass Apr 05 '15 at 17:34
  • i put the question and impose an answer by myself @@ :O) But i want to know the design issue on my question . :) – EastCompass Apr 05 '15 at 17:36

0 Answers0