Join不会按预期填充对象图

时间:2022-10-06 19:44:52

I have a small misunderstanding in how the joining in Doctrine2 work. We have a pretty complex structure in our app and we are building management screens for it.

我对加入Doctrine2的工作方式有一点误解。我们的应用程序中有一个非常复杂的结构,我们正在为它构建管理屏幕。

The area of concern is as follows:

关注的领域如下:

Once of the objects 'Application' looks like this:

一旦对象'Application'看起来像这样:

class Application

{ /** * List of supported statuses * * @var array */ private $statuses = array('released', 'expired');

{/ ** *支持的状态列表* * @var array * / private $ statuses = array('released','expired');

/**
 *
 * @var integer $id
 *
 * @Column(name="id", type="integer", columnDefinition="INT(10) UNSIGNED")
 * @Id
 * @GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @Column(name="Name", type="string", length=100)
 */
private $name;

/**
 * @var Customer
 *
 * @ManyToOne(targetEntity="libraries\persona\Entity\Customer")
 * @JoinColumn(name="Customers_id", referencedColumnName="id", nullable=true, columnDefinition="INT(10) UNSIGNED")
 */
private $customer;

/**
 * @var integer
 *
 * @Column(name="Partners_id", type="integer", nullable=true)
 */
private $partnerId;

/**
 * @var string
 *
 * @Column(name="appID", type="string", length=48)
 */
private $appId;

/**
 * @var string
 *
 * @Column(name="status", type="string", length=15)
 */
private $status;

/**
 * @var \DateTime
 *
 * @Column(name="eventStartDate", type="date")
 */
private $eventStartDate;

/**
 * @var \DateTime
 *
 * @Column(name="eventEndDate", type="date")
 */
private $eventEndDate;

/**
 * @var string
 *
 * @Column(name="timeZone", type="string", length=45)
 */
private $timeZone;

/**
 * @ManyToOne(targetEntity="libraries\application\Entity\ApplicationType", inversedBy="applications")
 * @JoinColumn(name="ApplicationTypes_id", referencedColumnName="id")
 */
private $applicationType;

/**
 * @var integer
 *
 * @Column(name="syncPeriod", type="integer", length=10, nullable=true)
 */
private $syncPeriod;

/**
 * @var \DateTime
 *
 * @Column(name="lastSync", type="datetime", nullable=true)
 */
private $lastSync;

/**
 * @var string
 *
 * @Column(name="lastSyncStatus", type="string", length=127, nullable=true)
 */
private $lastSyncStatus;

/**
 * @var string
 *
 * @Column(name="syncScript", type="string", length=250, nullable=true)
 */
private $syncScript = '/var/www/quickstart/application/controllers/scripts/qdissync.php';

/**
 * @var integer
 *
 * @Column(name="size", type="integer", length=10)
 */
private $size = 400;

/**
 * @var text
 *
 * @Column(name="metaData", type="text", nullable=true)
 */
private $metaData;

/**
 * @var \DateTime
 *
 * @Column(name="metaDataChangedTime", type="datetime", nullable=true)
 */
private $metaDataChangedTime;

/**
 * @var tinyint
 *
 * @Column(name="isSecure", type="smallint", length=1)
 */
private $isSecure = 2;

/**
 * @var integer
 *
 * @Column(name="Users_id", type="integer", nullable=true)
 */
private $userId;

/**
 * @var string
 *
 * @Column(name="singleEventAppId", type="string", length=48, nullable=true)
 */
private $singleEventAppId;

/**
 * @var string
 *
 * @Column(name="project_db", type="string", length=50)
 */
private $projectDb;

/**
 * @var string
 *
 * @Column(name="appName", type="string", length=12)
 */
private $appName;

/**
 * @var boolean
 *
 * @Column(name="mobileLog", type="boolean")
 */
private $mobileLog = false;

/**
 * @var tinyint
 *
 * @Column(name="eventType", type="smallint", length=1)
 */
private $eventType = 1;

/**
 * @ManyToOne(targetEntity="libraries\application\Entity\Project", inversedBy="applications")
 * @JoinColumn(name="project_id", referencedColumnName="id")
 */
private $project;

/**
 * @OneToMany(targetEntity="libraries\platform\Entity\Platform", mappedBy="application")
 **/
private $platforms;

/**
 * @var SnapEventAttributes
 *
 * @OneToOne(targetEntity="libraries\application\Entity\SnapEventAttributes", mappedBy="application")
 **/
private $snapEventAttributes;

//GETTERS & SETTERS

}

One of the related objects 'SnapEventAttributes':

其中一个相关对象'SnapEventAttributes':

class SnapEventAttributes

{ /** * * @var integer * * @Column(name="id", type="integer", columnDefinition="INT(11)") * @Id * @GeneratedValue(strategy="AUTO") */ private $id;

{/ ** * * @var integer * * @Column(name =“id”,type =“integer”,columnDefinition =“INT(11)”)* @Id * @GeneratedValue(strategy =“AUTO”)* /私人$ id;

/**
 * @var string
 *
 * @Column(name="directSelectId", type="string", length=10, nullable=true)
 */
private $directSelectId;

/**
 * @var string
 *
 * @Column(name="description", type="text", nullable=true)
 */
private $description;

/**
 * @var string
 *
 * @Column(name="access", type="string", length=10, nullable=false)
 */
private $access;

/**
 * @var string
 *
 * @Column(name="status", type="string", length=10, nullable=false)
 */
private $status;

/**
 * @var string
 *
 * @Column(name="snapAppVersion", type="string", length=5, nullable=true)
 */
private $snapAppVersion;

/**
 * @var string
 *
 * @Column(name="pwd", type="string", length=250, nullable=true)
 */
private $password;

/**
 * @var string
 *
 * @Column(name="thumbnailUrl", type="string", length=250, nullable=true)
 */
private $thumbnailUrl;

/**
 * @var string
 *
 * @Column(name="location", type="string", length=45, nullable=true)
 */
private $location;

/**
 * @OneToOne(targetEntity="libraries\application\Entity\Application", inversedBy="snapEventAttributes")
 * @JoinColumn(name="Applications_id", referencedColumnName="id", unique=true, nullable=false)
 */
private $application;

// GETTERS & SETTERS

}

In one of our work flows we need to join these two with bunch of additional joins and we got something like this:

在我们的一个工作流程中,我们需要将这两个连接加入一系列额外的连接,我们得到这样的结果:

$qb = $this->createQueryBuilder('e');
    $qb->innerJoin('e.applicationType', 'et', Expr\Join::WITH, 'et.mobileEvent = :mobileEvent AND et.snapApp = :snapApp AND et.snapEvent = :snapEvent');
    $qb->innerJoin('e.snapEventAttributes', 'attrs');
    $qb->innerJoin('e.project', 'p');
    $qb->innerJoin('p.applications', 'a');
    $qb->innerJoin('a.applicationType', 'at', Expr\Join::WITH, 'at.mobileEvent = :vMobileEvent AND at.snapApp = :vSnapApp AND at.snapEvent = :vSnapEvent');
    $qb->leftJoin('e.customer', 'cust');
    $qb->leftJoin('p.partner', 'partn');
    $qb->setParameters(array(
        'mobileEvent' => false,
        'snapApp' => false,
        'snapEvent' => true,
        'vMobileEvent' => false,
        'vSnapApp' => true,
        'vSnapEvent' => false,
    ));

