Join不会按预期填充对象图

时间:2015-05-13 20:36:44

标签: mysql codeigniter join doctrine

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

关注的领域如下:

一旦对象'应用程序'看起来像这样:

class Application

{     / **      *支持的状态列表      *      * @var数组      * /     private $ statuses = array('已发布','已过期');

/**
 *
 * @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

}

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

class SnapEventAttributes

{     / **      *      * @var整数      *      * @Column(name =" id",type =" integer",columnDefinition =" INT(11)")      * @ID      * @GeneratedValue(策略=" 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

}

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

$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;

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

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

主要查询按预期执行,但自动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 = ? 

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

谢谢, 甲

1 个答案:

答案 0 :(得分:0)

获取存储库

控制器

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

然后在视图

查看

foreach ($snapEvents $sanpEvent) {

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

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

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