0

I have a many to one association mapping where many Exams have one subject If I have to update marks property of all subject with exam id xxxx I use

Update namespace/Exam e set e.marks = 4 where e.subject.id = xxxx

it is not working can you please he help me to do that in a proper way ?

Sagar
  • 980
  • 1
  • 11
  • 27
  • What do you want to know? What is your question?? – Fumu 7 Sep 06 '14 at 07:56
  • hi, I updated my answer regarding to your second question. Is that what you meant? If it helped you, you could accept the answer as correct ;-) – SBH Sep 09 '14 at 17:37

1 Answers1

1

You can not access e.subject.id without a join in your query. But a join is not possible in a Doctrine update statement, since its MySQL specific.

What should work is to compare the whole entity:

$subject = $this->getEntityManager()->getRepository('namespace\subject')
    ->find(xxx);

$query = $this->getEntityManager()->createQueryBuilder()
    ->update('namespace\Exam', 'e') 
    ->set('e.marks', '4')
    ->where('e.subject = :subject')
    ->setParameter('subject', $subject);

Or, if you don't want to have another database query and the primary key of your subject entity consists only of one column (e.g. id), you could also compare with this primary key:

$query = $this->getEntityManager()->createQueryBuilder()
    ->update('namespace\Exam', 'e') 
    ->set('e.marks', '4')
    ->where('e.subject = :id')
    ->setParameter('id', xxx);

Also have a look at this question, the user wasn't able to join in his update statement too.

Edit: added more general approach

Community
  • 1
  • 1
SBH
  • 1,787
  • 3
  • 22
  • 27
  • if the property is not an identity then what should we do ? – Sagar Sep 07 '14 at 04:20
  • you mean if the primary key consists of more than one column? In that case you can compare with the complete subject object. – SBH Sep 07 '14 at 18:12