php - 如何在 doctrine2 中找到实体

标签 php mysql symfony doctrine-orm

我的实体开发人员具有现场角色、$unsubscribeDate、$tags,我需要找到以下人员: - 担任自由职业者, - 取消订阅日期 < NowDate - tag not=: blacklist, unsubscribed (inDB a:2:{i:0;s:12:"unsubscribed";i:1;s:9:"blacklist";}) 或 tag have = a:0:{ }

我不知道怎么做,请帮忙 这个实体

/**
* Developers
*
* @ORM\Table(name="developers")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\DeveloperRepository")
*/
class Developer extends CustomUser
{
    /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
    /**
 * @var \DateTime
 * @ORM\Column(name="unsubscribe_date", type="date")
 */
protected $unsubscribeDate;

/**
 * @var string
 *
 * @ORM\Column(name="tags", type="array", nullable=true)
 */
private $tags = array();
    /**
 * @var string
 *
 * @ORM\Column(name="role", type="string", length=25, nullable=true)
 */
protected $roles;

示例 kike 找到具有 $role 和 unsubscribeDate < nowTime 并且没有 a:2:{i:0;s:12:"unsubscribed";i:1;s:9:"blacklist";} 的开发人员但是如果开发者有 a:3:{i:0;s:12:"unsubscribed";i:1;s:9:"blacklist";i:2;s:7:"company";} 这个开发者添加列表 - 我如何检查这个是错误的???而且我不知道为什么字段标签 = NULL 的开发人员不添加到这个列表中??:

public function getWhoNotSendEmail($role, $tags)
{
    $date = new \DateTime;

    $qb = $this->getEntityManager()->createQueryBuilder('d');

    $qb
        ->select('d')
        ->from('ArtelProfileBundle:Developer', 'd')
        ->where('d.roles = :role')
        ->andWhere('d.unsubscribeDate <= :departureDate')
        ->setParameter('departureDate', $date)
        ->setParameter('role', $role)

        ->getQuery();

    $query = $qb->getQuery();
    $results = $query->getResult();

    $arrayResults = array();
    foreach ($results as $result) {

        if (is_array($result->getTags())) {
            if (count(array_diff($result->getTags(), $tags)) > 0) {
                $arrayResults[] = $result;
            }
        }
    }

    return $arrayResults;
}

最佳答案

你不能这样做,如果标签只是一个,你可以这样做,如果它是一个数组,你就不能。

一个解决方案可能是查询从标签中获取所有值,然后,如果 array_diff > 0 意味着您保留该开发人员

public function getWhoSendEmail($role, $bl)
{
    $date = new \DateTime;

    $qb = $this->getEntityManager()->createQueryBuilder('d');

    $qb
        ->select('d')
        ->from('ArtelProfileBundle:Developer', 'd')
        ->where('d.roles = :role')
        ->andWhere('d.unsubscribeDate <= :departureDate')
        ->setParameter('role', $role)
        ->setParameter('departureDate', $date)
        ->getQuery()
    ;

    $query = $qb->getQuery();
    $results = $query->getResult();

    $arrayResults = array();
    foreach ($results as $result) {

        $arrayTags = explode(",", $result->getTags());
        if (count(array_diff($arrayTags, $b1)) > 0) {
            $arrayResults[] = $result;
        }
    }

    return $arrayResults;
}

如果每行只有一个标签,你可以用

->andWhere('d.tag NOT IN (:tags)')

但事实并非如此......

关于php - 如何在 doctrine2 中找到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31456459/

相关文章:

php - 警告 : odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

php - 在 mysqli 中存储和迭代查询结果

php - Symfony 4 启动 session

php - 呈现具有特定状态代码的模板

php - 如果字段为空另一个字段需要 Laravel 验证规则

php - 如何使用 nl2br 只显示一个换行符?

java - 在 JAVA 中使用 session 用户的 MYSQL 查询

mysql 5.6 : group_concat help sought

javascript - Symfony 中的 Ajax 和 Jquery

php - 连续检查mysql数据库中的数据并使用php