代码之家  ›  专栏  ›  技术社区  ›  koshachok

CakePHP检索数据时忽略“hasMany”

  •  1
  • koshachok  · 技术社区  · 7 年前

    我有两张桌子——盒子和苹果。苹果属于一个盒子,一个盒子里有许多苹果。中的代码如下所示:

    class Apple extends AppModel {
        public $useDbConfig = 'somedb';
        public $useTable = 'apple';
        public $belongsTo = array(
            'Box' => array(
                'className' => 'Box',
                'foreignKey' => 'box_id'
            )
        );
    } 
    class Box extends AppModel {
        public $useDbConfig = 'somedb';
        public $useTable = 'box';
        public $hasMany = array(
            'Apple' => array(
                'className' => 'Apple',
                'foreignKey' => 'box_id',
                'dependent' => true
            )
        );
    }
    

    $this->Box->find("all")

    (Array):
        [0] => (Array):
            [Box] => (Array):
                // some data
            [Apple] => (Array):
                [0] => (Array):
                    // some data
                [1] => (Array):
                    // some data
                ...
        [1] => (Array):
            [Box] => (Array):
                // some data
            [Apple] => (Array):
                [0] => (Array):
                    // some data
                [1] => (Array):
                    // some data
        ...
    

    更新: 正如@Ben所回答的,我需要将recursive=-1(或0)放在这里。

    Room $hasMany 房间 Box ,及 $belongsTo 选择 在里面 盒子

     class Room extends AppModel {
        public $useDbConfig = 'somedb';
        public $useTable = 'room';
        public $hasMany = array(
            'Box' => array(
                'className' => 'Box',
                'foreignKey' => 'room_id'
            )
        );
    }
    // Edited Box class
    class Box extends AppModel {
        public $useDbConfig = 'somedb';
        public $useTable = 'box';
        public $hasMany = array(
            'Apple' => array(
                'className' => 'Apple',
                'foreignKey' => 'box_id',
                'dependent' => true
            )
        );
        public $belongsTo = array(
            'Room' => array(
                'className' => 'Room',
                'foreignKey' => 'room_id'
            )
        );
    }
    

    那么,我如何才能获得有关苹果、盒子和房间的数据呢?比方说,我有一个 房间 : where room.name like "room0%" . 我试了下一件事:

        $some = $this->Apple->find("all", array(
            'joins' => array(
                array('table' => 'room',
                    'alias' => 'Room',
                    'type' => 'INNER',
                    'conditions' => array(
                        'Room.id = Box.room_id',
                    )
                )
            ),
            'conditions' => array('Room.name LIKE' => 'room0%')
        ));
    

    但我只收到一组苹果盒“对”。

    目前我找到的唯一解决方案是next:

        $temp = "room0%";
        $db = $this->Apple->getDataSource();
        $some = $db->fetchAll(
            "SELECT * 
              FROM  apple 
              JOIN box on apple.box_id = box.id
              JOIN room on box.room_id = room.id
              WHERE room.name LIKE '$temp'"
        );
    

    有没有一种方法可以用模型来做同样的事情?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jari Keinänen Roberto Canogar    7 年前

    对于复杂的关联查询,建议使用 Containable go deeper 而不仅仅是 recursive = -1 在model/find调用中,不进行手动连接。

    设置Containable之后,您的查询可能类似于*

    $this->Apple->find('all', array(
        'contain' => array(
            'Box' => array(
                'Room' => array(
                    'conditions' => array('Room.name LIKE' => 'room0%')
                )
            )
        )
    ));
    

    *)只是我记忆中的模型,不能保证按原样工作

        2
  •  1
  •   Ben    7 年前

    在cakephp2中,包含是自动连接的 recursive => -1 在你的电话里。