1<?php
2
3class Doctrine_Ticket_1876b_TestCase extends Doctrine_UnitTestCase
4{
5    public function init()
6    {
7        Doctrine_Manager::connection('mysql://root:password@localhost/doctrine', 'Mysql');
8        $this->driverName = 'Mysql';
9        parent::init();
10        Doctrine_Manager::connection('mysql://root:password@localhost/doctrine', 'Mysql');
11        $this->prepareTables();
12        $this->prepareData();
13    }
14
15    public function run(DoctrineTest_Reporter $reporter = null, $filter = null)
16    {
17      parent::run($reporter, $filter);
18      $this->manager->closeConnection($this->connection);
19    }
20
21    public function prepareData()
22    {
23    }
24
25    public function prepareTables()
26    {
27        try {
28            $this->conn->exec('DROP TABLE t1876b_recipe_ingredient');
29            $this->conn->exec('DROP TABLE t1876b_recipe');
30            $this->conn->exec('DROP TABLE t1876b_company');
31        } catch(Doctrine_Connection_Exception $e) {
32        }
33
34        $this->tables = array(
35            'T1876b_Recipe', 'T1876b_Company', 'T1876b_RecipeIngredient'
36        );
37
38        parent::prepareTables();
39    }
40
41    public function testDuplicatedParamsInSubQuery()
42    {
43        $this->connection->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
44
45        for ($i = 0; $i < 2; $i++) {
46            $company = new T1876b_Company();
47            $company->name = 'Test Company ' . ($i + 1);
48            $company->save($this->connection);
49        }
50
51        for ($i = 0; $i < 10; $i++) {
52            $recipe = new T1876b_Recipe();
53
54            $recipe->name = 'test ' . $i;
55            $recipe->company_id = ($i % 3 == 0) ? 1 : 2;
56            $recipe->RecipeIngredients[]->name = 'test';
57
58            $recipe->save($this->connection);
59
60            if ($i % 2 == 0) {
61                $recipe->delete($this->connection);
62            }
63        }
64
65        try {
66            $q = Doctrine_Query::create()
67                ->from('T1876b_Recipe r')
68                ->leftJoin('r.Company c')
69                ->leftJoin('r.RecipeIngredients')
70                ->addWhere('c.id = ?', 2);
71
72            $this->assertEqual(
73                $q->getCountSqlQuery(),
74                'SELECT COUNT(*) AS num_results FROM ('
75                    . 'SELECT t.id FROM t1876b__recipe t '
76                    . 'LEFT JOIN t1876b__company t2 ON t.company_id = t2.id AND t2.deleted_at IS NULL '
77                    . 'LEFT JOIN t1876b__recipe_ingredient t3 ON t.id = t3.recipe_id AND t3.deleted_at IS NULL '
78                    . 'WHERE t2.id = ? AND (t.deleted_at IS NULL) GROUP BY t.id'
79                . ') dctrn_count_query'
80            );
81            $this->assertEqual($q->count(), 3);
82        } catch (Exception $e) {
83            $this->fail($e->getMessage());
84        }
85
86        $this->connection->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, false);
87    }
88}
89
90
91class T1876b_Recipe extends Doctrine_Record {
92    public function setTableDefinition() {
93        $this->hasColumn('id', 'integer', null, array('autoincrement' => true, 'primary' => true));
94        $this->hasColumn('company_id', 'integer', null);
95        $this->hasColumn('name', 'string', 255);
96    }
97
98    public function setUp() {
99        $this->hasOne('T1876b_Company as Company', array('local' => 'company_id', 'foreign' => 'id'));
100        $this->hasMany('T1876b_RecipeIngredient as RecipeIngredients', array('local' => 'id', 'foreign' => 'recipe_id'));
101
102        $this->actAs('SoftDelete');
103    }
104}
105
106class T1876b_Company extends Doctrine_Record {
107    public function setTableDefinition() {
108        $this->hasColumn('id', 'integer', null, array('autoincrement' => true, 'primary' => true));
109        $this->hasColumn('name', 'string', 255);
110    }
111
112    public function setUp() {
113        $this->hasMany('T1876b_Recipe as Recipes', array('local' => 'id', 'foreign' => 'company_id'));
114
115        $this->actAs('SoftDelete');
116    }
117}
118
119class T1876b_RecipeIngredient extends Doctrine_Record {
120    public function setTableDefinition() {
121        $this->hasColumn('id', 'integer', null, array('autoincrement' => true, 'primary' => true));
122        $this->hasColumn('recipe_id', 'integer', null);
123        $this->hasColumn('name', 'string', 255);
124    }
125
126    public function setUp() {
127        $this->hasOne('T1876b_Recipe as Recipe', array('local' => 'recipe_id', 'foreign' => 'id'));
128
129        $this->actAs('SoftDelete');
130    }
131}