Skip to content

Commit

Permalink
Merge pull request #1994 from vol4onok/patch-crossref
Browse files Browse the repository at this point in the history
Allow to use non-primary columns in crossref tables
  • Loading branch information
gechetspr authored Mar 29, 2024
2 parents f4889ff + 2f29c23 commit 2da1345
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Propel/Generator/Model/CrossForeignKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function getIncomingForeignKey(): ?ForeignKey
*/
public function isAtLeastOneLocalPrimaryKeyNotCovered(ForeignKey $fk): bool
{
$primaryKeys = $fk->getLocalPrimaryKeys();
$primaryKeys = $fk->getLocalColumnObjects();
foreach ($primaryKeys as $primaryKey) {
$covered = false;
foreach ($this->getCrossForeignKeys() as $crossFK) {
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Model/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public function isAtLeastOneLocalColumnRequired(): bool
*/
public function isAtLeastOneLocalPrimaryKeyIsRequired(): bool
{
foreach ($this->getLocalPrimaryKeys() as $pk) {
foreach ($this->getLocalColumnObjects() as $pk) {
if ($pk->isNotNull() && !$pk->hasDefaultValue()) {
return true;
}
Expand Down
98 changes: 98 additions & 0 deletions tests/Propel/Tests/Issues/IssueIsCrossRefTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* MIT License. This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Propel\Tests\Issues;

use TestGroupObject;
use TestUserObject;
use TestGroupObjectNegative;
use TestUserObjectNegative;
use Propel\Generator\Util\QuickBuilder;
use Propel\Tests\TestCase;

/**
* Regression test for https://github.com/propelorm/Propel2/pull/1994
*/
class IssueIsCrossRefTest extends TestCase
{
/**
* @return void
*/
public function setUp(): void
{
if (!class_exists('\TestUserGroupObject')) {
$schema = <<<EOF
<database>
<table name="test_group_object">
<column name="id" type="integer" primaryKey="true" required="true" />
</table>
<table name="test_user_object">
<column name="id" type="integer" primaryKey="true" required="true" />
</table>
<table name="test_group_object_negative">
<column name="id" type="integer" primaryKey="true" required="true" />
</table>
<table name="test_user_object_negative">
<column name="id" type="integer" primaryKey="true" required="true" />
</table>
<table name="test_user_group_object" isCrossRef="true">
<column name="id" type="integer" primaryKey="true" required="true" />
<column name="user_id" type="integer" required="true"/>
<column name="group_id" type="integer" required="true"/>
<foreign-key foreignTable="test_user_object">
<reference local="user_id" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="test_group_object">
<reference local="group_id" foreign="id"/>
</foreign-key>
</table>
<table name="test_user_group_object_negative" isCrossRef="true">
<column name="id" type="integer" primaryKey="true" required="true" />
<column name="user_negative_id" type="integer"/>
<column name="group_negative_id" type="integer"/>
<foreign-key foreignTable="test_user_object_negative">
<reference local="user_negative_id" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="test_group_object_negative">
<reference local="group_negative_id" foreign="id"/>
</foreign-key>
</table>
</database>
EOF;
QuickBuilder::buildSchema($schema);
}
}

/**
* @return void
*/
public function testGenerateIsCrossRefCode()
{
$testGroupObject = new TestGroupObject();
$testUserObject = new TestUserObject();
$testGroupNegative = new TestGroupObjectNegative();
$testUserNegative = new TestUserObjectNegative();

$this->assertTrue(
method_exists($testGroupObject, 'createTestUserObjectsQuery'),
'Class does not have method createTestUserObjectsQuery'
);
$this->assertTrue(
method_exists($testUserObject, 'createTestGroupObjectsQuery'),
'Class does not have method createTestUserObjectsQuery'
);
$this->assertFalse(
method_exists($testGroupNegative, 'createTestUserObjectNegativesQuery'),
'Class does not have method createTestUserObjectsQuery'
);
$this->assertFalse(
method_exists($testUserNegative, 'createTestGroupObjectNegativesQuery'),
'Class does not have method createTestUserObjectsQuery'
);
}
}

0 comments on commit 2da1345

Please sign in to comment.