public function test_for_has_and_belongs_to_many() { $Property = new Property(array('description' => 'Gandia Palace')); $this->assertEqual($Property->property_type->getType(), 'hasAndBelongsToMany'); $this->assertTrue(is_array($Property->property_types) && count($Property->property_types) === 0); $Property->property_type->load(); $this->assertEqual($Property->property_type->count(), 0); $Chalet = new PropertyType(array('description' => 'Chalet')); $Property->property_type->add($Chalet); $this->assertEqual($Property->property_type->count(), 1); $this->assertReference($Property->property_types[0], $Chalet); $Property->property_type->add($Chalet); $this->assertEqual($Property->property_type->count(), 1); $Condo = new PropertyType(array('description' => 'Condominium')); $Property->property_type->add($Condo); $this->assertEqual($Property->property_type->count(), 2); $this->assertTrue($Property->save()); $this->assertFalse($Chalet->isNewRecord()); $this->assertFalse($Condo->isNewRecord()); $this->assertTrue($Chalet = $Chalet->findFirstBy('description', 'Chalet', array('include' => 'properties'))); $this->assertEqual($Chalet->properties[0]->getId(), $Property->getId()); $this->assertTrue($Condo = $Condo->findFirstBy('description', 'Condominium', array('include' => 'properties'))); $this->assertEqual($Condo->properties[0]->getId(), $Property->getId()); $this->assertReference($Chalet, $Property->property_types[0]); $this->assertReference($Condo, $Property->property_types[1]); $Property = new Property($Property->getId()); $Property->property_type->load(); $this->assertEqual($Property->property_type->association_id, 'property_types'); $this->assertEqual($Property->property_type->count(), 2); $Property->property_types = array(); $this->assertEqual($Property->property_type->count(), 0); $Property->property_type->load(); $this->assertEqual($Property->property_type->count(), 0); $Property->property_type->load(true); $this->assertEqual($Property->property_type->count(), 2); $this->assertEqual($Property->property_types[1]->getType(), 'PropertyType'); $Property->property_type->delete($Property->property_types[1]); $this->assertEqual($Property->property_type->count(), 1); $Property->property_type->load(true); $this->assertEqual($Property->property_type->count(), 1); $Property =& $Property->findFirstBy('description', 'Gandia Palace'); $PropertyType = new PropertyType(); $PropertyTypes =& $PropertyType->find(); $Property->property_type->set($PropertyTypes); $this->assertEqual($Property->property_type->count(), count($PropertyTypes)); $Property =& $Property->findFirstBy('description', 'Gandia Palace'); $Property->property_type->load(); $this->assertEqual($Property->property_type->count(), count($PropertyTypes)); $Property =& $Property->findFirstBy('description', 'Gandia Palace'); $PropertyType->set('description', 'Palace'); $Property->property_type->set($PropertyType); $this->assertEqual($Property->property_type->count(), 1); $this->assertTrue(in_array('property_types', $Property->getAssociatedIds())); $Property = $Property->findFirstBy('description', 'Gandia Palace', array('include' => 'property_types')); $this->assertIdentical($Property->property_type->count(), 1); $this->assertTrue($Property->property_type->delete($Property->property_types[0])); $this->assertIdentical($Property->property_type->count(), 0); $Property = $Property->findFirstBy('description', 'Gandia Palace'); $this->assertIdentical($Property->property_type->count(), 0); // It should return existing Property even if it doesnt have property_types $this->assertTrue($Property->findFirstBy('description', 'Gandia Palace', array('include' => 'property_types'))); $Property = new Property(array('description' => 'Luxury Downtown House')); $Apartment =& $PropertyType->create(array('description' => 'Apartment')); $Loft =& $PropertyType->create(array('description' => 'Loft')); $Penthouse =& $PropertyType->create(array('description' => 'Penthouse')); $Property->property_type->setByIds(array($Apartment->getId(), $Loft->getId(), $Penthouse->getId())); $this->assertEqual($Property->property_type->count(), 3); $this->assertTrue($Property->save()); $this->assertTrue($Property->save()); $this->assertTrue($Property =& $Property->findFirstBy('description', 'Luxury Downtown House')); $Property->property_type->load(); $this->assertEqual($Property->property_type->count(), 3); $FoundApartment = $Property->property_type->find('first', array('description' => 'Apartment')); $this->assertEqual($Apartment->get('description') . $Apartment->getId(), $FoundApartment->get('description') . $FoundApartment->getId()); $FoundTypes = $Property->property_type->find(); $this->assertEqual(count($FoundTypes), $Property->property_type->count()); $descriptions = array(); foreach ($FoundTypes as $FoundType) { $descriptions[] = $FoundType->get('description'); } sort($descriptions); $this->assertEqual($descriptions, array('Apartment', 'Loft', 'Penthouse')); $this->assertFalse($Property->property_type->isEmpty()); $this->assertEqual($Property->property_type->getSize(), 3); $this->assertTrue($Property->property_type->clear()); $this->assertTrue($Property->property_type->isEmpty()); $this->assertEqual($Property->property_type->getSize(), 0); $Property = new Property(); $LandProperty =& $Property->property_type->build(array('description' => 'Land')); $this->assertReference($LandProperty, $Property->property_types[0]); $this->assertTrue($Property->property_types[0]->isNewRecord()); $this->assertEqual($LandProperty->getType(), 'PropertyType'); $Property->set('description', 'Plot of Land in Spain'); $this->assertTrue($Property->save()); $this->assertTrue($LandProperty = $Property->findFirstBy('description', 'Plot of Land in Spain', array('include' => 'property_types'))); $this->assertEqual($LandProperty->property_types[0]->get('description'), 'Land'); $Property = new Property(array('description' => 'Seaside house in Altea')); $SeasidePropertyType =& $Property->property_type->create(array('description' => 'Seaside property')); $this->assertReference($SeasidePropertyType, $Property->property_types[0]); $this->assertTrue($SeasidePropertyType->isNewRecord()); $Property = new Property(array('description' => 'Bermi\'s appartment in Altea')); $this->assertTrue($Property->save()); $SeasidePropertyType =& $Property->property_type->create(array('description' => 'Seaside property')); $this->assertReference($SeasidePropertyType, $Property->property_types[0]); $this->assertFalse($SeasidePropertyType->isNewRecord()); $this->assertTrue($PropertyInAltea = $Property->findFirstBy('description', 'Bermi\'s appartment in Altea', array('include' => 'property_types'))); $this->assertEqual($PropertyInAltea->property_types[0]->get('description'), 'Seaside property'); // Testing destroy callbacks $this->assertTrue($Property =& $Property->findFirstBy('description', 'Bermi\'s appartment in Altea')); $property_id = $Property->getId(); //echo '<pre>'.print_r($Property->_associations, true).'</pre>'; $this->assertTrue($Property->destroy()); $RecordSet = $PropertyInAltea->_db->execute('SELECT * FROM properties_property_types WHERE property_id = ' . $property_id); $this->assertEqual($RecordSet->RecordCount(), 0); }