I try to translate this query into Criteria (with Propel), but without success.
Can you help me please ?
SELECT DISTINCT (email)
FROM user, travail
WHERE travail.id_user = user.id_user
AND id_site = "1"
AND `droits` = "1"
This my Criteria query :
$c = new Criteria();
$c->add(self::DROITS, 1, Criteria::EQUAL);
$c->add(TravailPeer::ID_SITE, 1, CRITERIA::EQUAL);
$c->setDistinct(self::EMAIL);
How about this:
$c = new Criteria();
$c->add(UserPeer::DROITS, 1);
$c->addJoin(UserPeer::ID_USER, TravailPeer::ID_USER);
$c->add(TravailPeer::ID_SITE, 1);
$c->clearSelectColumns();
$c->addSelectColumn(UserPeer::EMAIL);
$c->setDistinct();
$rs = UserPeer::doSelectRS($c);
Hi You can use propel builder to translate not only this but any sql to the criteria. following is one of the online builder site.
http://propel.jondh.me.uk/
Related
I have been trying to create nodes and relations ships for our new module with neo4jphp [https://github.com/jadell/neo4jphp/wiki].
I am using cypher queries for the same.
Creating nodes with below query:
$queryNodes = "CREATE (n:User { props } ) ";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryNodes, array('props' => $arrNodeProperties));
$result = $query->getResultSet();
Creating relationships with below query:
$queryRelations = "
MATCH (authUser: User { userid: 0001 }),(friend)
WHERE friend.userid IN ['" . implode("','",$relations) . "']
CREATE UNIQUE (authUser)-[r:KNOWS { connection: 'user_friend' }]->(friend)";
So far node creation works gr8.
But when i try to create Unique relationships for the nodes, it takes too long....
Note:
There is unique constraint userid for label User, hence node with label user is indexed by Neo4j on property userid.
CREATE CONSTRAINT ON (user:User) ASSERT user.userid IS UNIQUE
Questions:
Is there any other way we can achieve creating unique relationships.
Can i use index on relationships?? If Yes how can I achieve the same.
You might try use use MERGE instead of CREATE UNIQUE. Additionally use a Cypher parameter for the fried's list instead of concatenation on client side, see http://docs.neo4j.org/chunked/stable/cypher-parameters.html
Finally I worked it out with few changes...
Thanks #MichaelHunger for the help.
So here is how i did it...
Creating Unique Nodes using MERGE, FOREACH, ON CREATE SET and params:
$queryNodes = "
FOREACH (nodeData IN {nodeProperties}|
MERGE (n:User { userid: nodeData.userid })
ON CREATE SET
n.login = nodeData.login,
n.userid = nodeData.userid,
n.username = nodeData.username,
n.name = nodeData.name,
n.gender = nodeData.gender,
n.profile_pic = nodeData.profile_pic,
n.create_date = timestamp()
ON MATCH SET
n.update_date = timestamp()
)
";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryNodes, array('nodeProperties' => $arrNodeProperties));
$result = $query->getResultSet();
Creating Unique Relationships with below query:
$queryRelations = "
MATCH (authUser: User { userid: {authUserid} }), (friend:User)
WHERE friend.userid IN {friendUserIds}
CREATE UNIQUE (authUser)-[r:KNOWS { connection: 'user_friend' }]->(friend)
";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryRelations, array('friendUserIds' => $arrFriendUserId, 'authUserid' => $authUserid));
$result = $query->getResultSet();
Please comment if we can improve the same even further.
How to use having() clause in ZF2?
There is almost no examples on the web how to prepare correct select object with having.
I have query like:
SELECT root_schema_id as `schema_id`
FROM `standard_specific_root_schemas`
WHERE `vehicle_id` IN (".implode(",",$vehiclesIds).")
GROUP BY `schema_id`, rootSubGroup_id HAVING count(*)=".$noOfVehicles
And I'm trying to run it in ZF2:
public function getVehicleWithinCommonRootSubgroupInSpecific($vehiclesIds)
{
$where = new Where();
$where->in('vehicle_id', $vehiclesIds);
$having = new Having('count(*) = '.count($vehiclesIds));
$rowset = $this->tableGateway->select(function (Select $select) use ($where, $having) {
$select
->where($where)
->having($having);
});
if (!$rowset) {
throw new \Exception("Could not find schemas for group $groupId");
}
return $rowset;
}
Of course that part in ZF2 is not finished yet as I wanted to check if it's working first.
I've tried few ways of providing params to having method but everything generates errors.
Help please, I'm desperate...
I cannot test your query, but can try and reproduce the query you need.
I adjusted the having to use ->expression() instead of a variable via the construct.
I also added the group statement.
To view the query I added a var_dump:
$where = new \Zend\Db\Sql\Where();
$where->in('vehicle_id', $vehiclesIds);
$having = new \Zend\Db\Sql\Having();
$having->expression('count(*) = ?', count($vehiclesIds));
$rowset = $this->tableGateway->select(function (\Zend\Db\Sql\Select $select) use ($where, $having) {
$select
->where($where)
->group(array('schema_id', 'rootSubGroup_id'))
->having($having);
var_dump( $select->getSqlString() );
});
Let me know if this helps.
To circumvent the error mentioned in the comments you would have to do something like below:
$sql = $this->tableGateway->getSql();
$select = $sql->select();
$where = new \Zend\Db\Sql\Where();
$where->in('vehicle_id', $vehiclesIds);
$having = new \Zend\Db\Sql\Having();
$having->expression('count(*) = ?', count($vehiclesIds));
$select
->where($where)
->group(array('schema_id', 'rootSubGroup_id'))
->having($having);
$preparedQuery = $sql->prepareStatementForSqlObject($select);
var_dump( $preparedQuery->getSql() );
However, if I'm right, the tableGateway does this for you so the error should go away once you start using the select to query the database.
Also, you can use the above to do that too, just replace this:
$preparedQuery = $sql->prepareStatementForSqlObject($select);
var_dump( $preparedQuery->getSql() );
With:
$this->tableGateway->selectWith($select);
I'm trying to make a select like this:
SELECT c.*, CONCAT(c.provider_id,'#',c.name") FROM contact AS c
so, I'm writing something like this...
$sql = new Sql($this->adapter);
$query = $sql->select()
->from(array('c' => 'contact'))
->columns(array("CONCAT(c.provider_id,'#',c.name"), false)
but, result is:
SELECT c``CONCAT(c.provider_id,'#',c.name AS
CONCAT(c.provider_id,'#',c.name FROM contact AS c
What am i doing wrong?
Thanks for any help!
When i have to extract some columns from a table and add a Sql function, i usually use this code:
$sql = new Sql($this->adapter);
$query = $sql->select()
->from(array('c' => 'contact'))
->columns(array(
'id', 'name', 'data' => new Expression('CONCAT(c.provider_id,'#',c.name)')
)
);
Expression is an instance of Zend\Db\Sql\Expression, the result is:
SELECT `id`, `name`, CONCAT(c.provider_id,'#',c.name) AS `data` FROM `contact` AS `c`
check out Database Expressions if you need to use MySQL functions or anything else which you don't want to be escaped automatically for you. some examples:
https://github.com/ralphschindler/Zend_Db-Examples
$sql = new Sql($this->adapter);
$query = $sql->select()
->from(array('c' => 'contact'))
->columns(array(
'*', new Expression("CONCAT(c.provider_id,'#',c.name) as data")
))
;
I have this sql:
SELECT link.ID, link.URL, link.ANCHOR, link.HOME, link.CREATED_AT
FROM `link`, `linkcategory`
WHERE link.ID=linkcategory.LINK_ID
GROUP BY linkcategory.LINK_ID
HAVING count(linkcategory.category_id) =(select count(*) from categoria)
And I'm trying to generate the criteria, this is the criteria that doesn't work:
$c = new Criteria();
$c->addJoin(self::ID, LinkcategoryPeer::LINK_ID);
$c->addAsColumn('catCount','(SELECT COUNT(*) FROM CATEGORIA)');
$c->addGroupByColumn(LinkcategoryPeer::LINK_ID);
$having = $c->getNewCriterion(count(LinkcategoryPeer::CATEGORY_ID),$c->getColumnForAs('catCount'));
$c->addHaving($having);
return self::doSelect($c);
The returning sql of this criteria is this one:
SELECT (SELECT COUNT(*) FROM CATEGORIA) AS catCount
FROM `link`, `linkcategory`
WHERE link.ID=linkcategory.LINK_ID
GROUP BY linkcategory.LINK_ID
HAVING 1='(SELECT COUNT(*) FROM CATEGORIA)'
I really don't know why the criteria convert the sql incorrectly. Anyone knows where is the mistake?
Try with this query. I don't remember how to perform a link.* using Propel. But I think the problem with having is that you are using the PHP count function insteand of the MySQL one.
$c = new Criteria();
$c->addSelectColumn(self::ID);
$c->addSelectColumn(self::URL);
$c->addSelectColumn(self::ANCHOR);
$c->addSelectColumn(self::HOME);
$c->addSelectColumn(self::CREATED_AT );
$c->addJoin(self::ID, LinkcategoryPeer::LINK_ID);
$c->addGroupByColumn(LinkcategoryPeer::LINK_ID);
$having = $c->getNewCriterion(
'COUNT('.LinkcategoryPeer::CATEGORY_ID.')'),
$c->getColumnForAs('catCount')
);
$c->addHaving($having);
return self::doSelect($c);
i don't know how to translate this query :
SELECT distinct(id_ville) FROM `point_location`
I try to do it, but it doesn't work :
$c = new Criteria();
$c->add(PointLocationPeer::ID_VILLE, Criteria::DISTINCT);
$c->setDistinct();
$this->villes = PointLocationPeer::doSelect($c);
Try something like this:
$c = new Criteria();
$c->addSelectColumn(PointLocationPeer::ID_VILLE);
$c->setDistinct();
$this->villes = PointLocationPeer::doSelect($c);