I want to use Collection#Matching against associated objects, but it isn't working. Doing some digging it looks like Doctrine does an in_array call against the two values. The needle is a Persisted Collection (my association) and the haystack is an array of the entities I want to match. Because the needle is a Persisted Collection, the match fails.
Is this possibly a bug or are associations not supported? If they're not supported, is there a work around?
Example:
$query = $em->createQuery("SELECT c FROM Entity\BidCategory c WHERE c.code IN(:categories)");
$query->setParameter('categories', array('CATEGORY_1', 'CATEGORY_2'));
$my_categories = $query->getResult();
$criteria = array(
"min_pub_date" => "01-07-2012",
"max_pub_date" => "01-08-2012"
);
$query = $em->createQuery("SELECT b From Entity\Bid b JOIN b.categories c WHERE b.pub_date > :min_pub_date AND b.pub_date < :max_pub_date");
$query->setParameter("min_pub_date", new DateTime($criteria['min_pub_date']));
$query->setParameter("max_pub_date", new DateTime($criteria['max_pub_date']));
$query->setMaxResults(1);
$bids = $query->getResult();
$criteria = Criteria::create()
->where(Criteria::expr()->in("categories", $my_categories));
$collection = new Doctrine\Common\Collections\ArrayCollection();
foreach($bids as $bid)
{
$collection->add($bid);
}
$matched_bids = $collection->matching($criteria);
The Criteria feature doesn't support associations. The Criteria::expr()->in() method is for regular arrays.
Related
I have a query result same as below:
var ptns = from p in db.Patients
select p;
This query returns a list of patients, but I need to filter the result based on DoctorNameID. The DoctorNameID should be in list of doctors as below:
List<string> listofDoctors = usrtodrs.Split(',').ToList();
I have searched a lot but I don't know how to do this. I have tested this query which doesn't work:
var ptns1 = from d in listofDoctors
join p in ptns.ToList() on d equals p.DoctorNameID
select p;
And also this query:
var ptns1 = ptns.ToList()
.Where(a => listofDoctors.Equals(a.DoctorNameID))
.ToList();
Any help?
You can use Contains extension and get the desired result.
var ptns1 = ptns.Where(x => listofDoctors.Contains(x.DoctorNameID)).ToList();
Refer the C# Fiddle with sample data.
i try to use NativeQuery of Doctrine. If i use my SQL ind Phpmyadmin i have the good result. If i use this, my var_dump return an empty array. I don't understand why.
$sql = 'SELECT COUNT(id) as nb FROM app_facture f WHERE SUBSTR(f.datecreate_facture, 1, 4) = 2014 AND SUBSTR(f.numero_facture,1,1) != "E"';
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Acme\MyBundle\Entity\Facture', 'f');
$rsm->addFieldResult('f', 'COUNT(id)', 'nb');
$query = $this->getEntityManager()->createNativeQuery($sql,$rsm);
$results = $query->getResult();
var_dump($results);//return empty array
Thanks
Try
$rsm->addFieldResult('f', 'nb', 'id');
this worked for me.
I'm trying to do a simple select query with a subquery in the SELECT clause and have simply not found a way to do it. I've tried with both DQL and with the QueryBuilder, neither work. The code follows, please don't say I could just use a join, this is a simplified example just to illustrate the problem, I have legitimate use cases for subqueries.
// With QueryBuilder
$query = $qb->select(array('a',
'(SELECT at.addresstypeName
FROM e:Addresstype at
WHERE at.addresstypeId = a.addresstypeId
) AS addresstypeName'))
->from('e:Address', 'a')
->where('a.addressId = :addressId')
->setParameter('addressId', 1);
// With DQL
$dql = "SELECT a,
(SELECT at.addresstypeName
FROM e:Addresstype at
WHERE at.addresstypeId = a.addresstypeId
) AS addresstypeName
FROM e:Address a
WHERE a.addressId = :addressId";
$query = $em->createQuery($dql)->setParameter(':addressId', 1);
The following relationship is defined on the Address table:
/**
* #ORM\ManyToOne(targetEntity="Addresstype")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="addresstype_id", referencedColumnName="addresstype_id")
* })
*/
protected $addresstype;
In native SQL, the query would look like this:
SELECT
a.*,
(
SELECT at.addresstype_name
FROM addresstype at
WHERE at.addresstype_id = a.addresstype_id
) AS addresstype_name
FROM address a
WHERE a.address_id = 1
Any ideas?
$query = $qb->select('a')
->addSelect('(SELECT at.addresstypeName
FROM e:Addresstype at
WHERE at.addresstypeId = a.addresstypeId) AS addresstypeName'
)
->from('e:Address', 'a')
->where('a.addressId = :addressId')
->setParameter('addressId', 1);
For me subquery with doctrine works with this query :
$qb->select('e.field')
->addSelect('(SELECT count(mv.nm)
FROM Clt\Bundle\MyBundle\Entity\MV mv
LEFT JOIN Clt\Bundle\MyBundle\Entity\M ma WITH mv.nm=ma.nm
WHERE mv.ne=e.ne and ma.nm is null
) AS nm'
)
->from($this->_entityName, 'e')
->leftJoin('e.m', 'm')
->where($qb->expr()->eq('t.id'.$typeModule, $idElementModule));
Note that in the left join you must use WITH instead of ON...
I know this is an old question, but if you want, you could have used another query builder as your subquery:
$qb->select("a")
->addSelect("(" . $qb2->select("at.addresstypeName")
->from("e:Addresstype", "at")
->where("at.addresstypeId = a.addresstypeId")
->getDQL() . ") AS addresstypeName"
)
->from('e:Address', 'a')
->where('a.addressId = :addressId')
->setParameter('addressId', 1);
In my scenario what I needed was to look into a join and find an Id and use it as boolean, found 1 otherwise 0, then applying this to orderBy. DQL expressions worked only when combined with Where clause, which wasn't my case. So, a DQL subselect saved me.
Adapted more or less to your scenario, it would look like this:
// With QueryBuilder
// In AddressRepository
// Where one address may belong to several addressTypes
public function getWithType($addressType){
$qb = $this->createQueryBuilder('a1');
$qb->addSelect('a1.someField', 'a1.otherField')
$qb->addSelect(
'(SELECT at.addressTypeName
FROM App\Entity\Address a2
JOIN a2.addressType at
WHERE at.id = '.$addressType.' AND a2.id = a1.id
) AS addressTypeName')
//The rest part of the query
}
I perform a simple query like this to fetch an association with episodes:
$query = $this->getEntityManager()
->createQuery('
SELECT p,e
FROM AcmeDemoBundle:Place p
LEFT JOIN p.episodes e
WHERE p.id = :id'
)
->setParameter('id',$id);
This is a simple asso:
/**
* #ORM\OneToMany(targetEntity="Episode", mappedBy="place")
*/
protected $episodes;
This works well. Now, I don't want to fetch episodes, but simply the place object (and nothing else):
$query = $this->getEntityManager()
->createQuery('
SELECT p
FROM AcmeDemoBundle:Place p
LEFT JOIN p.episodes e
WHERE p.id = :id'
)
->setParameter('id',$id);
This is still loading episodes by lazy-loading. Is there a way to avoid lazy-loading in that case?
Many thanks.
Like this:
$query = $this->getEntityManager()
->createQuery('
SELECT p
FROM AcmeDemoBundle:Place p
WHERE p.id = :id'
)
->setParameter('id',$id);
$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
How can I add a dynamic key to an anonymous List such as the mydatetime below:
DateTime myDateTime = DateTime.Parse(datepickerval, ukCulture.DateTimeFormat);
var qid = (from p in db.Vw_INTERACTPEOPLE
select p
);
var AvilList = new List<object>();
var ddate = myDateTime.DayOfWeek.ToString().Substring(0, 3) + "Jul" + myDateTime.Day;
foreach (var q in qid)
{
AvilList.Add(
new
{// Availability
Name = q.Fullname,
here >>> ddate = "Some Test"
});
As adam says above there is no way to do this using Lists, however since the Slickgrid is expecting a Json return, I simply built the string in .net then returned it via the JavaScriptSerializer serializer, then in the code behind simply used eval to de-serialize back into an array.