shuba.ivan
shuba.ivan

Reputation: 4061

How to find entity in doctrine2

I have entity developer who has field role, $unsubscribeDate, $tags and I need find developer who: - have role freelancer, - unsubscribeDate < NowDate - tag not=: blacklist, unsubscribed (inDB a:2:{i:0;s:12:"unsubscribed";i:1;s:9:"blacklist";}) or tag have = a:0:{}

How I do this I dont know, help please this entity

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

example kike that find developer who has $role and unsubscribeDate < nowTime and who not have a:2:{i:0;s:12:"unsubscribed";i:1;s:9:"blacklist";} but if developer have a:3:{i:0;s:12:"unsubscribed";i:1;s:9:"blacklist";i:2;s:7:"company";} This developer add in list - it is wrong how I check this ??? And I dont know why developer who have field tags = NULL not add in this list??:

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

Upvotes: 2

Views: 87

Answers (1)

Fernando Caraballo
Fernando Caraballo

Reputation: 583

You cannot do that, you could do it if tags were just one, if it's an array you cannot.

A solution could be doing the query getting all of the values from tags and then, if array_diff > 0 means that you keep this developer

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

If it were just one tag per row you could do it with

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

But it not the case...

Upvotes: 1

Related Questions