如何在主义中加入多对多关系?

时间:2018-07-04 11:57:02

标签: php join doctrine jointable

我有一些实体:

供应商:

/**
 * @ORM\Entity(repositoryClass="Blah\Blah\ApiClientBundle\Entity\SupplierRepository")
 */
class Supplier
{
    [...]

    /**
     * @var ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="Blah\SopBundle\Entity\SupplierUser", mappedBy="suppliers")
     */
    protected $users;

    [...]
}

SupplierUser:

/**
 * @ORM\Entity(repositoryClass="SupplierUserRepository")
 */
class SupplierUser
{
    [...]

        /**
         * @var ArrayCollection
         *
         * @ORM\ManyToMany(targetEntity="\Blah\Blah\ApiClientBundle\Entity\Supplier", inversedBy="users", cascade={"persist"})
         * @ORM\JoinTable(name="supplier_user_supplier",
         *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="CASCADE")},
         *      inverseJoinColumns={@ORM\JoinColumn(name="supplier_id", referencedColumnName="supplier_id")}
         * )
         * @Assert\Valid()
         */
        protected $suppliers;

    [...]
}

现在在一个存储库中,我将上述实体与一个报告(rpr)结合在一起:

public function findBrandsBySupplierUser(SupplierUser $supplierUser)
{
    return array_column(
        $this
            ->createQueryBuilder('rpr')
            ->join(Supplier::class, 's', 'WITH', 's.supplierId = rpr.supplierId')
            ->join(SupplierUser::class, 'su')
            ->where('su = :supplierUser')
            ->setParameters([
                'supplierUser' => $supplierUser,
            ])
            ->select('rpr.brand')
            ->distinct()
            ->orderBy('rpr.brand')
            ->getQuery()
            ->getArrayResult(),
        'brand'
    );
}

但是结果是:

SELECT DISTINCT r0_.brand AS brand_0 FROM returnable_products_report r0_ 
INNER JOIN supplier s1_ ON (s1_.supplier_id = r0_.supplier_id)
INNER JOIN supplier_user s2_
WHERE s2_.user_id = ?
ORDER BY r0_.brand ASC

所以我收到笛卡尔联接。为什么教义不能自动加入?

我可以选择强制加入字段,但是我不知道该怎么做。在这种情况下join(SupplierUser::class, 'su')应该如何显示?

1 个答案:

答案 0 :(得分:0)

有效答案似乎是:

<Window x:Class="WpfInkCavasSaveImage.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"  Height="1091" Width="873" WindowState="Maximized">

<Grid Margin="0,0,0,10" >
    <Grid.RowDefinitions>
        <RowDefinition Height="1200*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <InkCanvas HorizontalAlignment="Stretch" Margin="1,1,1,10" x:Name="inkCanvas1" VerticalAlignment="Stretch" Width="Auto" RenderTransformOrigin="0.5,0.5" Background="LightGreen" SnapsToDevicePixels="True" IsManipulationEnabled ="True"  Grid.RowSpan="2">
        <InkCanvas.CacheMode>
            <BitmapCache/>
        </InkCanvas.CacheMode>
        <InkCanvas.DefaultDrawingAttributes>
            <DrawingAttributes Color="Black" FitToCurve="True" Height="2.0031496062992127" IgnorePressure="False" IsHighlighter="False" StylusTip="Ellipse" StylusTipTransform="Identity" Width="2.0031496062992127"/>

        </InkCanvas.DefaultDrawingAttributes>
    </InkCanvas>
</Grid>
</Window>


public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        inkCanvas1.Width = System.Windows.SystemParameters.WorkArea.Width;
        inkCanvas1.Height = System.Windows.SystemParameters.WorkArea.Height;
    }
}

代替

->join('s.users', 'su')