Hello everyone I'm trying to use phalcon without ORM. I want to separate business logic and data access with DataMapper pattern and I can't find how I can build queries without raw SQL, but using original phalcon queryBuilder.
Is there any chance to do it?
Is this what you are looking for?
<?php
//Getting a whole set
$robots = $this->modelsManager->createBuilder()
->from('Robots')
->join('RobotsParts')
->orderBy('Robots.name')
->getQuery()
->execute();
//Getting the first row
$robots = $this->modelsManager->createBuilder()
->from('Robots')
->join('RobotsParts')
->orderBy('Robots.name')
->getQuery()
->getSingleResult();
From Phalcon docs: Phalcon Query Language (PHQL)
I figured out how to do it
I'm using phalcon with doctrine DBAL
it's working great for me
I found 2 ways solve it if i'm not mistaken.
Solution 1#
use Phalcon\Mvc\Model\Query\Builder;
$result = $this->modelsManager->createBuilder()
->columns("table1.f1, table1.f2,table2.f3,table2.f4")
->From('table1')
->innerjoin('table2', 'table1.matchfield = table2.matchfield')
->where("table1.fieldname = '$value' ")
->getQuery()
->execute();
Solution 2#
$res = $this->db->execute("UPDATE Tablename SET field1= ?,field2=?,field3=? WHERE id = ?",array($field1,$field2,$field3,$id));
Related
I am unable to figure what is the problem here.
I have created the below query using doctrine Query Builder
$repo = $this->em->getRepository(self::STORE_TIMING);
$qb = $repo->createQueryBuilder('store_timings');
$qb->select('st')
->where('st.id = :identifier')
->setParameter('identifier', 100);
When i print the DQL :
print_r($qb->getDQL());die();
The query that is output is :
SELECT st FROM Test\BotBundle\Entity\StoreTimings store_timings WHERE st.id = :identifier
To my surprise the :identifier is not being replaced.
Request for some guidelines here.
If you are using getDQL It doesn't return the query with parameter.
I advise you to use the _profiler to view the complete query with parameter
I want to get one single value from entity.Can anyone help me here.
Here is my code.Please let me know what is missing here.
$query = $em->createQuery("SELECT e FROM AdminBundle:MailTemplates e WHERE e.keyword = '" .$keywordVal."'");
$query->execute();
$result = $query->getResult();
echo $result ->getId();
Here i want the 'id'.
This is noted in the documentation how you can do this.
So given you're code this will become:
$query = $em->createQuery("SELECT e.id FROM AdminBundle:MailTemplates e WHERE e.keyword = ?1");
$query->setParameter(1, $keywordVal);
$query->execute();
$result = $query->getResult(); // array of MailTemplates ids
Note: I also made use of setParameters instead of setting the value directly in the query.
In your controller:
$this->get('database_connection')->fetchColumn('select id from mail_templates where...');
That's much better for performance and much easier if you don't want to have a deal with query builder and other doctrine orm stuff.
Using the query builder you could do...
$queryBuilder = $em->createQueryBuilder('e');
$queryBuilder
->select('e.yourColumn')
// This will return just this column
// Alternatively you could omit any select to return the whole object
// that you could then use like $object->getYourColumn() if you so chose
->where($queryBuilder->expr()->eq('e.keyword', ':keyword'))
->setParameter('keyword', $keyword)
;
return $queryBuilder
->getQuery()
->getResult();
try this on loading Entities instead of creating own queries
Loading the entity with the Repository.
$rep = $this->getDoctrine()->getManager()->getRepository("Bundlename:Entity");
//find one by keyword -> single entity
$entity = $rep->findOneBy(array('keyword' => $keyword));
//find all by keyword - Array of entities
$result = $rep->findBy(array('keyword' => $keyword));
I am using Eloquent ORM outside of Laravel-4 and I am building a custom Paginator.
First, I build a query using Fluent Query Builder. I want to get the number of result the query could return using count() and then I do a custom pagination using take(x) and skip(y). I need to do the count() before the take()->skip()->get() so I dont fall outside of the page range. The problem is that when I use the count() method on the query, it seems to remove any select I added previously.
I isolated the problem to this simple example:
$query = DB::table('companies')
->join('countries','companies.country_id','=','countries.id')
->select(
'companies.name as company_name',
'countries.name as country_name'
);
$nbPages = $query->count();
$results = $query->get();
//$results contains all fields of both tables 'companies' and 'countries'
If i invert the order of the count and get, it works fine:
$results = $query->get();
$nbPages = $query->count();
//$results contains only 'company_name' and 'country_name'
Question: is there a more elegant way the using something like this:
$tmp = clone $query;
$nbPages = $tmp->count();
$results = $query->get();
There is not, unfortunately. Open issue on github about the problem: https://github.com/laravel/framework/pull/3416
I am using Yii's Yii::app()->db->createCommand() to build an SQL query. In order to view the SQL code that Yii generates, I am using the getText() method of CDBCommand. Problem is, when I use the getText() method on SQL code that contain parameters, for example:
Yii::app()->db->createCommand()
->select("name")
->from('package')
->where('id=:id', array(':id'=>5))
->queryRow();
the getText() method returns the following SQL:
select name from package where id=:id
instead of:
select name from package where id=5
This is fine for simple queries, but for more complex queries with lots of parameters, it is quite a pain to copy/paste each parameter into the SQL code to test it.
Is there any way to display the parameters directly inside the SQL using getText() or some other method in Yii?
Cheers!
$sql = Yii::app()->db->createCommand()
->select("name")
->from('package')
->where('id=:id', array(':id'=>5))
->queryRow();
$query=str_replace(
array_keys($sql->params),
array_values($sql->params),
$sql->getText()
);
You can use the CDbConnetion::enableParamLogging propery. For instance, in config/main.php:
'db' => array (
'enableParamLogging' => true,
and the shown and logged error will contain the bound values.
why don you try as below.i am not an expert just posting to my knowledge if its no suitable for your prob pardon me...
$connection=Yii::app()->db;
$id=5; // you can able to change by "GET" or "POST" methods
$sql="SELECT name FROM package WHERE id = :id ";
$command = $connection->createCommand($sql);
$command->bindParam(":id",$id,PDO::PARAM_STR);
$dataReader=$command->query();
$rows=$dataReader->readAll();
$namevalue=array();
foreach($rows as $max)
{
$namevalue = $max['name'];
}
echo $namevalue; // which is the value u need
thank you...
Looks like you're after the PDOStatement that Yii is preparing:
$cmd = Yii::app()->createCommand();
$cmd->select("name")
->from('package')
->where('id=:id', array(':id'=>5))
->queryRow();
// returns a PDO Statement - http://php.net/manual/en/class.pdostatement.php
Yii::log($cmd->getPdoStatement()->queryString);
Does that work for you? Seems like it should (code untested).
I'm using Doctrine 1.2 and Symfony 1.4.
In my action, I have two different query that return different result set. Somehow the second query seem to change the result (or the reference?) of the first one and I don't have any clue why..
Here is an example:
$this->categories = Doctrine_Query::create()
->from('Categorie AS c')
->innerJoin('c.Activite AS a')
->where('a.archive = ?', false)
->execute();
print_r($this->categories->toArray()); // Return $this->categories results, normal behavior.
$this->evil_query = Doctrine_Query::create()
->from('Categorie AS c')
->innerJoin('c.Activite AS a')
->where('a.archive = ?', true)
->execute();
print_r($this->categories->toArray()); // Should be the same as before, but it return $this->evil_query results instead!
Why Doctrine behave this way ? It's totally driving me crazy. Thanks!
To make it simple it seem like the Query 2 are hijacking the Query 1 result.
Use something like this between queries ($em - entity manager):
$em->clear(); // Detaches all objects from Doctrine!
http://docs.doctrine-project.org/en/2.0.x/reference/batch-processing.html
In the API docs for the toArray() method in Doctrine_Collection it says:
Mimics the result of a $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
I suspect to answer this question to your satisfaction you're going to have to go through the source code.
This seems like it has to be an issue of $this->categories and $this->evil_query pointing to the same place. What are the results of $this->evil_query === $this->categories and $this->evil_query == $this->categories?
Hubert, have you tried storing the query into a separate variable and then calling the execute() method on it?
I mean something like:
$good_query = Doctrine_Query::create()
->from('...')
->innerJoin('...')
->where('...', false)
;
$evil_query = Doctrine_Query::create()
->from('...')
->innerJoin('...')
->where('...', true)
;
$this->categories = $good_query->execute();
$this->evil_query = $evil_query->execute();
It seems like both attributes (categories and evil_query) are pointing at the same object.
Erm, after both queries you print_r the result of the first query - change the last line to print_r($this->evil_query->toArray()); to see the difference :)