Ciao a tutti,
sto avendo dei problemi con l’utilizzo degli ‘scopes’ all’initerno dei ‘with’ in una query.
Vi riassumo la situazione:
ho 3 tabelle relazionate una a molti a cascata, come nello schema di seguito
e i tre modelli con le rispettive relations ed i rispettivi scopes, come di seguito:
class A extends CActiveRecord
{
public function tableName()
{
return 'tab_a';
}
public function relations()
{
return array(
'b' => array(self::HAS_MANY, 'B', 'id_tab_a'),
);
}
}
class B extends CActiveRecord
{
public function tableName()
{
return 'tab_b';
}
public function relations()
{
return array(
'a' => array(self::BELONGS_TO, 'A', 'id_tab_a'),
'c' => array(self::HAS_MANY, 'C', 'id_tab_b'),
);
}
public function defaultScope()
{
return array(
'condition' => $this->getTableAlias(false, false) . '.status != "deleted"'
);
}
public function scopes()
{
return array(
'enabled' => array(
'condition' => $this->getTableAlias() . '.status = "enabled"',
'with' => array(
'c' => array(
'select' => false,
'joinType' => 'INNER JOIN'
),
),
)
);
}
}
class C extends CActiveRecord
{
public function tableName()
{
return 'tab_c';
}
public function relations()
{
return array(
'b' => array(self::BELONGS_TO, 'B', 'id_tab_b'),
);
}
}
Quello che vorrei fare è selezionare un record da A con i rispettivi record in B che hanno il campo status=‘enabled’ e che hanno almeno un record in C collegato.
Speravo di ottenere il tutto in questo modo:
$a= A::model()->with(array(
'b' => array(
'scopes' => 'enabled',
),
))->findByPk([ID]);
invece è come se il with all’interno di enabled ( nel modello B ) non fosse applicato.