Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

How To Factorise Named Scopes

$
0
0
Hi there,

I have three models: Task, Worker and Material, where a Material BELONGS_TO a Task and a Task HAS_MANY workers. The named scope unassigned 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 unassignedTask 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?

Viewing all articles
Browse latest Browse all 18717

Trending Articles