I have three models: Task, Worker and Material, where a Material BELONGS_TO a Task and a Task HAS_MANY workers. The named scope [color="#0000FF"]unassigned[/color] inside my Task model refers to those tasks to which no worker is assigned:
public function scopes() {
return array(
'unassigned' => array(
'with' => 'workers',
'condition' => 'workers.id is null',
'together' => true
)
);
}
Similarly, the [color="#0000FF"]unassignedTask[/color] scope inside my Material model refers to materials belonging to those tasks to which no worker is assigned:
public function scopes() {
return array(
'unassignedTask' => array(
'with' => 'task.workers',
'condition' => 'workers.id is null',
'together' => true
)
);
}
I have simplified thoroughly, but this example illustrates my main concern, that is, the code looks redundant and forces me to synchronize code in multiple files of my application.
Is there any way I could factorise to above code so that the unassignedTask scope somehow "goes" through the Task model? What would you do?
thank you for your answer. I think you have understood very well my problem; as you say the [b]where[/b] clause is somehow attached to the [b]join[/b] clause and I cannot see how could I define the scope just once in the Task model and make use of it in the Material model.
Still looking for a way...
[color="#000080"]Column not found: 1054 [color="#FF0000"]Unknown column ‘workers.id’ in ‘on clause’[/color]. The SQL statement executed was: SELECT t.id AS t0_c0, t.name AS t0_c1, t.task_id AS t0_c2, task.id AS t1_c0, task.name AS t1_c1, workers.id AS t2_c0, workers.name AS t2_c1, workers.task_id AS t2_c2 FROM materialt LEFT OUTER JOIN tasktask ON (t.task_id=task.id) [color="#FF0000"]AND (workers.id is null)[/color] LEFT OUTER JOIN workerworkers ON (workers.task_id=task.id)[/color]
Undefined table: 7 ERROR: missing FROM-clause entry for table "taskWorkers"
LINE 1: ..."task" "task" ON ("t"."task_id"="task"."id") AND ("taskWorke...
^. The SQL statement executed was: SELECT COUNT(DISTINCT "t"."id") FROM "material_requisition" "t" LEFT OUTER JOIN "task" "task" ON ("t"."task_id"="task"."id") AND ("taskWorkers".id is null) LEFT OUTER JOIN "task_worker" "taskWorkers" ON ("taskWorkers"."task_id"="task"."id") WHERE (task.tenant_id = :tenant_id)
I find the solution quite acceptable as it saves me having to change the ‘unassignedTask’ scope in Material model whenever ‘unassigned’ scope in Task changes, which is what I was after.
Thanks very much for your help seenivasan and Leto. Best regards.
Sorry, although it was apparently working ok because it gave no errors, the last proposed approach does not produce the desired results, i.e. the scope is bound to Task model but has no effect on Material model.