DBAL - symfony2 bind a value for LIKE operator - symfony

I am trying to execute a sql query that involves a LIKE operator with DBAL
Basically my query is the following:
public function getSubsiteByHostname($host){
$sql = "SELECT A.id, A.title, A.layout_id
FROM sites AS A
LEFT JOIN layouts B
ON A.layout_id = B.id
WHERE A.baseurl LIKE '%:host%'
";
$stmt = $this->db->prepare($sql);
$stmt->bindValue("host", $host);
$stmt->execute();
return $stmt->fetch();
}
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%'hostname.dev'%
Obiviously I'm doing something wrong with the bindValue

The answer is easier than I thought, like Adam suggested
$stmt->bindValue("host", '%'.$host.'%');

Related

Mulesoft- Sql select connector query issue

I see the below error while connect and pull the records from the database through the Mule Database connector with an Oracle database. Can someone look at it and let me know what's wrong with this query.
Error:
"java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended\n"
type: DB:BAD_SQL_SYNTAX
Query:
SELECT A.sample_central_id,A.req_ndc_prod_id,B.lot_num,A.req_product_desc,A.req_hcp_fst_name,A.req_hcp_last_name,A.req_prof_desig,A.az_hcp_id,A.req_addr_line_1,A.req_addr_line_2,A.req_city,A.req_state_cd,A.req_zipcode,A.ffevnt_shipped_qty,A.ffevnt_ship_dt FROM scrf.ship_prod_hist_vw A,scrfval.ship_dtl B WHERE Trunc(A.req_sample_requested_dt)>= Trunc(SYSDATE - 30)AND Trunc(A.req_sample_requested_dt)<= Trunc(SYSDATE + 10)AND Trunc(A.ffevnt_ship_dt)>= Trunc(SYSDATE - 8)AND Trunc(A.ffevnt_ship_dt)<Trunc(SYSDATE - 1)AND Upper(A.lkp_brand_desc) LIKE '%LYNPARZA%' AND A.sample_central_id = B.sc_req_id AND A.ffevnt_ff_vendor_id = B.shipment_id AND A.req_ndc_prod_id = B.ndc_prod_id AND A.ffevnt_shipped_qty<>0 ORDER BY 1;
Try removing the semicolon (';') at the end.

Symfony3.4 / Doctrine : subquery in FROM Clause : Error: Class 'SELECT' is not defined

