简体   繁体   中英

In CakePHP how to make Containable behavior cooperate with Tree behavior

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM