Symfony 3学说资料库联接和数组

时间:2018-06-26 14:07:21

标签: sql symfony doctrine-orm

我正在使用symfony 3,但我的存储库中的构建器出现了一些问题。

工作方式: 我有一个公告,其中有很多信息,例如姓名,游戏等... 然后用户通过关系OneToOne链接

我从您的公告实体发送了一部分

class Announce
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
      * @ORM\OneToOne(targetEntity="Wolk\UsersBundle\Entity\User")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

      /**
     * @var string
     *
     * @ORM\Column(name="game", type="string", length=255)
     */
    private $game;


    Etc...

现在您可以看到我的存储库

public function byResearch($role, $game, $region, $language, $rank, $gamemode, $matchtype, $platform)
    {

        $qb = $this->createQueryBuilder('u')
                ->select('u');

        if ($language != null) {
                $qb->join('u.user' , 's');
                $qb->addSelect('s');
                $qb->andWhere('s.language like \''.$language.'\'');
        }




        if ($gamemode!= null) {
            $qb->andWhere('u.gamemode = \''.$gamemode.'\'');
        }

        if ($matchtype!= null) {
            $qb->andWhere('u.matchtype = \''.$matchtype.'\'');
        }

        if ($region!= null) {
            $qb->andWhere('u.region = \''.$region.'\'');
        }

        if ($rank!= null) {
            $qb->andWhere('u.rank like \'%'.$rank.'%\'');
        }

        if ($platform!= null) {
            $qb->andWhere('u.platform like \'%'.$platform.'%\'');
        }

        if ($game!= null) {
            $qb->andWhere('u.game = \''.$game.'\'');
        }

        if ($role!= null) {
            foreach($role as $itm1)
            {
                $qb->andWhere('u.role like \'%'.$itm1.'%\'' );
            }
        }
        $qb->andwhere('u.active = :active');
        $qb->setParameter('active', '1');
        $qb->orderBy('u.date', 'DESC');



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

    }

我的问题与用户使用的语言有关

人们只会搜索法语,德语和英语,并且所有用户在实体用户中拥有所有语言

User.Language = Array('fr','en')//类似于

实际上,我在我的网站上有此结果(来自探查器)

SELECT 
  a0_.id AS id_0, 
  a0_.game AS game_1, 
  a0_.platform AS platform_2, 
  a0_.Availability AS Availability_3, 
  a0_.language AS language_4, 
  a0_.Description AS Description_5, 
  a0_.category AS category_6, 
  a0_.goal AS goal_7, 
  a0_.Rank AS Rank_8, 
  a0_.active AS active_9, 
  a0_.premium AS premium_10, 
  a0_.level AS level_11, 
  a0_.visit AS visit_12, 
  a0_.region AS region_13, 
  a0_.role AS role_14, 
  a0_.exp AS exp_15, 
  a0_.lan AS lan_16, 
  a0_.gamemode AS gamemode_17, 
  a0_.matchtype AS matchtype_18, 
  a0_.date AS date_19, 
  f1_.username AS username_20, 
  f1_.username_canonical AS username_canonical_21, 
  f1_.email AS email_22, 
  f1_.email_canonical AS email_canonical_23, 
  f1_.enabled AS enabled_24, 
  f1_.salt AS salt_25, 
  f1_.password AS password_26, 
  f1_.last_login AS last_login_27, 
  f1_.confirmation_token AS confirmation_token_28, 
  f1_.password_requested_at AS password_requested_at_29, 
  f1_.roles AS roles_30, 
  f1_.id AS id_31, 
  f1_.gender AS gender_32, 
  f1_.birthday AS birthday_33, 
  f1_.subscribedate AS subscribedate_34, 
  f1_.Country AS Country_35, 
  f1_.language AS language_36, 
  f1_.timezone AS timezone_37, 
  a0_.user_id AS user_id_38, 
  f1_.image_id AS image_id_39, 
  f1_.premium_id AS premium_id_40 
FROM 
  announce a0_ 
  INNER JOIN fos_user f1_ ON a0_.user_id = f1_.id 
WHERE 
  f1_.language LIKE 'fr' 
  AND a0_.platform LIKE '%PC%' 
  AND a0_.game = 'lol' 
  AND a0_.active = ? 
ORDER BY 
  a0_.date DESC

我真的不知道语言搜索有什么问题

加入关系对于OneToOne可能是错误的?

也许“像哪里”这样的数组不是一个好的解决方案?

从一天开始,我一直在搜索,不知道哪些事情不起作用,所以我希望您可以在此方面提供帮助:)

如果您需要更多信息,我们将很高兴

1 个答案:

答案 0 :(得分:2)

您忘记在语言值周围添加百分号。应该是:

$qb->andWhere('s.language like \'%'.$language.'%\'');