Issue with Query Doctrine and zend 2 - php

I have doctrine and zend 2 I do this script
$dql = $entityManager->createQuery('SELECT g FROM \Synchro\Entity\Geographicalarea g WHERE (g.namegeographicalarea = :namegeographicalarea ) AND g.codegeo = :codegeo');
$dql->setParameters(array(
'namegeographicalarea' => '$this->GetSQLValueString($nameGeographicalArea,"text")',
'codegeo' => '$this->GetSQLValueString($nameGeographicalArea,"text")',
));
$checkgeographicalarea = $query->getResult();
I var_dumped ($checkgeographicalarea) It was empty. I don't know why because I have the data
how can I resolved that ?

As long as you use Doctrine 2 and since your query was very simple i advise you to use this
$entityManager->getRepository('Synchro\Entity\Geographicalarea')->findBy(array(
'namegeographicalarea' => $nameGeographicalArea,
'codegeo' => $nameGeographicalArea,
));
Plus you have declared '$this->GetSQLValueString($nameGeographicalArea,"text")', with simple quotes wich is not working in PHP. and your codego = $nameGeographicalAreais this what you want ?
You have many issues here.

Related

How to get all rows from related table using join in Zend Framework

I have two tables. First table contain offer details and second table contains offer collection of languages(collection can be selected in main offer form). I want to use query with "join" language table to get all of them in single query. How it's look like:
Relation
Trying to get all of the offers I want to get all offer languages as one field.
array [
'id' => string '11',
'name' => string '134',
'date' => string '01-12-2016',
'languages' => array(all language value from related table)
]
Here is my query:
$select = $this->getDbTable()->select()
->setIntegrityCheck(false)
->from(array('o' => 'offers'), array('o.*'))
->joinLeft(array('ol' => 'offer_language'), 'ol.id_offer = o.id', array('ol.*'));
$resultSet = $this->getDbTable()->fetchAll($select)
In that way if one offer has a three language then I get three same offer with different language field.
Zend Version: 1.11
Thanks for help.
The solution is to use a Zend_Db_Expr with GROUP_CONCAT.
Here are two posts that will give you a clear explanation:
Can I concatenate multiple MySQL rows into one field? and How to use GROUP_CONCAT with Zend Framework?
Your code will end up looking something like this:
$select = $this->getDbTable()->select()
->setIntegrityCheck(false)
->from(array('o' => 'offers'), array('o.*'))
->joinLeft(array('ol' => 'offer_language'), 'ol.id_offer = o.id', array('languages' => new Zend_Db_Expr('GROUP_CONCAT(ol.language)')))
->group('o.id');
Good luck!

zend framework paginator does not work?

I use ZendFramework Paginator and I have some code like this:
$defaultCount=1000;
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from(array('u' => 'core_users'));
$select->join(array('ur' => 'core_users_roles'), 'u.uid = ur.uid');
$select->join(array('r' => 'core_roles'), 'r.rid = ur.rid');
$adapter=new Zend_Paginator_Adapter_DbSelect($select);
$adapter->setRowCount($db->select()->from('core_users',array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN =>'uid')));
$paginator= new Zend_Paginator($adapter);
$paginator->setItemCountPerPage($defaultCount);
$paginator->setCurrentPageNumber($page);
but ,i can not get all of my data from DbSelect Adapter.when i remove $defaultCount , it always give me 20 total data (default ,I guess). should i use single table ?
$db->select()->from('core_users',array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN =>'uid'))
give me wrong number. I use hard code ,works on me.

Comparing 2 fields within a Mongo Aggregation Query

I have a collection in my Mongo Database called WorkOrder with 2 fields DateComplete and DateDue. Using those 2 fields I'd like to use the aggregation framework to count the number of 'Late' Work Orders by comparing the two fields. However the research I've found hasn't had any useful ways to format the query so that the 'Late' Work Orders will be filtered through. Does anyone know of a way to format a Mongo DB Aggregation Query (preferably in PHP) that can compare 2 fields in the collection?
EDIT:
An example entry in WorkOrder might look like
_id
some mongo id
DateDue
2014-10-10
DateCompleted
2014-10-12
This entry would want to be filtered through since DateCompleted is greater than DateDue. I didn't know about the $cond operator so I haven't tried anything for that yet.
EDIT:
After trying #BatScream's suggestion with the following query in my PHP script
array(
'$cond' => array(
'if' => array(
'dateDue' => array(
'$lt' => 'dateComplete
)
)
)
)
However the MongoCollection::Aggregate function told me that $cond wasn't a recognized operator.
EDIT: #BatScream's answer seems to work but I wasn't aware of the fact that the group operator doesn't work properly after a $project is applied. I was hoping to be able to group these document on another field cID, is that possible?
The below aggregation pipeline would give you the result, considering your fields are of ISODate type. If not i suggest you to store them as ISODate type and not Strings.
db.collection.aggregate([
{$project:{"isLateWorkOrder":{$cond:[{$lt:["$dateDue","$dateCompleted"]},
true,false]}}},
{$match:{"isLateWorkOrder":true}},
{$group:{"_id":null,"lateWorkOrders":{$sum:1}}},
{$project:{"_id":0,"lateWorkOrders":1}}
])
The PHP syntax should look similar to,
$projA = array("isLateWorkOrder" =>
array("$cond" =>
array(array("$lt" =>
array("$dateDue","$dateCompleted")),
true,false)))
$matchA = array("isLateWorkOrder" => true)
$grp = array("_id" => null,"lateWorkOrders" => array("$sum" => 1))
$projB = array("_id" => 0,"lateWorkOrders" => 1)
$pipeline = array($projA,$matchA,$grp,$projB);
$someCol -> aggregate($pipeline)
or, simply using the count function:
db.collection.count({$where:"this.dateDue < this.dateCompleted"})

get a single value from a table using the query() method inside a model

I have a very complex setup on my tables and achieving this via any of the find() methods is not an option for me, since I would need to fix relationships between my tables and I don't have the time right now, so I'm looking for a simple fix here.
All I want to achieve is run a query like this:
SELECT MAX( id ) as max FROM MyTable WHERE another_field_id = $another_field_id
Then, I need to assign that single id to a variable for later use.
The way I have it now it returns something like [{{max: 16}}], I'm aware I may be able to do some PHP on this result set to get the single value I need, but I was hoping there was already a way to do this on CakePHP.
Assuming you have a model for your table and your are using CakePHP 2.x, do:
$result = $this->MyTable->field('id', array('1=1'), 'id DESC');
This will return a single value.
see Model::field()
This example is directly from the CakePHP documentation. it seems you can use the find method of a model to get count
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array(
'conditions' => array('Article.status' => 'pending')
));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array(
'fields' => 'DISTINCT Article.user_id',
'conditions' => array('Article.status !=' => 'pending')
));

Can we use multiple where clause in Zends delete()?

I am trying to use multiple where() with Zends delete(), but nothing is happening..!!!
$where=array('id' => $id,'likedbyID' => $likedbyID);
$this->delete($where);
The above code is written inside a Model.
Please help me.....
Thanks in advance.....
From zend documentation :
Since the table delete() method proxies to the database adapter delete() method, the argument can also be an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.
Knowing all that you can use it like this :
$this->delete(
array(
'id = ?' => $id,
'likedbyID = ?' => $likedbyID,
)
);
Allso you could use the > or < or IN or .. operators instead of =

Categories