Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use non-primary columns in crossref tables #1994

Merged
merged 8 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
);
}
}
Loading