Как я запрашиваю данные в CakePHP с помощью отношений HABTM?

Это то, что вы ищете

<html>

<style>
.scroll{
	width:300px;
	height:200px;
	background:red;
	overflow: scroll;
}
</style>

<div class="scroll">
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).However, this code scrolls the whole page and not just one div block. Also, I got stuck in the way I could specify to scroll up and down up to three times and in what positions (otherwise: -300px, -400px and -500px up and down - one after the other).
</div>
</html>

7
задан Chuck Burgess 18 January 2013 в 15:48
поделиться

4 ответа

Поверните свой уровень отладки до 2 и посмотрите на вывод SQL. Найдите запрос, который генерирует Ваш код, и Вы заметите, что существуют несколько. Уровень ORM в CakePHP не присоединяется к связанным таблицам HABTM в первом запросе. Это получает результаты первого выбора, затем отдельно выбирает данные HABTM для каждого объекта. Поскольку объединяющая таблица не находится в первом запросе, Ваше условие, которое предназначается для использования на объединяемой таблице, приводит к ошибке, которую Вы видите.

Поваренная книга имеет раздел по ассоциациям HABTM и выбирающим данным на основе условий в таблице HABTM, которая будет соответствовать Вашим требованиям.

7
ответ дан 6 December 2019 в 15:36
поделиться

Вашу таблицу нужно назвать "schools_users" и не "school_members", потому что это - many-many, таким образом использование формы множественного числа в имени таблицы с обеих сторон является appropiate.

Вы также устанавливаете Модель ClassName "Школа" как Псевдоним для HABTM. Необходимо изменить это на что-то более универсальное как "Школы", как в "Пользователе находится в и имеет многие SchoolS" для предотвращения конфликтов.

И другая подсказка: Попытайтесь найти пользователя "с помощью" Школьной Модели, а не Пользовательской Модели.

$this-> Пользователь-> Школы-> находят ()

Надеюсь, это поможет.

2
ответ дан 6 December 2019 в 15:36
поделиться

FWIW, Ваши объединяющие таблицы, действительно кажется, "странно называют", поскольку они не следуют конвенции, описанной здесь:

http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

В любом случае, удача, я помню HABTM быть болью торца в CakePHP.

0
ответ дан 6 December 2019 в 15:36
поделиться

Из Поваренной книги: http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

Один из вариантов - поиск по модели тегов (вместо рецепта), который также даст нам всем связанных рецептов.

$this->Recipe->Tag->find('all', array(
    'conditions' => array('Tag.name' => 'Dessert')));

Мы также могли бы использовать модель таблицы соединений (которую нам предоставляет CakePHP) для поиска заданного идентификатора.

$this->Recipe->bindModel(array('hasOne' => array('RecipesTag')));
$this->Recipe->find('all', array(
    'fields' => array('Recipe.*'),
    'conditions' => array('RecipesTag.tag_id' => 124) // id of Dessert
));

Также можно создать экзотическую ассоциацию с целью создания столько объединений, сколько необходимо для обеспечения возможности фильтрации, например:

$this->Recipe->bindModel(array('hasOne' => array('RecipesTag',
    'FilterTag' => array(
        'className' => 'Tag',
        'foreignKey' => false,
        'conditions' => array('FilterTag.id = RecipesTag.tag_id')
))));
$this->Recipe->find('all', array(
    'fields' => array('Recipe.*'),
    'conditions' => array('FilterTag.name' => 'Dessert')
));
5
ответ дан 6 December 2019 в 15:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: