1

I'm using Symfony 2.3

I have a function that lets me send email notifications when pressed. It sends email and everything, but when I want to retrieve an id from the database to send it as part of the subject of the email, it doesn't work.

private function updateForm($data)
    {
$repository = $this->getDoctrine()->getRepository('MainBundle:Unity');     
            $attached = $repository->find('unityid');

$message = \Swift_Message::newInstance()
                ->setSubject($attached)
                ->setFrom('some@email')
                ->setTo('some@email')
                ->setBody($attached);
                $this->get('mailer')->send($message);
return true;
}

I do not know why I cannot retrieve the ID. I'm doing exactly like the Symfony documentation says.

What am I missing?

4
  • what you doing with that id ? printing or returning that id ? return true! Commented Jul 13, 2017 at 22:20
  • I'm just sending it to the email in ->setBody Commented Jul 13, 2017 at 22:21
  • The repository find() method finds an entity by its primary key / identifier. 'unityid' looks like it's probably a column name rather than a primary key value. Commented Jul 13, 2017 at 22:23
  • Yes, it is a column name. How would I go to get a value from an specific column? Commented Jul 13, 2017 at 23:42

1 Answer 1

1

Assuming you want to get Id from Unity entity based on unityid. To fine records based on specific column you can use findOneBy() or findBy(). Here we are using findOneBy() doctrine function to get single. Make sure you have replaced <Your value> with your value.

$attached = $repository->findOneBy(array('unityid' => '<Your value>'));

You have not mentioned your primary key of Unity entity so we are assuming id is name of primary key column. $attached is object rather than value so you need to use getId() function to get value of id column.

Full Code

<?php
    private function updateForm($data)
    {
        $repository = $this->getDoctrine()->getRepository('MainBundle:Unity');     
        $attached = $repository->findOnyBy(array('unityid' => '<Your value>'));

        $message = \Swift_Message::newInstance()
            ->setSubject($attached->getId()) // $attached->getId() returns value of "id" column
            ->setFrom('some@email')
            ->setTo('some@email')
            ->setBody($attached);
            $this->get('mailer')->send($message);
        return true;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.