/** * Test getting associations by property. * * @return void */ public function testGetByProperty() { $table = $this->getMockBuilder('Cake\\ORM\\Table')->setMethods(['table'])->getMock(); $table->schema([]); $belongsTo = new BelongsTo('Users', ['sourceTable' => $table]); $this->assertEquals('user', $belongsTo->property()); $this->associations->add('Users', $belongsTo); $this->assertNull($this->associations->get('user')); $this->assertSame($belongsTo, $this->associations->getByProperty('user')); }
/** * Returns the association named after the passed value if exists, otherwise * throws an exception. * * @param string $property the association name * @return \Cake\ORM\Association * @throws \RuntimeException if no association with such name exists */ public function __get($property) { $association = $this->_associations->get($property); if (!$association) { throw new RuntimeException(sprintf('Table "%s" is not associated with "%s"', get_class($this), $property)); } return $association; }