0

Old Query in symfony 1.4 and doctrine 1.2

$user = Doctrine_Query::create()
                ->from('User.u')
                ->innerJoin('u.State s')
                ->where('u.id = ?', $id)
                ->andWhere('u.state_id = ?', $state_id)
                ->fetchOne();

Now my Query in symfony2:

$repository = $this->getDoctrine()
->getRepository('FrontendAccountBundle:User');

$user = $repository->findBy(array(
    'activationId' => $activation_id), 
    array('state' => 3));

My error is comming up:

Unrecognized field: state

What is the problem?

Edit: reformatted code

Update

User-Entity:

namespace Frontend\AccountBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User implements UserInterface, \Serializable
{

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


    /**
     * @var \State
     *
     * @ORM\ManyToOne(targetEntity="State")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="state_id", referencedColumnName="id")
     * })
     */
    private $state;




    /**
     * Set activationId
     *
     * @param string $activationId
     * @return User
     */
    public function setActivationId($activationId)
    {
        $this->activationId = $activationId;

        return $this;
    }

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

    /**
     * Set state
     *
     * @param \Frontend\AccountBundle\Entity\State $state
     * @return User
     */
    public function setState(\Frontend\AccountBundle\Entity\State $state = null)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return \Frontend\AccountBundle\Entity\State 
     */
    public function getState()
    {
        return $this->state;
    }

    public function __construct()
    {
        $this->isActive = true;
        $this->salt = md5(uniqid(null, true));
    }

    /**
     * @inheritDoc
     */
    public function getUsername()
    {
        return $this->email;
    }
    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
        ) = unserialize($serialized);
    }
}

User-Entity:

namespace Frontend\AccountBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * State
 *
 * @ORM\Table(name="state")
 * @ORM\Entity
 */
class State
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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



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

    /**
     * Set state
     *
     * @param string $state
     * @return State
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     * @return State
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }
}
craphunter
  • 961
  • 2
  • 13
  • 34

2 Answers2

1

The problem is that the variable in the User entity is "state" not "stateId". You must always use the names from the entity, not the database. The join from User to State also needs to be done since the stateId is in the State entity.

When joins are needed you are probably better off using queryBuilder or DQL.

Here's a post about joins in Doctrine 2 queryBuilder: doctrine 2 query builder and join tables

Here's the documentation from the Symfony Book for Doctrine: http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

Here's an example from my project that is very similar to your problem:

    $uid = 2;
    $rep = $this->getDoctrine()->getRepository('DevondevTrackRTimeBundle:Activity');
    $q = $rep->createQueryBuilder('a')
        ->select ('a.activityId, a.startTime, a.endTime, u.username')
        ->join('a.login','u')
        ->where('u.id = :uid')
        ->setParameter('uid', $uid)
        ->getQuery();

    $acts = $q->getResult();

If I didn't need anything from the user table the query could be written as

    $uid = 2;
    $rep = $this->getDoctrine()->getRepository('DevondevTrackRTimeBundle:Activity');
    $q = $rep->createQueryBuilder('a')
        ->where('a.login = :uid')
        ->setParameter('uid', $uid)
        ->getQuery();

    $acts = $q->getResult();

This is your query reworked in the same way:

    $rep = $this->getDoctrine()->getRepository('FrontendAccountBundle:User');
    $q = $rep->createQueryBuilder('u')
        ->join('u.state','s')
        ->where ('u.id = :uid')
        ->andWhere ('s.stateId = :sid')
        ->setParameters(array('uid' => $id, 'sid' => $state_id))             
        ->getQuery(); 

    $user = $q->getSingleResult();        
Community
  • 1
  • 1
Peter Wooster
  • 6,009
  • 2
  • 27
  • 39
  • Sorry, no, if I use $user = $repository->findBy(array('activationId' => $activation_id), array('State' => 3)); or with 'state' with small cap. The error is still coming up. – craphunter Jan 26 '13 at 13:23
  • You also need to do the join from user to state. – Peter Wooster Jan 26 '13 at 13:27
  • Okay, would you mind to post an example for this very simple query with this two tables? I am very new in doctrine2 and I am really stucked. – craphunter Jan 26 '13 at 13:32
  • It's very similar to your Doctrine 1.2 code. I've added a link to another post in my answer. Sorry, but I can't put an explicit code example here, I'm on an iPad, no code editor or test bed. – Peter Wooster Jan 26 '13 at 13:36
  • The point is, that I need one little example with the entity manager and innerJoin. I never needed to use it. – craphunter Jan 26 '13 at 13:39
  • Either not workting. I am really disparing. Symfony1.4 with doctrine 1.2 was so freaking easy. Really since 3 h I am stucked actually in this very easy little problem. – craphunter Jan 26 '13 at 14:08
  • The section of the Symfony book I just linked to has examples of this. I'm also relatively new to Doctrine 2. I have a similar table and will try to get a working example when I'm back on my Mac. – Peter Wooster Jan 26 '13 at 14:12
  • Hey, do you might have a solution? I am totally disparing...Don't know what to do. – craphunter Jan 26 '13 at 15:35
  • A little bit, but still not working yet. But it lighted me up a little bit. I answer if I got it. Thanks for the reply! – craphunter Jan 26 '13 at 17:23
  • No, I don't get it at all. Symfony1.4 in combination with doctrine1.2 was so easy. I just want to join two tables and make a query like above in my question. – craphunter Jan 26 '13 at 17:49
  • Thanks Peter for your help. You lighted me a up a little bit! If you want to see a solution take a look to my new post. THANKS and greetings from Germany to Canada! – craphunter Jan 26 '13 at 18:25
0

Thanks to Peter to light me up a little bit!!!

If you don't want again a the stupid solution from symfony2(-docs) and doctrine2, because you need much more code than in symfony1.4, like that way http://symfony.com/doc/current/book/doctrine.html#joining-to-related-records. Try my solution.

Here is the result.

        $em = $this->getDoctrine()->getEntityManager();
        $user = $em->createQuery('SELECT u FROM FrontendAccountBundle:User u
              INNER JOIN FrontendAccountBundle:State s
                WHERE
                u.activation_id=:activation_id
                and
                u.state=:state_id
                ')
            ->setParameter('activation_id', $activation_id)
            ->setParameter('state_id', 3)
            ->getSingleResult();
craphunter
  • 961
  • 2
  • 13
  • 34