I would like to get the 'real' SQL Query doctrine is passing to the SQL Server:
<?php
$em = $this->getDoctrine()->getEntityManager();
$myQuery = $em->createQuery('SELECT v FROM ....... v');
echo $myQuery->???????
?>
What I must to write instead of ???????? characters ?
I have tried with getSQLQuery() and with getSQL() but no luck for now.
Thanks..
You were almost there, it's getSql, not getSQL:
$myQuery->getSql()
You could try this:
$myQuery->getResult();
See if it helps
$myQuery->getDql();
Related
I'm sorry for my bad english, I have a little problem but i don't understand how to solve it.
When I do SQL update, I have one question mark ? which appears in the field
Here is the code
public function FooterUpdate($footer_text, $client_id) {
$connection = new Connection;
$db = $connection->openConnection();
$req = $db->prepare("UPDATE jcmsi_parametre SET footer_text = :footer_text WHERE client_id = :client_id");
$req->bindValue(':footer_text', $footer_text);
$req->bindValue(':client_id', $client_id);
$req->execute();}
The text before insert
<p><em class="fa fa-arrow-right"></em>Contact</p>
The text after insert
<p><em class="fa fa-arrow-right"></em><a href="contact.php" >Contact?</a></p>
After the insert query, I have the word Contact with the question mark ?.
I try echo $footer_text just before update and the text is ok, but not in sql
If you have an idea
Thank you
Didier
I have an Asset Entity that uses an embedded association:
/**
* #ORM\Entity
*/
class Asset
{
....
/**
* #ORM\Embedded(class="Money\Money")
*/
private $code;
I want to search this class and my first instinct was to do something like this:
public function findOneByCurrencyCode(string $currencyCode)
{
$qb = $this->assetRepository->createQueryBuilder('asset');
$qb->innerJoin('asset.code', 'code')
->where('code.currency = :currency')
->setParameter('currency', $currencyCode);
$result = $qb->getQuery()->getOneOrNullResult();
return $result;
}
This, however, returns the following:
[Semantical Error] line 0, col 65 near 'code WHERE code.currency': Error:
Class Domain\Asset\Asset has no association named code
How do you search embedded classes?
EDIT:
I can get a result by doing something like this, however, this I feel is a hack:
$query = "SELECT * from asset where code_currency='BTC';";
$statement = $this->objectManager->getConnection()->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
return $result;
I tried a bunch of different things and managed to get the answer:
$qb = $this->assetRepository->createQueryBuilder('asset');
$qb->where('asset.code.currency = :currency')
->setParameter('currency', $currencyCode);
$result = $qb->getQuery()->getOneOrNullResult();
return $result;
Turns out no inner join is required. Not sure why and perhaps someone can answer this in time, however the above appears to work with embedded objects
Hope this helps someone else.
"Embedded associations" does NOT exist. So, you doesn't need JOINS.
An embedded is part of your entity along with the others properties.
You have to do something like that:
SELECT u
FROM User u
WHERE u.address.city
(In this query, your entity is User, your embedded is Address and *city is the property of your
embedded).
It's pretty good explained in Doctrine documentation:
Doctrine embeddables
I tried to query Loopback js Model using "inq" (mongo $in) syntax
like this:
let itemNames = [/test/i, /test2/i];
app.models.skill.find({where: {name: {inq: itemNames}}}, ....
But loopback is changing regexp to strings.
loopback sends strings like
{ name: { $in: [ "/test/i", "/test2/i" ] } }
expected to work like described here:
https://docs.mongodb.com/manual/reference/operator/query/in/#use-the-in-operator-with-a-regular-expression
Can you suggest a fix or a workaround for this (but I can't patch loopback itself it is a business requirement)
Loopback accepted my changes,
https://github.com/strongloop/loopback-datasource-juggler/pull/1279
so it should be possible to use regexps like in mongo.
You may create inq item like this:
let itemNames = [new RegExp('/test/i'), new RegExp('/test2/i')];
It worked for me.
I developed a small script to update some fields in two tables. The query run with no error, but for any reason nothing happen on these fields. I'm sure that something are omittedd by me, but I don't know what. Any idea?
<?php
//connection to the database
$connect=mysql_connect("localhost","xxxxxx","xxxxxxxxx") or
die("Unable to Connect"); echo ("Connected to server<br>");
//select a database to work with
mysql_select_db("xxxxxxxx") or die("Could not open the db"); echo ("Connected to database<br>");
//execute the SQL query tu update Price taxes on Products
$sql= "UPDATE pslpn_product SET id_tax_rules_group='68'";
$sql= "UPDATE pslpn_product_shop SET id_tax_rules_group='68'";
or die ('Could not update data: ' . mysql_error());
//close the connection
echo ("Finalizado<br>")
?>
If you forget to add WHERE clause, it will update all rows in the table.
UPDATE table_name SET field = value WHERE id = something -- or else
You forgot to execute your statement.
The mysql_connect() extension is deprecated. Use the MySQLi or PDO_MySQL extension instead: When should I use MySQLi instead of MySQL?
With mysqli you can execute a query like this:
if ($connect->query($sql) === TRUE) {
echo “Updated";
} else {
echo "Error " . $connect->error;
}
Read more: http://www.w3schools.com/php/php_mysql_update.asp
I'm looking at Caching and how to use it in Doctrine.
I've got the following in my Zend Framework Bootstrap.php:
// Build Configuration
$orm_config = new \Doctrine\ORM\Configuration();
// Caching
$cacheOptions = $options['cache']['backendOptions'];
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new Memcache;
$memcache->connect($cacheOptions['servers']['host'], $cacheOptions['servers']['port']);
$cache->setMemcache($memcache);
$orm_config->setMetadataCacheImpl($cache);
$orm_config->setQueryCacheImpl($cache);
$orm_config->setResultCacheImpl($cache);
I'm running a very simple query on my DB using:
self::_instance()->_em->getRepository('UserManagement\Users')->find('1');
And I'm not sure if I'm using caching properly, because with it on (as
per the above config) the query seems to take twice as long to execute
as with it disabled, is this right?
Thanks in advance,
Steve
I seem to have sorted this myself, sort of related to enter link description here. Basically, from what I understand a repository query like:
self::_instance()->_em->getRepository('UserManagement\Users')->find('1');
Will not cache the results. If the same query is executed again throughout the script processing, it will not perform the search and use the result it has in memory - this isn't the same as real caching, in my case using Memcache.
The only way to achieve this, is to override the Doctrine EntityRepository find() method in a custom repository with something like:
public function find($id)
{
// Retrieve an instance of the Entity Manager
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('u')
->from('UserManagement\Users', 'u')
->where('u.id = :id')
->setParameter('id', $id);
$query = $qb->getQuery();
$query->useResultCache(TRUE);
$result = $query->getSingleResult();
return $result;
}
Notably, the most important line from the above is $query->useResultCache(TRUE); - this informs the Application to cache the results.
Hope this helps.