| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | require_once "lib/Doctrine.php"; |
|---|
| 4 | |
|---|
| 5 | spl_autoload_register(array('Doctrine', 'autoload')); |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class Foo extends Doctrine_Record { |
|---|
| 9 | public function setTableDefinition() { |
|---|
| 10 | $this->hasColumn('id', 'integer', 4, array('primary' => true)); |
|---|
| 11 | } |
|---|
| 12 | public function setUp() { |
|---|
| 13 | $this->hasMany('Bar as bars', array('local' => 'foo_id', 'foreign' => 'bar_id', 'refClass' => 'Foobar')); |
|---|
| 14 | } |
|---|
| 15 | }; |
|---|
| 16 | |
|---|
| 17 | class Bar extends Doctrine_Record { |
|---|
| 18 | public function setTableDefinition() { |
|---|
| 19 | $this->hasColumn('id', 'integer', 4, array('primary' => true)); |
|---|
| 20 | } |
|---|
| 21 | public function setUp() { |
|---|
| 22 | $this->hasMany('Foo as foos', array('local' => 'bar_id', 'foreign' => 'foo_id', 'refClass' => 'Foobar')); |
|---|
| 23 | } |
|---|
| 24 | }; |
|---|
| 25 | |
|---|
| 26 | class Foobar extends Doctrine_Record { |
|---|
| 27 | public function setTableDefinition() { |
|---|
| 28 | $this->hasColumn('foo_id', 'integer', 4, array('primary' => true)); |
|---|
| 29 | $this->hasColumn('bar_id', 'integer', 4, array('primary' => true)); |
|---|
| 30 | } |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | $pdo = new PDO("mysql:host=localhost;dbname=doctrine", "doctrine", "test"); |
|---|
| 35 | |
|---|
| 36 | $pdo->query("DROP TABLE foo"); |
|---|
| 37 | $pdo->query("DROP TABLE bar"); |
|---|
| 38 | $pdo->query("DROP TABLE foobar"); |
|---|
| 39 | |
|---|
| 40 | $pdo->query("CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY)"); |
|---|
| 41 | $pdo->query("CREATE TABLE bar (id INTEGER NOT NULL PRIMARY KEY)"); |
|---|
| 42 | $pdo->query("CREATE TABLE foobar (foo_id INTEGER NOT NULL, bar_id INTEGER NOT NULL, PRIMARY KEY (foo_id, bar_id))"); |
|---|
| 43 | |
|---|
| 44 | $pdo->query("INSERT INTO foo VALUES (1),(2)"); |
|---|
| 45 | $pdo->query("INSERT INTO bar VALUES (1),(2)"); |
|---|
| 46 | $pdo->query("INSERT INTO foobar VALUES (1,1),(2,2)"); |
|---|
| 47 | |
|---|
| 48 | $db = Doctrine_Manager::connection($pdo, "pdo"); |
|---|
| 49 | $db->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true); |
|---|
| 50 | |
|---|
| 51 | $foo_t = $db->getTable("Foo"); |
|---|
| 52 | $foo = $foo_t->find(1); |
|---|
| 53 | |
|---|
| 54 | // Below line causes exception |
|---|
| 55 | $bars = $foo->bars; |
|---|