Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    /**
     * test that saving habtm records respects conditions relating to an associated model
     * set in the 'conditions' key for the association.
     *
     * @return void
     */
        public function testHabtmSaveWithAssociatedModelConditionsInAssociation() {
            $this->loadFixtures('JoinThing', 'Something', 'SomethingElse');
            $Something = new Something();
            $Something->unbindModel(array('hasAndBelongsToMany' => array('SomethingElse')), false);
 
            $Something->bindModel(array(
                'hasAndBelongsToMany' => array(
                    'DoomedSomethingElse' => array(
                        'className' => 'SomethingElse',
                        'joinTable' => 'join_things',
                        'conditions' => array('Something.published' => '1'),
                        'unique' => true
                    ),
                    'NotDoomedSomethingElse' => array(
                        'className' => 'SomethingElse',
                        'joinTable' => 'join_things',
                        'conditions' => array('Something.published' => '0'),
                        'unique' => true
                    )
                )
            ), false);
            $result = $Something->read(null, 1);
            $this->assertTrue(empty($result['NotDoomedSomethingElse']));
            $this->assertEquals(count($result['DoomedSomethingElse']), 1);
 
            $data = array(
                'Something' => array('id' => 1),
                'NotDoomedSomethingElse' => array(
                    'NotDoomedSomethingElse' => array(
                        array('something_else_id' => 2, 'doomed' => 0),
                        array('something_else_id' => 3, 'doomed' => 0)
                    )
                )
            );
            $Something->create($data);
            $result = $Something->save();
            $this->assertFalse(empty($result));
 
            $result = $Something->read(null, 1);
            $this->assertEquals(count($result['NotDoomedSomethingElse']), 2);
            $this->assertEquals(count($result['DoomedSomethingElse']), 1);
        }