    return $qb;

All the logic works perfectly fine, however once I execute this query:

所有逻辑都可以正常工作,但是一旦我执行了这个查询:

$qb->getQuery()->getResult();

the main query is getting executed as expected but automatically doctrine executes bunch of queries to get the SnapEventAttributes objects:

主查询按预期执行,但自动doctrine执行一堆查询以获取SnapEventAttributes对象:

SELECT t0.id AS id1, t0.directSelectId AS directSelectId2, t0.description AS description3, t0.access AS access4, t0.status AS status5, t0.snapAppVersion AS snapAppVersion6, t0.pwd AS pwd7, t0.thumbnailUrl AS thumbnailUrl8, t0.location AS location9, t0.Applications_id AS Applications_id10 FROM SnapEventAttributes t0 WHERE t0.Applications_id = ? 

What am I missing? What could be a reason for this behavior?

我错过了什么?这种行为可能是什么原因?

Thanks, A.

1 个解决方案

#1


get the repository

获取存储库

Controller

$snapEvents = $this->em->snapEvents ( 'Entity\SnapEventAttributes' )->findAll ();
    $data ['snapEvents '] = $snapEvents ;

then on the view

然后在视图上

View

foreach ($snapEvents $sanpEvent) {

                echo "<tr>".
                "<td>" . $sanpEvent->getAppliction()->getApplicationType() . "</td>".

                "<td>" . $sanpEvent->getAppliction()->getCutomer() . "</td>".
                }

Like this keep on calling what u want since u have already joined columns at the model

像这样继续调用你想要的东西,因为你已经在模型中加入了列

#1


get the repository

获取存储库

Controller

$snapEvents = $this->em->snapEvents ( 'Entity\SnapEventAttributes' )->findAll ();
    $data ['snapEvents '] = $snapEvents ;

then on the view

然后在视图上

View

foreach ($snapEvents $sanpEvent) {

                echo "<tr>".
                "<td>" . $sanpEvent->getAppliction()->getApplicationType() . "</td>".

                "<td>" . $sanpEvent->getAppliction()->getCutomer() . "</td>".
                }

Like this keep on calling what u want since u have already joined columns at the model

像这样继续调用你想要的东西,因为你已经在模型中加入了列