0
votes

I am new in symfony2, I would like to register some user in my profile_tbl. here is my code in the controller.

 <?php

    namespace Ai\QABlogBundle\Controller;

    use Ai\QABlogBundle\Entity\Profile_tbl;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;



    class QABlogController extends Controller
    {

        /**
        *@Route("/" , name= "home")
        */
        public function homeAction()
        {
            return $this->render('AiQABlogBundle:QABlog:primaries/index.html.twig');

        }

        /**
        *@Route("/register/" ,name= "register")
        */

        public function registerAction(Request $request)
        {
            $profile = new Profile_tbl();

            $form  = $this -> createFormBuilder($profile)
                    -> add ('fname' , 'text')
                    -> add ('lname' , 'text')
                    -> add ('gender' , 'text')
                    -> add ('email' , 'text')
                    -> add ('register' , 'submit')
                    ->getForm();

            $form->handleRequest($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($profile);
                $em->flush();
                return new Response('News added successfuly');
            }

             $build['form'] = $form->createView();
             return $this->render('AiQABlogBundle:QABlog:primaries/registration.html.twig', $build);
        }


    }

When I try to run it in my browser it gives an error "Attempted to load class "Profile_tbl" from namespace "Ai\QABlogBundle\Entity". Did you forget a "use" statement for another namespace?"

I don't know what is wrong .. Can you help me?

my Profile_tbl entity

<?php

namespace Ai\QABlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Profile_tbl
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Profile_tbl
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="fname", type="string", length=255)
     */
    private $fname;

    /**
     * @var string
     *
     * @ORM\Column(name="lname", type="string", length=255)
     */
    private $lname;

    /**
     * @var string
     *
     * @ORM\Column(name="gender", type="string", length=255)
     */
    private $gender;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set fname
     *
     * @param string $fname
     * @return Profile_tbl
     */
    public function setFname($fname)
    {
        $this->fname = $fname;

        return $this;
    }

    /**
     * Get fname
     *
     * @return string 
     */
    public function getFname()
    {
        return $this->fname;
    }

    /**
     * Set lname
     *
     * @param string $lname
     * @return Profile_tbl
     */
    public function setLname($lname)
    {
        $this->lname = $lname;

        return $this;
    }

    /**
     * Get lname
     *
     * @return string 
     */
    public function getLname()
    {
        return $this->lname;
    }

    /**
     * Set gender
     *
     * @param string $gender
     * @return Profile_tbl
     */
    public function setGender($gender)
    {
        $this->gender = $gender;

        return $this;
    }

    /**
     * Get gender
     *
     * @return string 
     */
    public function getGender()
    {
        return $this->gender;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Profile_tbl
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }
}

I try to rename or make another entity ..

steps that I tried to generate my new entity. Creating Database:

php app/console doctrine:database:create

Generate Entity:

php app/console doctrine:generate:entity

Generate the Getters and Setters:

php app/console doctrine:generate:entities AiQABlogBundle

after this I run it in my browser.

and gives me an error ,

Attempted to load class "Profile" from namespace "Ai\QABlogBundle\Controller". Did you forget a "use" statement for e.g. "Twig_Profiler_Profile" or "Symfony\Component\HttpKernel\Profiler\Profile"?

1
Maybe you should show the other file. Is the class named correctly in it? Is the file named correctly? - sjagr
yes .. what file the entity file? - aiai
Is there any other way that I can make my registration work? Maybe need to change the code or something? Can you give me some sample on how to do it as you know I'm just new to symfony2? - aiai
Don't use underscores in classnames. and the tbl suffix is not necessary - Emii Khaos
By the way my new entity is Profile - aiai

1 Answers

0
votes

This happened to me a few times. It usually happens when I copy-paste an old class file to create a new class. Check your classname and the filename of the file containing your class; they should be the same. In your case they should be Profile_tbl and Profile_tbl .php respectively.

Update

Profile_tbl is converted to Profile/tbl.php as per the PSR-0 autoload convention which Composer uses by default to load classes inside the src/ directory. You'll have to rename the class to something that does not use an underscore e.g. ProfileTbl or simply Profile.

// This should be in the file: src/Ai/QABlogBundle/Entity/Profile.php
namespace Ai\QABlogBundle\Entity;

/**
 * Profile
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Profile
{
    // ...
}

Credits to Pazi for pointing this out.