Dani California
Dani California

Reputation: 331

Pulling values from database in Symfony 2.3

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?

Upvotes: 1

Views: 76

Answers (1)

Gopal Joshi
Gopal Joshi

Reputation: 2358

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;
    }

Upvotes: 1

Related Questions