I'm working on a Symfony 3.4 project and I'm trying to translate an sql query to DQL query but I get an Issue.
Mysql Query:
select sum(montant_paye)
from
(select montant_paye
from vente
where client_id = 1
and montant_paye > 0
order by date ASC
limit 2)
as T;
DQL Query (Error):
return $this->getEntityManager()
->createQuery('
SELECT SUM(montantPaye) as Total
FROM
SELECT v.montantPaye
FROM AppBundle:Vente v
where v.montantPaye > 0
AND v.client = '.$clientId.'
ORDER BY v.date ASC
limit 2
')
->getResult();
Error :
[Semantical Error] line 0, col 71 near 'SELECT v.montantPaye
': Error: Class 'SELECT' is not defined.
Is any one have a solution for a correct DQL query ?
Quoting from Christophe stoef Coevoet (Symfony Core Developer):
DQL is about querying objects. Supporting subselects in the FROM clause means that the DQL parser is not able to build the result set mapping anymore (as the fields returned by the subquery may not match the object anymore).
This is why it cannot be supported (supporting it only for the case you run the query without the hydration is a no-go IMO as it would mean that the query parsing needs to be dependant of the execution mode).
In your case, the best solution is probably to run a SQL query instead
(as you are getting a scalar, you don't need the ORM hydration anyway)
Details here.
add this function to your VenteRepository:
public function sumMontantPaye($clientId)
{
return $this->createQueryBuilder("v")
->select("sum(v.montantPaye) as sum")
->where("v.client = :id")
->andWhere("v.montantPaye > 0")
->setParameter("id", $clientId)
->setMaxResults(2)
->getQuery()->getSingleResult();
}
you can access the sum using $result["sum"] assuming $result is the variable assigned to this function in the controller

Semantical Error - Doctrine 2.0

These are my tables:
Table Gift:
-id
-price
...
Table Couple:
-id
-name
...
table registry: //provide a many-many relation between gifts and couples
-id
-coupleId
-giftId
table purchase:
-amount
-registryId
I already wrote a sql query to get all the gift info for a specific couple
$qb = $this->createQueryBuilder('g') //gift
->from('\BBB\GiftBundle\Entity\Registry', 'reg')
->select('g.id , g.price')
->where('reg.gift = g.id')
->andWhere('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->setParameter('coupleID', $coupleID);
OR
SELECT g.id , g.price,
FROM gift g, registry reg
WHERE reg.gift_id = g.id AND reg.couple_id = 1
I also want to get the total amount that for the gifts that have been bought (if any)
EX. SUM(purchase.amount) as totalContribute
I have tried:
$qb = $this->createQueryBuilder('g')
->from('\BBB\GiftBundle\Entity\Purchase', 'p')
->from('\BBB\GiftBundle\Entity\Registry', 'reg')
->select('g.id , g.price')
->addSelect('SUM(p.amount) as totalContribute')
->leftJoin('p','pp', 'ON','reg.id = pp.registry')
->where('reg.gift = g.id')
->andWhere('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->setParameter('coupleID', $coupleID);
but it gives me the following error:
[Semantical Error] line 0, col 145 near 'pp ON reg.id': Error: Identification Variable p used in join path expression but was not defined before.
First of all, you should define join condition in your SQL statements after joins, not in WHERE clause. The reason is that it's really not efficient. So the query shoul look like:
SELECT g.id , g.price,
FROM gift g JOIN registry reg ON reg.gift_id = g.id
WHERE reg.couple_id = 1
But about your Doctrine query, You get error because you're defining joins in wrong way. Your query should more like:
$qb = $this->createQueryBuilder('g') // You don't have put "from" beacuse I assume you put this into GiftRepository and then Doctrine knows that should be \BBB\GiftBundle\Entity\Gift
->select('g.id , g.price')
->addSelect('SUM(p.amount) as totalContribute')
->join('g.purchase','p') // pay attention for this line: you specify relation basing on entity property - I assume that is "$purchase" for this example
->leftJoin('p.registry', 'reg') // here you join with \BBB\GiftBundle\Entity\Purchase
->where('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->setParameter('coupleID', $coupleID);
Please treat this as pseudocode - I didn't check it works but it should like more like this.
One more thing - if your repository method returns object(s) of X entity you should put this method to XRepository.

Join query exception in Symfony 1.4.11/Propel 1.4.2

I need to run following query:
SELECT
m.TITLE,
m.MOMENTOIMAGE,
s.CREATED_AT,
s.UNREAD,
mem.FIRSTNAME,
mem.LASTNAME,
mem.MEMBER_PHOTO,
mem.ID
FROM `momento_send` s,
`send_distribution` sd,
`momento_distribution` d,
`momento` m,
`member` mem
WHERE
s.momento_idmember=6 AND
sd.id_send=s.id AND
sd.id_distribution=d.id AND
d.momento_id=m.id;
For that, I wrote following code in Symfony 1.4 (using Propel 1.4.2) (Thanks to #j0k)
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(MomentoPeer::TITLE);
$c->addSelectColumn(MomentoPeer::MOMENTOIMAGE);
$c->addSelectColumn(MomentoSendPeer::CREATED_AT);
$c->addSelectColumn(MomentoSendPeer::UNREAD);
$c->addSelectColumn(MemberPeer::FIRSTNAME);
$c->addSelectColumn(MemberPeer::LASTNAME);
$c->addSelectColumn(MemberPeer::MEMBER_PHOTO);
$c->addSelectColumn(MemberPeer::ID);
$c->addJoin(SendDistributionPeer::ID_SEND, MomentoSendPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(SendDistributionPeer::ID_DISTRIBUTION, MomentoDistributionPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(MomentoDistributionPeer::MOMENTO_ID, MomentoPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(MomentoDistributionPeer::MOMENTO_IDMEMBER, MemberPeer::ID, Criteria::INNER_JOIN);
$c->add(MomentoSendPeer::MOMENTO_IDMEMBER, $memberid);
//echo $c->toString();exit;
$records = SendDistributionPeer::doSelect($c);
Running this code generated following error
[wrapped: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN momento_send ON (send_distribution.ID_SEND=momento_send.ID) INNER JOI' at line 1]
Can someone suggest, what is the issue.
Just to give little more info, SQL printed with echo $c->toString();exit; was
SELECT momento.TITLE,
momento.MOMENTOIMAGE,
momento_send.CREATED_AT,
momento_send.UNREAD,
member.FIRSTNAME,
member.LASTNAME,
member.MEMBER_PHOTO,
member.ID
FROM
INNER JOIN momento_send ON (send_distribution.ID_SEND=momento_send.ID)
INNER JOIN momento_distribution ON (send_distribution.ID_DISTRIBUTION=momento_distribution.ID)
INNER JOIN momento ON (momento_distribution.MOMENTO_ID=momento.ID)
INNER JOIN member ON (momento_distribution.MOMENTO_IDMEMBER=member.ID)
WHERE momento_send.MOMENTO_IDMEMBER=6
Try with $c->setPrimaryTableName(MomentoPeer::TABLE_NAME);

update doctrine with join table

I am trying to update the field "note" in Note Entity which have a relation ManyToOne (bidirectionnel) with "ElementModule" and "Inscription", the entity "Inscription" have a relation ManyToOne with the "Etudiant" Entity
I tried this DQL Query:
$query = $this->_em->createQuery('update UaePortailBundle:Note u JOIN u.inscription i JOIN u.elementmodule e join i.etudiant et set u.note = ?3
where et.id = ?1 and e.id = ?2 ');
$query->setParameter(1, $etudiant);
$query->setParameter(2, $element);
$query->setParameter(3, $note);
$resultat = $query->execute();
i get this error
[Syntax Error] line 0, col 50: Error: Expected Doctrine\ORM\Query\Lexer::T_EQUALS, got 'i'
LEFT JOIN, or JOINs in particular are only supported in UPDATE statements of MySQL. DQL abstracts a subset of common ansi sql, so this is not possible. Try with a subselect or IDENTITY ( you must use the latest version of Doctrine 2.2.2 ) :
createQuery('update UaePortailBundle:Note u set u.note = ?3
where IDNETITY(u.inscription) = ?1 and IDENTITY(u.elementmodule) = ?2 ');
after searching and trying to find a solution for that using doctrine dql, i was not able to find it, so i used direct sql query to update the database.
$stmt = $this->getEntityManager()
->getConnection()
->prepare("update note set note = :note where `etudiant_id` like :etudiant_id and `elementmodule_id` like :elementmodule_id");
$stmt->bindValue('etudiant_id',$etudiant);
$stmt->bindValue('elementmodule_id' ,$element );
$stmt->bindValue('note', $note);
$stmt->execute();

Resources