我将用户链接到事件实体。基本上,用户可能想参加几个事件,并且通过许多人到许多事件,我正在注册谁想参加X活动。
我需要使用学说构建查询,如果用户可用(可用设置为1),或者他们打算根据其位置去参加活动(我也得到了这个)。第一个条件很好。但是对于第二个,我需要确保用户至少有一个他想参加的事件,并在许多关系中注册:EventNotified。
。在php中,我会做一个foreach循环,然后条件如果$ user-> getEventNotified()不是一个空数组(或null),但这会让我提起太多结果。我想用SQL或DQL
选择它我该怎么做?
这是我到目前为止所取得的成就,如果用户想参加事件,我只需要添加一个或条件(Eventnotified而不是null?):
//We only want to return users nearby who are available OR who
$qb = $this->_em->getRepository( 'EntityUser' )->createQueryBuilder('a');
$qb->andwhere( 'a.latitude > :minLat' );
$qb->andWhere( 'a.latitude < :maxLat' );
$qb->andWhere( 'a.longitude > :minLng' );
$qb->andWhere( 'a.longitude < :maxLng' );
$qb->andWhere( 'a.available = 1' );
$qb->add( 'orderBy', $qb->expr()->sum( 'a.latitude - :lat', 'a.longitude - :lng' ));
$array = array(
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLng' => $minLng,
'maxLng' => $maxLng,
'lat' => $lat,
'lng' => $lng
);
$qb->setParameters( $array );
// $qb->setFirstResult( $offset );
// $qb->setMaxResults( $limit );
// print_r(array(
// 'sql' => $qb->getQuery()->getSQL(),
// 'parameters' => $qb->getQuery()->getParameters(),
// ));
$usersNearby = $qb->getQuery()->getResult();
return $usersNearbt;
这是我的用户实体:
class User
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer", nullable=false, unique=true)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORMColumn(name="fb_id", type="bigint", nullable=false, unique=true)
*/
private $fb_id;
/**
* @var string
*
* @ORMColumn(name="firstname", type="string", length=100, nullable=false, unique=false)
*/
private $first_name;
/**
* @var string
*
* @ORMColumn(name="lastname", type="string", length=100, nullable=true, unique=false)
*/
private $last_name;
/**
* @var string
*
* @ORMColumn(name="email", type="string", length=255, nullable=true, unique=true)
*/
private $email;
/**
* @var integer
*
* @ORMColumn(name="notation", type="integer", nullable=true, unique=true)
*/
private $notation;
/**
* Bidirectional - Many users have Many favorite comments (OWNING SIDE)
*
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="EntityCategory", inversedBy="userInterests")
*/
private $interests;
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="EntityUser", cascade={"persist"})
* @ORMJoinTable(name="friends",
* joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
**/
private $friends;
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMOneToMany(targetEntity="EntityRequest", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
* @ORMJoinColumn(nullable=true)
*/
private $requests;
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMOneToMany(targetEntity="EntityRequest", mappedBy="friend", cascade={"remove"}, orphanRemoval=true)
* @ORMJoinColumn(nullable=true)
*/
private $notifications;
/**
* Bidirectional - Many users have notified they want to go to different events (OWNING SIDE)
*
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="EntityEvent", inversedBy="userNotified", cascade={"persist"})
*/
private $eventNotified;
/**
* @var integer
*
* @ORMColumn(name="age", type="integer", length=3, nullable=true, unique=false)
*/
private $age;
/**
* @var string
*
* @ORMColumn(name="description", type="text", nullable=true, unique=false)
*/
private $description;
/**
* @var string
*
* @ORMColumn(name="picture", type="string", length=300, nullable=true, unique=false)
*/
private $picture;
/**
* @var string
*
* @ORMColumn(name="genre", type="string", length=10, nullable=true, unique=false)
*/
private $genre;
/**
* @var boolean
*
* @ORMColumn(name="isregistered", type="boolean", length=1, nullable=false, unique=false)
*/
private $registered;
/**
* @var string
*
* @ORMColumn(name="latitude", type="decimal", length=64, precision=25, scale=20, nullable=true, unique=false)
*/
private $latitude;
/**
* @var string
*
* @ORMColumn(name="longitude", type="decimal", length=64, precision=25, scale=20, nullable=true, unique=false)
*/
private $longitude;
/**
* @var EntitySecurity_Key
*
* @ORMOneToOne(targetEntity="EntitySecurity_Key", cascade={"persist","remove"}, orphanRemoval=true)
* @ORMJoinColumns({
* @ORMJoinColumn(name="private_key_id", referencedColumnName="id", unique=true, onDelete="SET NULL")
* })
*/
private $private_key;
/**
* @var boolean
*
* @ORMColumn(name="isavailable", type="boolean", length=1, nullable=false, unique=false)
*/
private $available = 0;
...
编辑:
感谢下面的解决方案,我对其进行了一些调整(和一些优化):
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select( 'USER', 'EVENT', 'c', 'p' )
->from( 'EntityUser', 'USER' )
->innerJoin( 'USER.eventNotified', 'EVENT' )
->leftJoin( 'EVENT.categories', 'c' )
->leftJoin( 'EVENT.place', 'p' )
->add('where', $qb->expr()->andX(
$qb->expr()->between('USER.latitude', ':minLat', ':maxLat'),
$qb->expr()->between('USER.longitude', ':minLng', ':maxLng')
)
)
->add( 'orderBy', $qb->expr()->sum( 'USER.latitude - :lat', 'USER.longitude - :lng' ) );
$array = array(
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLng' => $minLng,
'maxLng' => $maxLng,
'lat' => $lat,
'lng' => $lng
);
$qb->setParameters( $array );
$usersNearby = $qb->getQuery()->getResult();
您需要加入 ManyTomany entity。如果用户没有任何 event 相关(通过您的 user.eventNotified 属性),则不会检索他/她。我还将使用 expr QUERYBUILDER 的类构建条件:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select( 'USER' )
->from( 'EntityUser', 'USER' )
->innerJoin( 'USER.eventNotified' )
->where(
$qb->expr()->andX(
$qb->expr()->gt( 'USER.latitude', ':minLat' ),
$qb->expr()->lt( 'USER.latitude', ':maxLat' ),
$qb->expr()->gt( 'USER.longitude', ':minLng' ),
$qb->expr()->lt( 'USER.longitude', ':maxLng' ),
$qb->expr()->eq( 'USER.available', 1 )
)
);
(...) //orderBy and parameters
检查QueryBuilder文档。