0

I have this problem with containable, that I have Categories model that behaves like a tree:

class Category extends AppModel {
  public $actsAs = ['Tree', 'Containable'];

  public $hasMany = [
    'Novelty' => [
      'className' => 'Novelty',
      'foreignKey' => 'category_id',
      'dependent' => false,
    ]
  ]
}

its structure:

Category(
  id int(11)
  name varchar(255)
  parent_id int(11)
  lft int(11)
  rght int(11)
)

and i have a Novelty model:

class Novelty extends AppModel {
  public $actsAs = ['Containable'];

  public $belongsTo = [
    'Category' => [
      'className' => 'Category',
      'foreignKey' => 'category_id',
    ]
  ];
}

Novelty structure:

Novelty(
  id int(11)
  name varchar(255)
  category_id int(11)
)

Associations

Category -> hasMany -> Novlety
Novelty -> belongsTo -> Category

Categories example structure from find('threaded') is like this:

['Category'] => [
  'id' => 13
  'name' => 'Main'
  'lft' => 88
  'rght' => 105
  'children' => [
    [0] => [
      ['Category'] => [
        'id' => 131
        'name' => 'sub1'
        'lft' => 89
        'rght' => 90
      ]
    ]
    [1] => [
      ['Category'] => [
        'id' => 132
        'name' => 'sub2'
        'lft' => 91
        'rght' => 92
      ]
    ]
    [...]
  ]
]

And now when I try containable:

$this->Category->contain(
  'Novelty' => [
    'conditions' => [
      'AND' => [
        'Novelty.category_id >=' => 13,
        'Novelty.category_id !=' => 14
      ]
    ]
  ]
);

Now when i run Category->find('all') there are no Novelties in result. Because cake adds its own condition Novelty.category = (13). And I can't get rid nor expand it. So query looks like this:

SELECT `Novelty`.`id`, `Novelty`.`name`, `Novelty`.`category_id`, 
FROM `databse`.`novelties` AS `Novelty` 
WHERE ((`Novelty`.`category_id` >= 13) 
  AND (`Novelty`.`category_id` < 14)) 
  AND `Novelty`.`category_id` = (13)

Is there any special condition that modifies default query condition?

My goal:

To get Novelties that are related to subcategory also, not only those that are related to main category. But is it even possible?

Sebastian Piskorski
  • 4,026
  • 3
  • 23
  • 29

1 Answers1

2

Now when i run Category->find('all') there are no Novelties in result. Because cake adds its own condition Novelty.category = (13). And I can't get rid nor expand it. So query looks like this:

This is the correct behavior of the code. You have a Cateogry hasMany Novelty association, to make this happen Cake adds the condition.

To me it looks like you want to find all Novelty records except the ones for category id 14, is that right?

Temporary unbindModel() your Novelty association and add it back as a hasOne assoc without a foreign key but with a condition:

['conditions' => ['Novelty.cateogry_id !=' 14]]

Or use the joins array of the query.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • I have forgot to mention that Category is a Tree. So I want to get all Novelty for Category = 13 and its children. I have convention that all child categories have number begining with 13. Bu now I see that condition would be wrong until i'll make id to sort in linguistic order. Anyway. I want to get all Novelties for category = 13 and its children – Sebastian Piskorski Jan 15 '15 at 16:12
  • "I have convention that all child categories have number begining with 13." This doesn't sound like any of both supported types (parent-child, nested set) of tree structures of the behavior. However, use joins and not the containable behavior to build the queries you need. – floriank Jan 16 '15 at 08:45