Doctrine 2 ORM does not support INSERT
via DQL or the DQL query builder. For a complete syntax, check the EBNF of DQL.
To handle inserts in ORM, you always manually instantiate an entity and persist it with the entity manager:
$user = new MyEntityUser();
$entityManager->persist($user);
$entityManager->flush();
You can only handle SELECT
, UPDATE
and DELETE
via DQL in Doctrine ORM:
Select:
SELECT u FROM MyEntityUser u WHERE u.id = :userId
Update:
UPDATE MyEntityUser u SET u.status = 'banned' WHERE u.id = :userId
Delete
DELETE MyEntityUser u WHERE u.id = :userId
You can handle these operations with the QueryBuilder as well:
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
->select('u')
->from('MyEntityUser', 'u')
->where($queryBuilder->expr()->eq('u.id', ':userId'));
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
->delete('MyEntityUser', 'u')
->where($queryBuilder->expr()->eq('u.id', ':userId'));
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
->update('MyEntityUser', 'u')
->set('u.status', 'banned')
->where($queryBuilder->expr()->eq('u.id', ':userId'));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…