I am not getting idea how to create sql views in symfony. Please can anyone help me how to create sql views in symfony.
1 Answer
first create the test data :
CREATE TABLE T
(`id` int, `name` varchar(5))
;
INSERT INTO T
(`id`, `name`)
VALUES
(1, 'john'),
(2, 'henry')
;
creat view in Symfony :
$em = $this->getDoctrine()->getManager();
$connection = $em->getConnection();
$statement = $connection->prepare("create view View_T as select * from T where id = 1;");
$statement->execute();
then you can use the View_T by Query:
select * from View_T
get the result
| id | name |
|----|------|
| 1 | john |
Details about view you can learn from SQL CREATE VIEW, REPLACE VIEW, DROP VIEW Statements
4 Comments
Narayan Ghimire
Thank you @IT weiHan, I have understood. But I am unclear in one things.Do I need to run creating view statement every time?
Wei Lin
no, view just need create one time and use any time. when you create exist view it'll throw exception.
Narayan Ghimire
okay. Thank you I have understood now.Moreover, please could you tell me,How I can change collation of table in symfony. I have this query " ALTER TABLE table_data CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" .I think I have to run it through doctrine. I am not getting how to do
Wei Lin
please ask another question. it'll get more detail.