Print Sql query in Symfony - mysql

I need to findout the normal sql query for the below. Can you guys please suggest me how can i achieve in Symfony.
Example:
$r = Doctrine_Query::create()
->select('u.worked_hours')
->from('tasksComments u')
->where('u.tasks_id = ?', $arr_values['tasks_id'])
->andwhere('u.id != ?', $arr_values['id'])
->andwhere('u.created_at LIKE ?', $date."%");
$results1 = $r->execute();

On the query object, use the method getSQL.
In your case:
$r = Doctrine_Query::create()
->select('u.worked_hours')
->from('tasksComments u')
->where('u.tasks_id = ?', $arr_values['tasks_id'])
->andwhere('u.id != ?', $arr_values['id'])
->andwhere('u.created_at LIKE ?', $date."%");
var_dump($r->getSQL()); // print the SQL query - you will need to replace the parameters in the query
var_dump($r->getParams()); // print the parameters of the query so you can easily replace the missing parameters in the query
Note that I don't know the namespace of Doctrine_Query but I am assuming that your Query object in this Query object in the Doctrine API documentation.

Using Symfony 1.4 with Doctrine 1.2.3
echo $q->getSqlQuery();

You can view all the executed queries in the profiler toolbar by clicking on the db panel icon (the last section of the toolbar) then on the [Display runnable query] link under your query.

Related

Need suggestion in query builder of Laravel 9 application

How to avoid foreach-loop because Laravel query builder is used to get a single record detail.
When I tried without foreach-loop, the following error occurred "Property [name] does not exist on this collection instance".
Controller
public function show(Student $student)
{
$result = DB::table('students')
->select('*')
->where('id', "=" ,$student['id'])
->get();
foreach($result as $studentData){}
return view('student.show', ['studentData' => $studentData]);
}
Blade View
{{ $studentData->name}}
->get() always returns a Collection, even if there's only one match. If you only want the first record, you can use ->first() instead.
Do you really have to use raw queries? I assume Student is a Laravel Model, in that case, you should be able to use Student::find($id) to get the model directly.

Use "Query Builder" to create a function allowing to take the average of values - Symfony

I need help concerning the use of "Query Builder" in Symfony.
I would like to retrieve the values ​​of an attribute (Note) from one of my tables (Avis) in my database. After that, I would like to average all of his scores for display on my site.
For now I have the SQL query which achieves what I want :
SELECT AVG(avis.note) AS notetotal FROM avis
But afterwards, I don't understand what to do, or at least how "Query Builder" works
With the QueryBuilder you can build your queries step by step in a programmatically way, but its actualy only build the DQL wich you can get with $queryBuilder->getDql(). There are some cases where its easier to use the QueryBuilder instead of concatinate a long DQL query string.
There are two possible ways to build the query with the QueryBuilder. But for most cases its recommended to use plain DQL where ever you can, as its more readable.
First with the Expr Class
$queryBuilder = $this->getDoctrine()->getManager()->createQueryBuilder('a');
$queryBuilder->select($queryBuilder->expr()->avg('a.note'))
->from(Avis::class, 'a');
$avg = $queryBuilder->getQuery()->getSingleResult();
// $avg = [1 => (int) AVG]
or without Expr Class
$queryBuilder = $this->getDoctrine()->getManager()->createQueryBuilder('a');
$queryBuilder->select('AVG(a.note) as notetotal')
->from(Avis::class, 'a');
$avg = $queryBuilder->getQuery()->getSingleResult();
// $avg = ['notetotal' => (int) AVG]
The same in DQL
$dql = 'SELECT AVG(a.note) AS notetotal FROM ' . Avis::class . ' a';
$avg = $this->getDoctrine()->getManager()->createQuery($dql)->execute();
// $avg = [0 => ['notetotal' => (int) AVG]]

how to convert sql command to yii2

select g_code
from t_graph_name
where character_length(g_code) = 5
You need to read about build Query in Yii2..also you can read about active record too.
after reading it, you can see you can build it by Query builder like this:
$rows = (new \yii\db\Query())
->select(['g_code'])
->from('t_graph_name')
->where('character_length(g_code) = 5')
->all();

cakephp3 own query builder

i'm new in cakephp, and i'm following the toutorial, i came from other languages and is not usual for me to read query like this:
public function findTagged(Query $query, array $options)
{
$columns = [
'Articles.id', 'Articles.user_id', 'Articles.title',
'Articles.body', 'Articles.published', 'Articles.created',
'Articles.slug',
];
$query = $query
->select($columns)
->distinct($columns);
if (empty($options['tags'])) {
// If there are no tags provided, find articles that have no tags.
$query->leftJoinWith('Tags')
->where(['Tags.title IS' => null]);
} else {
// Find articles that have one or more of the provided tags.
$query->innerJoinWith('Tags')
->where(['Tags.title IN' => $options['tags']]);
}
return $query->group(['Articles.id']);
}
This is a simple query and it's easy to understand ,but if i have a more complex query with a lot of join etc, is there the possibility to write your own query with sql sintax, can you help me translating this code to a query written in sql?
Thanks
You can write directly execute an SQL query with $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) but I would recommend to stick to cakephp's ORM.
If you want to know how the query that you posted above translates into SQL, I would recommend to use the DebugKit. If you have in your app configuration debug = true, you will see this red rectangle at the bottem right corner, when you open your app in the browser. Click on it and click on "Sql queries": you will find the generated SQL from the query above somewhere in there. Alternatively you could use query logging (see here: https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging)

Troubles with IN in Yii query builder

I use Yii Framework and i need to build difficult query with many conditions.
I'm filling 2 arrays $conditions and $values.
And i have one problem.
Below is example
When i use
$usersId = '1,2';
$conditions[] = 'e.user_id IN(:usersId)';
$values[':usersId'] = $usersId;
I get only value from user_id = 1
When i'm not use option and write manually
$usersId = '1,2';
$conditions[] = 'e.user_id IN(' . $usersId . ')';
no problem.
Of course i can use second construction, but it seems not very good.
You should addInCondition
$criteria->addInCondition('e.user_id',array(1,2));
Yii way would be to use CDbCriteria addInCondition function
$usersId = array(1,2); //must be array
$criteria=new CDbCriteria();
$criteria->addInCondition('user_id',$usersId);
$result = MyModel::model()->findAll($criteria);
$values[':usersId'] = $usersId;
If I understand your wuestion correctly, you can use the BindParam function in yii?
Instead of this - $values[':usersId'] = $usersId;
Write this - $command->BindParam(':usersId', $usersId, PDO::PARAM_STR);
Very simply, you're binding your parameters to your command statement.
Hope it works!